Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize SSE Transport Configuration to Prevent Duplicate Connections #2493

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

aliyun1024qjc
Copy link

Overview

This PR addresses the issue of duplicate SSE connections and MCP tools creation when both WebFlux and HTTP Client implementations are available.

Current Issues

  1. Duplicate SSE Connections Problem

    • When both WebFlux and HTTP Client are available, McpClientAutoConfiguration loads both implementations
    • SseWebFluxTransportAutoConfiguration and SseHttpClientTransportAutoConfiguration create duplicate connections
    • Results in unnecessary resource consumption and potential performance issues
  2. Duplicate MCP Tools Creation

    • Multiple identical MCP tools are created due to duplicate transport implementations
    • Affects system efficiency and resource utilization
    • May cause confusion in tool management and callbacks

Solution

  1. Transport Mode Configuration

    public enum SseTransportMode {
        WEBFLUX,    // Default implementation
        HTTP_CLIENT
    }
  2. Configuration Property

    spring.ai.mcp.client.sse.transport-mode=WEBFLUX  # Default
    # or
    spring.ai.mcp.client.sse.transport-mode=HTTP_CLIENT
  3. Implementation Details

    • Added transport mode selection in McpSseClientProperties
    • Used conditional loading to ensure single implementation
    • Optimized tool creation process
    • Maintained backward compatibility

Benefits

  1. Resource Efficiency

    • Single SSE connection per endpoint
    • Optimized tool initialization
    • Reduced memory footprint
  2. Better Configuration Control

    • Explicit transport mode selection
    • Clear configuration options
    • IDE support with metadata
  3. Improved Stability

    • Prevents duplicate connections
    • Streamlined tool management
    • Better error handling

Testing Done

  • Verified single transport initialization
  • Tested tool creation and management
  • Validated configuration switching
  • Checked backward compatibility

Related Files

  • McpClientAutoConfiguration.java
  • McpSseClientProperties.java
  • SseWebFluxTransportAutoConfiguration.java
  • SseHttpClientTransportAutoConfiguration.java
  • additional-spring-configuration-metadata.json

Additional Notes

  • This change is backward compatible
  • Added configuration metadata for better IDE support
  • Documentation has been updated accordingly

1. **Duplicate SSE Connections Problem**
   - When both WebFlux and HTTP Client are available, `McpClientAutoConfiguration` loads both implementations
   - `SseWebFluxTransportAutoConfiguration` and `SseHttpClientTransportAutoConfiguration` create duplicate connections
   - Results in unnecessary resource consumption and potential performance issues

2. **Duplicate MCP Tools Creation**
   - Multiple identical MCP tools are created due to duplicate transport implementations
   - Affects system efficiency and resource utilization
   - May cause confusion in tool management and callbacks

1. **Transport Mode Configuration**
   ```java
   public enum SseTransportMode {
       WEBFLUX,    // Default implementation
       HTTP_CLIENT
   }
   ```

2. **Configuration Property**
   ```properties
   spring.ai.mcp.client.sse.transport-mode=WEBFLUX  # Default
   # or
   spring.ai.mcp.client.sse.transport-mode=HTTP_CLIENT
   ```

3. **Implementation Details**
   - Add transport mode selection in `McpSseClientProperties`
   - Use conditional loading to ensure single implementation
   - Optimize tool creation process
   - Maintain backward compatibility

1. Resource Efficiency
   - Single SSE connection per endpoint
   - Optimized tool initialization
   - Reduced memory footprint

2. Better Configuration Control
   - Explicit transport mode selection
   - Clear configuration options
   - IDE support with metadata

3. Improved Stability
   - Prevents duplicate connections
   - Streamlined tool management
   - Better error handling

1. Verify single transport initialization
2. Test tool creation and management
3. Validate configuration switching
4. Check backward compatibility

- `McpClientAutoConfiguration.java`
- `McpSseClientProperties.java`
- `SseWebFluxTransportAutoConfiguration.java`
- `SseHttpClientTransportAutoConfiguration.java`

Labels: enhancement, optimization, configuration
Signed-off-by: aliyun1024qjc <[email protected]>
@aliyun1024qjc aliyun1024qjc force-pushed the fix-auto-configurations branch from 4226795 to 1a52988 Compare March 17, 2025 10:51
@helloHuiW
Copy link

在版本 v1.0.0-M6, 我临时解决方案是在 application.properties 中添加以下内容,使其不加载 SseHttpClientTransportAutoConfiguration
spring.autoconfigure.exclude = org.springframework.ai .autoconfigure.mcp.client.SseHttpClientTransportAutoConfiguration

@aliyun1024qjc
Copy link
Author

在版本 v1.0.0-M6, 我临时解决方案是在 application.properties 中添加以下内容,使其不加载 SseHttpClientTransportAutoConfiguration spring.autoconfigure.exclude = org.springframework.ai .autoconfigure.mcp.client.SseHttpClientTransportAutoConfiguration

@SpringBootApplication(exclude = {
org.springframework.ai.autoconfigure.mcp.client.SseHttpClientTransportAutoConfiguration.class
})
多种实现方案,我修改了源代码,并提交了,也进行了测试

@tzolov
Copy link
Contributor

tzolov commented Mar 25, 2025

@aliyun1024qjc The SseHttpClientTransportAutoConfiguration and SseWebFluxTransportAutoConfiguration should be mutually exclusive based on this condition.

Even if both spring-ai-starter-mcp-client and spring-ai-starter-mcp-client-webflux are in your POM, they shouldn't be active simultaneously, avoiding any "duplication."

Could you clarify your configuration and how to reproduce this issue?

@tzolov tzolov added the MCP label Mar 25, 2025
@tzolov tzolov self-assigned this Mar 25, 2025
@aliyun1024qjc
Copy link
Author

@aliyun1024qjc根据这个条件,SseHttpClientTransportAutoConfigurationSseWebFluxTransportAutoConfiguration应该是互斥的。

即使spring-ai-starter-mcp-clientspring-ai-starter-mcp-client-webflux都在您的POM中,它们也不应该同时处于活动状态,以避免任何“重复”。"

您能否说明您的配置以及如何重现此问题?

@tzolov Thank you for your feedback! I've created a sample project to demonstrate the issue:

Repository: https://github.com/aliyun1024qjc/spring-ai-bug-sse

Issue Demonstration

The repository contains a minimal setup that clearly shows both auto-configurations are activated simultaneously in certain scenarios, leading to duplicate SSE connections and tool registrations.

Key Findings

  1. Both transport implementations (SseHttpClientTransportAutoConfiguration and SseWebFluxTransportAutoConfiguration) are activated when the WebFlux dependency is present
  2. This occurs regardless of the type setting (SYNC or ASYNC)

Evidence

Scenario 1: SYNC Mode

  • When type: SYNC is configured, the application fails to start
  • Error: Multiple tools with the same name (get_addr_date)
  • Logs show two client connections being created:
    • [ient-1-Worker-2]
    • [ient-2-Worker-0]

Scenario 2: ASYNC Mode

  • When type: ASYNC is configured, the application starts
  • But accessing /getServerList endpoint shows duplicate tools
  • Logs confirm two separate connections:
    工具名称: get_addr_date, 描述: 获取指定地点的当前时间
    工具名称: get_addr_date, 描述: 获取指定地点的当前时间
    

Root Cause

The issue is in McpClientAutoConfiguration which doesn't ensure mutually exclusive activation of transport implementations:
java
@autoConfiguration(after = {
StdioTransportAutoConfiguration.class,
SseHttpClientTransportAutoConfiguration.class,
SseWebFluxTransportAutoConfiguration.class
})

How to Reproduce

  1. Clone the repository: git clone https://github.com/aliyun1024qjc/spring-ai-bug-sse.git
  2. Open in your IDE
  3. Run the client application with different configurations:
    • Change type: SYNC in application.yml to see startup failure
    • Use type: ASYNC and access /getServerList to see duplicate tools

My PR adds explicit transport mode configuration (spring.ai.mcp.client.sse.transport-mode) to ensure only one implementation is active at a time, preventing these issues.

Would you like me to provide any additional information?

@aliyun1024qjc
Copy link
Author

How to Reproduce

  1. Clone the repository: git clone https://github.com/aliyun1024qjc/spring-ai-bug-sse.git
  2. Open in your IDE
  3. Run the client application with different configurations:
    • Change type: SYNC in application.yml to see startup failure
    • Use type: ASYNC and access /getServerList to see duplicate tools

My PR adds explicit transport mode configuration (spring.ai.mcp.client.sse.transport-mode) to ensure only one implementation is active at a time, preventing these issues.

Would you like me to provide any additional information?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants