Can multiple MCP servers with different endpoints be published in one service? An entry application needs to have a unified capability to implement routing forwarding for multiple MCP servers.

Comment From: flamezhang

+1

Comment From: flamezhang

@alexanderqiu have you found a solution now?

Comment From: apappascs

You can give it a try, but it will likely require some additional setup. Specifically, you'll need to ensure that each MCP server exposes its endpoints under a distinct path or port to avoid conflicts. Also, session management could become a challenge, as the MCP server maintains its own state. This might require a deeper look into the server implementation to fully understand how sessions are handled and whether multiple instances can safely coexist.

Comment From: alexanderqiu

I tried to rewrite McpServerAutoConfiguration and McpWebFluxServerAutoConfiguration and have succeeded

Comment From: sunchangji

+1

Comment From: wuque1024

@alexanderqiu Contribute your code, thank you very much!

Comment From: flamezhang

@alexanderqiu can you contribute your code? spirng-ai-alibaba nocos should be a good choice?but , I didn't use nocos。

Comment From: alexanderqiu

@alexanderqiu Contribute your code, thank you very much!

rewrite McpServerAutoConfiguration and McpWebFluxServerAutoConfiguration

Comment From: alexanderqiu

@alexanderqiu can you contribute your code? spirng-ai-alibaba nocos should be a good choice?but , I didn't use nocos。

rewrite McpServerAutoConfiguration and McpWebFluxServerAutoConfiguration

Comment From: goribun

+1

Comment From: czhcc

I want to create another McpSyncServer through such code after the project starts, but it has no effect.

public void createSyncServer()
    {
        WebMvcSseServerTransportProvider transportProvider =
                new WebMvcSseServerTransportProvider(new ObjectMapper(), "/mcp/message3","/sse3");
        mcpToolsBoot.registerSingletonBean("transportProvider3", transportProvider);
        RouterFunction<ServerResponse> f =  transportProvider.getRouterFunction();
        mcpToolsBoot.registerRouterFunction("dynamicRouterFunctionMapping3", f);
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        WebMvcSseServerTransportProvider transportProvider1 = (WebMvcSseServerTransportProvider)beanFactory.getBean("transportProvider3");

        var capabilities = McpSchema.ServerCapabilities.builder()
                .tools(true) // Tool support with list changes notifications
                .prompts(true)
                .logging() // Logging support
                .build();

        // Create the server with both tool and resource capabilities
        // Add @Tools
//        ToolCallbackProvider provider = MethodToolCallbackProvider.builder().build();
        McpSyncServer syncServer = McpServer.sync(transportProvider1)
                .serverInfo("MCP Demo Weather Server3", "3.0.3")
                .capabilities(capabilities)
//                .tools(McpToolUtils.toSyncToolSpecifications(provider.getToolCallbacks())) // Add @Tools
                .build(); // @formatter:on

        mcpToolsBoot.registerSingletonBean("mcpSyncServer3", syncServer);
    }

Comment From: tzolov

@alexanderqiu, can you please elaborate on your use case.

It looks like you are trying to implement a load balancer with server replication? This will not fly with the existing HTTP/SSE protocol version . The new streamable http protocol provides a server stateless mode that could help with simple replication scenarios. But if you use the fully fledged SSE response modes you will like have to implement a sticky session or server request redirection solution.

But maybe I'm overcomplicating your use case?

Comment From: czhcc

I want to add different tool methods to different sseendpoints. Now different sseendpoints can be generated through bean annotations. But I want to implement it through code.

Comment From: mklp

solon-ai support!!!

Comment From: handicraftsman

Similar a lot to https://github.com/spring-projects/spring-ai/issues/2912 I created back in April.

Comment From: alexanderqiu

@tzolov Thank you for your reply.

It's the same as this problem, but I've already solved it

I want to add different tool methods to different sseendpoints. Now different sseendpoints can be generated through bean annotations. But I want to implement it through code.

Comment From: lihuagang03

I tried to rewrite McpServerAutoConfiguration and McpWebFluxServerAutoConfiguration and have succeeded

Yes, I rewrite McpServerStreamableHttpWebFluxAutoConfiguration success too, that class is similar to McpWebFluxServerAutoConfiguration. @alexanderqiu

Create multiple McpAsyncServer, McpServerTransportProvider, RouterFunction<?> @Bean instances, such as Map<appName, McpAsyncServer>, exclude to McpWebFluxServerAutoConfiguration. CC @tzolov @apappascs

In the same application process, multiple endpoint paths are mapped to different MCP-Servers. Such as, /mcp /mcp/appA -> toolsA (McpAsyncServer <-> MCP-Tools) /mcp/appB -> toolB (McpAsyncServer <-> MCP-Tools)

Comment From: lihuagang03

You can give it a try, but it will likely require some additional setup. Specifically, you'll need to ensure that each MCP server exposes its endpoints under a distinct path or port to avoid conflicts. Also, session management could become a challenge, as the MCP server maintains its own state. This might require a deeper look into the server implementation to fully understand how sessions are handled and whether multiple instances can safely coexist.

Spring-AI have done these features, we can build on it. There is a simple solution, currently Spring-AI only starts a McpServer and TransportProvider. In fact, multiple MCP-Servers and Transports can be started. @apappascs CC @tzolov

Comment From: lihuagang03

@alexanderqiu, can you please elaborate on your use case.

It looks like you are trying to implement a load balancer with server replication? This will not fly with the existing HTTP/SSE protocol version . The new streamable http protocol provides a server stateless mode that could help with simple replication scenarios. But if you use the fully fledged SSE response modes you will like have to implement a sticky session or server request redirection solution.

But maybe I'm overcomplicating your use case?

RouterFunction<?> can as a load balancer. I solve this issue build on Streamable-HTTP. MCP tools can be loaded and bind dynamically. @tzolov

Comment From: lihuagang03

I want to create another McpSyncServer through such code after the project starts, but it has no effect.

public void createSyncServer() { WebMvcSseServerTransportProvider transportProvider = new WebMvcSseServerTransportProvider(new ObjectMapper(), "/mcp/message3","/sse3"); mcpToolsBoot.registerSingletonBean("transportProvider3", transportProvider); RouterFunction f = transportProvider.getRouterFunction(); mcpToolsBoot.registerRouterFunction("dynamicRouterFunctionMapping3", f); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); WebMvcSseServerTransportProvider transportProvider1 = (WebMvcSseServerTransportProvider)beanFactory.getBean("transportProvider3");

    var capabilities = McpSchema.ServerCapabilities.builder()
            .tools(true) // Tool support with list changes notifications
            .prompts(true)
            .logging() // Logging support
            .build();

    // Create the server with both tool and resource capabilities
    // Add @Tools

// ToolCallbackProvider provider = MethodToolCallbackProvider.builder().build(); McpSyncServer syncServer = McpServer.sync(transportProvider1) .serverInfo("MCP Demo Weather Server3", "3.0.3") .capabilities(capabilities) // .tools(McpToolUtils.toSyncToolSpecifications(provider.getToolCallbacks())) // Add @Tools .build(); // @formatter:on

    mcpToolsBoot.registerSingletonBean("mcpSyncServer3", syncServer);
}

Create multiple McpAsyncServer, McpServerTransportProvider, RouterFunction<?> @Bean instances, such as Map<appName, McpServerTransportProvider/McpServer>, exclude to McpWebFluxServerAutoConfiguration. @czhcc

Comment From: lihuagang03

@alexanderqiu can you contribute your code? spirng-ai-alibaba nocos should be a good choice?but , I didn't use nocos。

I rewrite McpServerStreamableHttpWebFluxAutoConfiguration on Spring-AI or Spring-AI-Alibaba success. @flamezhang

Comment From: lihuagang03

Can multiple MCP servers with different endpoints be published in one service? An entry application needs to have a unified capability to implement routing forwarding for multiple MCP servers.

Yes, you can use RouterFunction<?> webFluxStreamableServerRouterFunction() on McpServerStreamableHttpWebFluxAutoConfiguration as load balance router. @alexanderqiu