Class McpClientAutoConfiguration
java.lang.Object
org.springframework.ai.mcp.client.autoconfigure.McpClientAutoConfiguration
@AutoConfiguration(after={StdioTransportAutoConfiguration.class,SseHttpClientTransportAutoConfiguration.class,SseWebFluxTransportAutoConfiguration.class})
@ConditionalOnClass(io.modelcontextprotocol.spec.McpSchema.class)
@EnableConfigurationProperties(McpClientCommonProperties.class)
@ConditionalOnProperty(prefix="spring.ai.mcp.client",
name="enabled",
havingValue="true",
matchIfMissing=true)
public class McpClientAutoConfiguration
extends Object
Auto-configuration for Model Context Protocol (MCP) client support.
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 TypeMethodDescriptionmakeAsyncClientsClosable
(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 mcpAsyncClientConfigurer, 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.
-
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
-
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 mcpAsyncClientConfigurer, McpClientCommonProperties commonProperties, org.springframework.beans.factory.ObjectProvider<List<NamedClientMcpTransport>> transportsProvider) -
makeAsyncClientsClosable
@Bean @ConditionalOnProperty(prefix="spring.ai.mcp.client", name="type", havingValue="ASYNC") public McpClientAutoConfiguration.CloseableMcpAsyncClients makeAsyncClientsClosable(List<io.modelcontextprotocol.client.McpAsyncClient> clients)
-