Class McpClientAutoConfiguration

java.lang.Object
org.springframework.ai.autoconfigure.mcp.client.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 name
  • spring.ai.mcp.client.version - Client implementation version
  • spring.ai.mcp.client.request-timeout - Request timeout duration
  • spring.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:
See Also:
  • 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 of McpSyncClient 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 creation
      commonProperties - common MCP client properties
      transportsProvider - 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 by toolCallbacks(ObjectProvider) that returns a ToolCallbackProvider instead of a list of ToolCallback
    • 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 by asyncToolCallbacks(ObjectProvider) that returns a ToolCallbackProvider instead of a list of ToolCallback
    • 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)