I am using Spring AI WebMvc MCP Server to build my MCP server, but I now want to start multiple MCP servers within a single project. However, my attempts have failed. Please provide a solution.
Expected Behavior I now want to start multiple MCP servers within a single project.
Current Behavior
The current situation is that only one MCP server can be started, and attempting to start multiple servers results in an error.
Context
version springboot 3.5.0 spring-ai-mcp 1.0.0
package org.example.mcpserverdemo;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.modelcontextprotocol.server.McpServer;
import io.modelcontextprotocol.server.McpSyncServer;
import io.modelcontextprotocol.server.transport.WebMvcSseServerTransportProvider;
import io.modelcontextprotocol.spec.McpSchema;
import org.springframework.ai.mcp.McpToolUtils;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.ServerResponse;
/**
* <p>
*
* </p>
*
* @author kunqi.fc
* @since 2025/6/12
**/
@Configuration
public class McpServerConfig {
@Bean("t1")
public WebMvcSseServerTransportProvider webMvcSseServerTransportProvider1() {
return new WebMvcSseServerTransportProvider(new ObjectMapper(), "/mcp/message","/sse");
}
@Bean
public RouterFunction<ServerResponse> mvcMcpRouterFunction1(@Qualifier("t1") WebMvcSseServerTransportProvider transportProvider) {
return transportProvider.getRouterFunction();
}
@Bean("t2")
public WebMvcSseServerTransportProvider webMvcSseServerTransportProvider2() {
return new WebMvcSseServerTransportProvider(new ObjectMapper(), "/mcp/message","/sse2");
}
@Bean
public RouterFunction<ServerResponse> mvcMcpRouterFunction2(@Qualifier("t2")WebMvcSseServerTransportProvider transportProvider) {
return transportProvider.getRouterFunction();
}
@Bean("weather-mcp-server_instance1")
public McpSyncServer mcpServer(ToolCallbackProvider provider,@Qualifier("t1") WebMvcSseServerTransportProvider transportProvider) { // @formatter:off
// Configure server capabilities with resource support
var capabilities = McpSchema.ServerCapabilities.builder()
.tools(true) // Tool support with list changes notifications
.logging() // Logging support
.build();
// Create the server with both tool and resource capabilities
// Add @Tools
return McpServer.sync(transportProvider)
.serverInfo("MCP Demo Weather Server1", "1.0.1")
.capabilities(capabilities)
.tools(McpToolUtils.toSyncToolSpecifications(provider.getToolCallbacks())) // Add @Tools
.build(); // @formatter:on
} // @formatter:on
@Bean(name = "weather-mcp-server_instance2")
public McpSyncServer mcpServer2(ToolCallbackProvider provider,@Qualifier("t2") WebMvcSseServerTransportProvider transportProvider) { // @formatter:off
// Configure server capabilities with resource support
var capabilities = McpSchema.ServerCapabilities.builder()
.tools(true) // Tool support with list changes notifications
.logging() // Logging support
.build();
// Create the server with both tool and resource capabilities
// Add @Tools
return McpServer.sync(transportProvider)
.serverInfo("MCP Demo Weather Server2", "1.0.2")
.capabilities(capabilities)
.tools(McpToolUtils.toSyncToolSpecifications(provider.getToolCallbacks())) // Add @Tools
.build(); // @formatter:on
} // @formatter:on
@Bean
public ToolCallbackProvider weatherTools(WeatherService weatherService) {
return MethodToolCallbackProvider.builder().toolObjects(weatherService).build();
}
}
errormssage: 2025-06-12T11:14:54.713+08:00 INFO 57942 --- [mcp_server_demo] [ main] .s.b.a.l.ConditionEvaluationReportLogger :
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2025-06-12T11:14:54.720+08:00 ERROR 57942 --- [mcp_server_demo] [ main] o.s.b.d.LoggingFailureAnalysisReporter :
APPLICATION FAILED TO START
Description:
Parameter 0 of method mcpSyncServer in org.springframework.ai.mcp.server.autoconfigure.McpServerAutoConfiguration required a single bean, but 2 were found: - t1: defined by method 'webMvcSseServerTransportProvider1' in class path resource [org/example/mcpserverdemo/McpServerConfig.class] - t2: defined by method 'webMvcSseServerTransportProvider2' in class path resource [org/example/mcpserverdemo/McpServerConfig.class]
This may be due to missing parameter name information
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
Ensure that your compiler is configured to use the '-parameters' flag. You may need to update both your build tool settings as well as your IDE. (See https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-6.1-Release-Notes#parameter-name-retention)
Comment From: Susuper2019
I have the same requirement. After I configured @SpringBootApplication(exclude = {McpServerAutoConfiguration.class}), both servers can start successfully, but they are unable to load their respective tools.