From 21c661eda70d1291f8ca3e6470dd678cdd89e59f Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Tue, 13 May 2025 17:50:06 +0200 Subject: [PATCH] fix(mcp sampling): Disable MCP tool callbacks to prevent cyclic dependencies - Add note in documentation about preventing cyclic dependencies - Disable mcp tool callback auto-configuration: spring.ai.mcp.client.toolcallback.enabled=false Signed-off-by: Christian Tzolov --- mcp.weather.log | 13 +++++++++++++ model-context-protocol/sampling/README.md | 2 ++ .../sampling/mcp-sampling-client/README.md | 4 ++-- .../ai/mcp/samples/client/McpClientApplication.java | 7 ++----- .../src/main/resources/application.properties | 2 +- 5 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 mcp.weather.log diff --git a/mcp.weather.log b/mcp.weather.log new file mode 100644 index 0000000..23b6a5e --- /dev/null +++ b/mcp.weather.log @@ -0,0 +1,13 @@ +2025-05-13T17:39:30.096+02:00 INFO 33839 --- [main] o.s.a.m.s.server.McpServerApplication : Starting McpServerApplication using Java 17.0.12 with PID 33839 (/Users/christiantzolov/Dev/projects/spring-ai-examples/model-context-protocol/weather/manual-webflux-server/target/classes started by christiantzolov in /Users/christiantzolov/Dev/projects/spring-ai-examples) +2025-05-13T17:39:30.098+02:00 INFO 33839 --- [main] o.s.a.m.s.server.McpServerApplication : No active profile set, falling back to 1 default profile: "default" +2025-05-13T17:39:30.658+02:00 INFO 33839 --- [main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port 8080 (http) +2025-05-13T17:39:30.666+02:00 INFO 33839 --- [main] o.s.a.m.s.server.McpServerApplication : Started McpServerApplication in 0.745 seconds (process running for 0.868) +2025-05-13T17:39:44.284+02:00 INFO 33839 --- [reactor-http-nio-3] i.m.server.McpAsyncServer : Client initialize request - Protocol: 2024-11-05, Capabilities: ClientCapabilities[experimental=null, roots=null, sampling=null], Info: Implementation[name=Java SDK MCP Client, version=1.0.0] +2025-05-13T17:39:44.458+02:00 ERROR 33839 --- [boundedElastic-1] i.n.r.d.DnsServerAddressStreamProviders : Unable to load io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider, fallback to system defaults. This may result in incorrect DNS resolutions on MacOS. Check whether you have a dependency on 'io.netty:netty-resolver-dns-native-macos'. Use DEBUG level to see the full stack: java.lang.UnsatisfiedLinkError: failed to load the required native library +2025-05-13T17:39:48.448+02:00 INFO 33839 --- [SpringApplicationShutdownHook] o.s.b.w.embedded.netty.GracefulShutdown : Commencing graceful shutdown. Waiting for active requests to complete +2025-05-13T17:39:48.450+02:00 INFO 33839 --- [netty-shutdown] o.s.b.w.embedded.netty.GracefulShutdown : Graceful shutdown complete +2025-05-13T17:40:18.064+02:00 INFO 34836 --- [main] o.s.a.m.s.server.McpServerApplication : Starting McpServerApplication v0.0.1-SNAPSHOT using Java 23.0.1 with PID 34836 (/Users/christiantzolov/Dev/projects/spring-ai-examples/model-context-protocol/weather/manual-webflux-server/target/mcp-weather-server-0.0.1-SNAPSHOT.jar started by christiantzolov in /Users/christiantzolov/Dev/projects/spring-ai-examples) +2025-05-13T17:40:18.065+02:00 INFO 34836 --- [main] o.s.a.m.s.server.McpServerApplication : No active profile set, falling back to 1 default profile: "default" +2025-05-13T17:40:18.543+02:00 INFO 34836 --- [pool-2-thread-1] i.m.server.McpAsyncServer : Client initialize request - Protocol: 2024-11-05, Capabilities: ClientCapabilities[experimental=null, roots=null, sampling=null], Info: Implementation[name=Java SDK MCP Client, version=1.0.0] +2025-05-13T17:40:18.630+02:00 INFO 34836 --- [main] o.s.a.m.s.server.McpServerApplication : Started McpServerApplication in 0.772 seconds (process running for 1.003) +2025-05-13T17:40:18.761+02:00 ERROR 34836 --- [boundedElastic-1] i.n.r.d.DnsServerAddressStreamProviders : Unable to load io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider, fallback to system defaults. This may result in incorrect DNS resolutions on MacOS. Check whether you have a dependency on 'io.netty:netty-resolver-dns-native-macos'. Use DEBUG level to see the full stack: java.lang.UnsatisfiedLinkError: failed to load the required native library diff --git a/model-context-protocol/sampling/README.md b/model-context-protocol/sampling/README.md index 6da9a15..e3a4a70 100644 --- a/model-context-protocol/sampling/README.md +++ b/model-context-protocol/sampling/README.md @@ -140,6 +140,8 @@ McpSyncClientCustomizer samplingCustomizer(Map chatClients) } ``` +NOTE: To prevent cyclic dependencies you have to disable MCP tool callbacks auto-configuration: `spring.ai.mcp.client.toolcallback.enabled=false` + ## Running the Examples ### Prerequisites diff --git a/model-context-protocol/sampling/mcp-sampling-client/README.md b/model-context-protocol/sampling/mcp-sampling-client/README.md index fa018f4..7c9cdf7 100644 --- a/model-context-protocol/sampling/mcp-sampling-client/README.md +++ b/model-context-protocol/sampling/mcp-sampling-client/README.md @@ -120,8 +120,8 @@ spring.ai.mcp.client.sse.connections.server1.url=http://localhost:8080 logging.level.io.modelcontextprotocol.client=WARN logging.level.io.modelcontextprotocol.spec=WARN -# Uncomment to disable MCP tool callbacks -# spring.ai.mcp.client.toolcallback.enabled=false +#Disable MCP tool callbacks +spring.ai.mcp.client.toolcallback.enabled=false ``` ## How It Works diff --git a/model-context-protocol/sampling/mcp-sampling-client/src/main/java/org/springframework/ai/mcp/samples/client/McpClientApplication.java b/model-context-protocol/sampling/mcp-sampling-client/src/main/java/org/springframework/ai/mcp/samples/client/McpClientApplication.java index 28e010f..f15eaee 100644 --- a/model-context-protocol/sampling/mcp-sampling-client/src/main/java/org/springframework/ai/mcp/samples/client/McpClientApplication.java +++ b/model-context-protocol/sampling/mcp-sampling-client/src/main/java/org/springframework/ai/mcp/samples/client/McpClientApplication.java @@ -31,19 +31,18 @@ import org.springframework.ai.openai.OpenAiChatModel; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; @SpringBootApplication public class McpClientApplication { public static void main(String[] args) { - SpringApplication.run(McpClientApplication.class, args); + SpringApplication.run(McpClientApplication.class, args).close();; } @Bean public CommandLineRunner predefinedQuestions(OpenAiChatModel openAiChatModel, - ConfigurableApplicationContext context, List mcpClients) { + List mcpClients) { return args -> { @@ -59,8 +58,6 @@ public class McpClientApplication { System.out.println("> USER: " + userQuestion); System.out.println("> ASSISTANT: " + chatClient.prompt(userQuestion).call().content()); - - context.close(); }; } diff --git a/model-context-protocol/sampling/mcp-sampling-client/src/main/resources/application.properties b/model-context-protocol/sampling/mcp-sampling-client/src/main/resources/application.properties index 6a2ece5..f59001d 100644 --- a/model-context-protocol/sampling/mcp-sampling-client/src/main/resources/application.properties +++ b/model-context-protocol/sampling/mcp-sampling-client/src/main/resources/application.properties @@ -13,4 +13,4 @@ logging.level.io.modelcontextprotocol.client=WARN logging.level.io.modelcontextprotocol.spec=WARN -# spring.ai.mcp.client.toolcallback.enabled=false \ No newline at end of file +spring.ai.mcp.client.toolcallback.enabled=false \ No newline at end of file