Class McpClientAutoConfiguration
This configuration class sets up the necessary beans for MCP client functionality, including both synchronous and asynchronous clients along with their respective tool callbacks. It is automatically enabled when the required classes are present on the classpath and can be explicitly disabled through properties.
Configuration Properties:
spring.ai.mcp.client.enabled
- Enable/disable MCP client support (default: true)spring.ai.mcp.client.type
- Client type: SYNC or ASYNC (default: SYNC)spring.ai.mcp.client.name
- Client implementation namespring.ai.mcp.client.version
- Client implementation versionspring.ai.mcp.client.request-timeout
- Request timeout durationspring.ai.mcp.client.initialized
- Whether to initialize clients on creation
The configuration is activated after the transport-specific auto-configurations (Stdio, SSE HTTP, and SSE WebFlux) to ensure proper initialization order. At least one transport must be available for the clients to be created.
Key features:
- Synchronous and Asynchronous Client Support:
- Creates and configures MCP clients based on available transports
- Supports both blocking (sync) and non-blocking (async) operations
- Automatic client initialization if enabled
- Integration Support:
- Sets up tool callbacks for Spring AI integration
- Supports multiple named transports
- Proper lifecycle management with automatic cleanup
- Customization Options:
- Extensible through
McpSyncClientCustomizer
andMcpAsyncClientCustomizer
- Configurable timeouts and client information
- Support for custom transport implementations
- Extensible through
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final record
static final record
Record class that implementsAutoCloseable
to ensure proper cleanup of MCP clients. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionasyncToolCallbacks
(org.springframework.beans.factory.ObjectProvider<List<io.modelcontextprotocol.client.McpAsyncClient>> mcpClientsProvider) asyncToolCallbacksDeprecated
(org.springframework.beans.factory.ObjectProvider<List<io.modelcontextprotocol.client.McpAsyncClient>> mcpClientsProvider) Deprecated.makeAsynClientsClosable
(List<io.modelcontextprotocol.client.McpAsyncClient> clients) makeSyncClientsClosable
(List<io.modelcontextprotocol.client.McpSyncClient> clients) Creates a closeable wrapper for MCP sync clients to ensure proper resource cleanup.List<io.modelcontextprotocol.client.McpAsyncClient>
mcpAsyncClients
(McpAsyncClientConfigurer mcpSyncClientConfigurer, McpClientCommonProperties commonProperties, org.springframework.beans.factory.ObjectProvider<List<NamedClientMcpTransport>> transportsProvider) List<io.modelcontextprotocol.client.McpSyncClient>
mcpSyncClients
(McpSyncClientConfigurer mcpSyncClientConfigurer, McpClientCommonProperties commonProperties, org.springframework.beans.factory.ObjectProvider<List<NamedClientMcpTransport>> transportsProvider) Creates a list ofMcpSyncClient
instances based on the available transports.toolCallbacks
(org.springframework.beans.factory.ObjectProvider<List<io.modelcontextprotocol.client.McpSyncClient>> mcpClientsProvider) Creates tool callbacks for all configured MCP clients.toolCallbacksDeprecated
(org.springframework.beans.factory.ObjectProvider<List<io.modelcontextprotocol.client.McpSyncClient>> mcpClientsProvider) Deprecated.replaced bytoolCallbacks(ObjectProvider)
that returns aToolCallbackProvider
instead of a list ofToolCallback
-
Constructor Details
-
McpClientAutoConfiguration
public McpClientAutoConfiguration()
-
-
Method Details
-
mcpSyncClients
@Bean @ConditionalOnProperty(prefix="spring.ai.mcp.client", name="type", havingValue="SYNC", matchIfMissing=true) public List<io.modelcontextprotocol.client.McpSyncClient> mcpSyncClients(McpSyncClientConfigurer mcpSyncClientConfigurer, McpClientCommonProperties commonProperties, org.springframework.beans.factory.ObjectProvider<List<NamedClientMcpTransport>> transportsProvider) Creates a list ofMcpSyncClient
instances based on the available transports.Each client is configured with:
- Client information (name and version) from common properties
- Request timeout settings
- Custom configurations through
McpSyncClientConfigurer
If initialization is enabled in properties, the clients are automatically initialized.
- Parameters:
mcpSyncClientConfigurer
- the configurer for customizing client creationcommonProperties
- common MCP client propertiestransportsProvider
- provider of named MCP transports- Returns:
- list of configured MCP sync clients
-
toolCallbacks
@Bean @ConditionalOnProperty(prefix="spring.ai.mcp.client", name="type", havingValue="SYNC", matchIfMissing=true) public ToolCallbackProvider toolCallbacks(org.springframework.beans.factory.ObjectProvider<List<io.modelcontextprotocol.client.McpSyncClient>> mcpClientsProvider) Creates tool callbacks for all configured MCP clients.These callbacks enable integration with Spring AI's tool execution framework, allowing MCP tools to be used as part of AI interactions.
- Parameters:
mcpClientsProvider
- provider of MCP sync clients- Returns:
- list of tool callbacks for MCP integration
-
toolCallbacksDeprecated
@Deprecated @Bean @ConditionalOnProperty(prefix="spring.ai.mcp.client", name="type", havingValue="SYNC", matchIfMissing=true) public List<ToolCallback> toolCallbacksDeprecated(org.springframework.beans.factory.ObjectProvider<List<io.modelcontextprotocol.client.McpSyncClient>> mcpClientsProvider) Deprecated.replaced bytoolCallbacks(ObjectProvider)
that returns aToolCallbackProvider
instead of a list ofToolCallback
-
makeSyncClientsClosable
@Bean @ConditionalOnProperty(prefix="spring.ai.mcp.client", name="type", havingValue="SYNC", matchIfMissing=true) public McpClientAutoConfiguration.CloseableMcpSyncClients makeSyncClientsClosable(List<io.modelcontextprotocol.client.McpSyncClient> clients) Creates a closeable wrapper for MCP sync clients to ensure proper resource cleanup.- Parameters:
clients
- the list of MCP sync clients to manage- Returns:
- a closeable wrapper for the clients
-
mcpAsyncClients
@Bean @ConditionalOnProperty(prefix="spring.ai.mcp.client", name="type", havingValue="ASYNC") public List<io.modelcontextprotocol.client.McpAsyncClient> mcpAsyncClients(McpAsyncClientConfigurer mcpSyncClientConfigurer, McpClientCommonProperties commonProperties, org.springframework.beans.factory.ObjectProvider<List<NamedClientMcpTransport>> transportsProvider) -
asyncToolCallbacksDeprecated
@Deprecated @Bean @ConditionalOnProperty(prefix="spring.ai.mcp.client", name="type", havingValue="ASYNC") public List<ToolCallback> asyncToolCallbacksDeprecated(org.springframework.beans.factory.ObjectProvider<List<io.modelcontextprotocol.client.McpAsyncClient>> mcpClientsProvider) Deprecated.replaced byasyncToolCallbacks(ObjectProvider)
that returns aToolCallbackProvider
instead of a list ofToolCallback
-
asyncToolCallbacks
@Bean @ConditionalOnProperty(prefix="spring.ai.mcp.client", name="type", havingValue="ASYNC") public ToolCallbackProvider asyncToolCallbacks(org.springframework.beans.factory.ObjectProvider<List<io.modelcontextprotocol.client.McpAsyncClient>> mcpClientsProvider) -
makeAsynClientsClosable
@Bean @ConditionalOnProperty(prefix="spring.ai.mcp.client", name="type", havingValue="ASYNC") public McpClientAutoConfiguration.CloseableMcpAsyncClients makeAsynClientsClosable(List<io.modelcontextprotocol.client.McpAsyncClient> clients)
-
asyncToolCallbacks(ObjectProvider)
that returns aToolCallbackProvider
instead of a list ofToolCallback