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 <christian.tzolov@broadcom.com>
This commit is contained in:
Christian Tzolov
2025-05-13 17:50:06 +02:00
parent ffd8def9eb
commit 21c661eda7
5 changed files with 20 additions and 8 deletions

13
mcp.weather.log Normal file
View File

@@ -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

View File

@@ -140,6 +140,8 @@ McpSyncClientCustomizer samplingCustomizer(Map<String, ChatClient> 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

View File

@@ -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

View File

@@ -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<McpSyncClient> mcpClients) {
List<McpSyncClient> 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();
};
}

View File

@@ -13,4 +13,4 @@ logging.level.io.modelcontextprotocol.client=WARN
logging.level.io.modelcontextprotocol.spec=WARN
# spring.ai.mcp.client.toolcallback.enabled=false
spring.ai.mcp.client.toolcallback.enabled=false