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

Multiple SSE connections issue and SYNC mode startup failure in McpClientAutoConfiguration #2486

Open
aliyun1024qjc opened this issue Mar 17, 2025 · 3 comments

Comments

@aliyun1024qjc
Copy link

In the current implementation of McpClientAutoConfiguration, there are two critical issues:

  1. When using type: ASYNC, both SSE implementations (WebFlux and HttpClient) attempt to establish connections to the same server endpoint simultaneously. This occurs because the auto-configuration allows both transports to be registered:

java
@autoConfiguration(after = {
StdioTransportAutoConfiguration.class,
SseHttpClientTransportAutoConfiguration.class,
SseWebFluxTransportAutoConfiguration.class
})

This results in:

  • Duplicate SSE connections to the same endpoint
  • Unnecessary resource consumption
  • Potential message duplication
  1. When using type: SYNC, the application fails to start. This appears to be related to the order of auto-configuration initialization specified in the @AutoConfiguration(after = {...}) annotation.

Example configuration where these issues occur:

yaml
spring:
ai:
mcp:
client:
type: ASYNC # or SYNC
server:
endpoint: "http://localhost:8080/"

Questions:

  1. Why are both SSE implementations allowed to register and connect simultaneously?
  2. Could we implement a mechanism to ensure only one SSE implementation is active at a time?
  3. What is the recommended approach for handling these transport implementations to avoid duplicate connections?
  4. Why does SYNC mode fail to start, and what is the proper way to handle the auto-configuration order?

Environment:

  • Spring AI version: [1.0.0 M6]
  • Spring Boot version: [3.4.3]
  • Java version: [JDK 17]
@aliyun1024qjc
Copy link
Author

I have closed the duplicate questions. This one is kept and I have already written the code and am ready to submit it.

@aliyun1024qjc
Copy link
Author

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

Proposed 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

    • Add transport mode selection in McpSseClientProperties
    • Use conditional loading to ensure single implementation
    • Optimize tool creation process
    • Maintain 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 Requirements

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

Related Files

  • McpClientAutoConfiguration.java
  • McpSseClientProperties.java
  • SseWebFluxTransportAutoConfiguration.java
  • SseHttpClientTransportAutoConfiguration.java

Labels: enhancement, optimization, configuration

@aliyun1024qjc
Copy link
Author

aliyun1024qjc commented Mar 17, 2025

I apologize for the previous incomplete submission. I have created a new PR (#2493) with a more comprehensive solution:

Improvements in the New PR

  1. More Complete Solution:

    • Added transport mode configuration (WEBFLUX/HTTP_CLIENT)
    • Optimized connection management
    • Provided explicit configuration options
  2. Better Implementation:

    spring.ai.mcp.client.sse.transport-mode=WEBFLUX  # Default
    # or
    spring.ai.mcp.client.sse.transport-mode=HTTP_CLIENT
  3. Comprehensive Testing:

    • Verified single transport initialization
    • Tested configuration switching
    • Ensured backward compatibility

I suggest closing this issue (#2486) and moving the discussion to the new PR: #2493

#2493

Thank you for your understanding and support.

Signed-off-by: aliyun1024qjc [email protected]

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

No branches or pull requests

1 participant