You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix MCP client timing and lazy tool callback resolution
Resolves critical timing issues in MCP client initialization and
tool callback resolution that prevented proper registration of
MCP-annotated beans.
- MCP clients created after all singleton beans initialized
- Implement SmartInitializingSingleton for deferred client creation
via McpSyncClientInitializer and McpAsyncClientInitializer
- Tool callbacks resolved at execution instead configuration
- Store ToolCallbackProvider instances in ChatClient and resolve
lazily at execution time (call/stream)
- Filter MCP providers from StaticToolCallbackResolver
- Inconsistent list reference handling
- Add mcpClientsReference() methods for proper reference sharing
Breaking Changes:
- MCP providers no longer in static resolver (transparent)
- Client creation timing changed (transparent)
Fixes: #4670, #4618
Signed-off-by: Christian Tzolov <[email protected]>
Copy file name to clipboardExpand all lines: auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/main/java/org/springframework/ai/mcp/client/common/autoconfigure/McpClientAutoConfiguration.java
Copy file name to clipboardExpand all lines: auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/main/java/org/springframework/ai/mcp/client/common/autoconfigure/McpToolCallbackAutoConfiguration.java
+35-13Lines changed: 35 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -60,45 +60,67 @@ public McpToolNamePrefixGenerator defaultMcpToolNamePrefixGenerator() {
60
60
* <p>
61
61
* These callbacks enable integration with Spring AI's tool execution framework,
62
62
* allowing MCP tools to be used as part of AI interactions.
63
+
*
64
+
* <p>
65
+
* IMPORTANT: This method receives the same list reference that is populated by
66
+
* {@link McpClientAutoConfiguration.McpSyncClientInitializer} in its
67
+
* {@code afterSingletonsInstantiated()} method. This ensures that when
68
+
* {@code getToolCallbacks()} is called, even if it's called before full
69
+
* initialization completes, it will eventually see the populated list.
63
70
* @param syncClientsToolFilter list of {@link McpToolFilter}s for the sync client to
64
71
* filter the discovered tools
65
-
* @param syncMcpClients provider of MCP sync clients
72
+
* @param syncMcpClients the MCP sync clients list (same reference as returned by
73
+
* mcpSyncClients() bean method)
66
74
* @param mcpToolNamePrefixGenerator the tool name prefix generator
67
75
* @return list of tool callbacks for MCP integration
68
76
*/
69
77
@Bean
70
78
@ConditionalOnProperty(prefix = McpClientCommonProperties.CONFIG_PREFIX, name = "type", havingValue = "SYNC",
Copy file name to clipboardExpand all lines: auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/main/java/org/springframework/ai/mcp/client/common/autoconfigure/annotations/McpClientSpecificationFactoryAutoConfiguration.java
Copy file name to clipboardExpand all lines: auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/McpClientAutoConfigurationIT.java
Copy file name to clipboardExpand all lines: auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/annotations/McpClientListChangedAnnotationsScanningIT.java
0 commit comments