Package org.springframework.ai.mcp
Class AsyncMcpToolCallbackProvider
java.lang.Object
org.springframework.ai.mcp.AsyncMcpToolCallbackProvider
- All Implemented Interfaces:
- ToolCallbackProvider
Implementation of 
ToolCallbackProvider that discovers and provides MCP tools
 asynchronously from one or more MCP servers.
 This class acts as a tool provider for Spring AI, automatically discovering tools from multiple MCP servers and making them available as Spring AI tools. It:
- Connects to MCP servers through async clients
- Lists and retrieves available tools from each server asynchronously
- Creates AsyncMcpToolCallbackinstances for each discovered tool
- Validates tool names to prevent duplicates across all servers
Example usage with a single client:
 McpAsyncClient mcpClient = // obtain MCP client
 ToolCallbackProvider provider = new AsyncMcpToolCallbackProvider(mcpClient);
 // Get all available tools
 ToolCallback[] tools = provider.getToolCallbacks();
 
 List<McpAsyncClient> mcpClients = // obtain multiple MCP clients
 ToolCallbackProvider provider = new AsyncMcpToolCallbackProvider(mcpClients);
 // Get tools from all clients
 ToolCallback[] tools = provider.getToolCallbacks();
 // Or use the reactive API
 Flux<ToolCallback> toolsFlux = AsyncMcpToolCallbackProvider.asyncToolCallbacks(mcpClients);
 - Since:
- 1.0.0
- Author:
- Christian Tzolov
- See Also:
- 
- ToolCallbackProvider
- AsyncMcpToolCallback
- McpAsyncClient
 
- 
Constructor SummaryConstructorsConstructorDescriptionAsyncMcpToolCallbackProvider(io.modelcontextprotocol.client.McpAsyncClient... mcpClients) Creates a newAsyncMcpToolCallbackProviderinstance with one or more MCP clients.AsyncMcpToolCallbackProvider(BiPredicate<io.modelcontextprotocol.client.McpAsyncClient, io.modelcontextprotocol.spec.McpSchema.Tool> toolFilter, io.modelcontextprotocol.client.McpAsyncClient... mcpClients) Creates a newAsyncMcpToolCallbackProviderinstance with one or more MCP clients.AsyncMcpToolCallbackProvider(BiPredicate<io.modelcontextprotocol.client.McpAsyncClient, io.modelcontextprotocol.spec.McpSchema.Tool> toolFilter, List<io.modelcontextprotocol.client.McpAsyncClient> mcpClients) Creates a newAsyncMcpToolCallbackProviderinstance with a list of MCP clients.AsyncMcpToolCallbackProvider(List<io.modelcontextprotocol.client.McpAsyncClient> mcpClients) Creates a newAsyncMcpToolCallbackProviderinstance with a list of MCP clients.
- 
Method SummaryModifier and TypeMethodDescriptionstatic reactor.core.publisher.Flux<ToolCallback>asyncToolCallbacks(List<io.modelcontextprotocol.client.McpAsyncClient> mcpClients) Creates a reactive stream of tool callbacks from multiple MCP clients.Discovers and returns all available tools from the configured MCP servers.
- 
Constructor Details- 
AsyncMcpToolCallbackProviderpublic AsyncMcpToolCallbackProvider(BiPredicate<io.modelcontextprotocol.client.McpAsyncClient, io.modelcontextprotocol.spec.McpSchema.Tool> toolFilter, List<io.modelcontextprotocol.client.McpAsyncClient> mcpClients) Creates a newAsyncMcpToolCallbackProviderinstance with a list of MCP clients.- Parameters:
- mcpClients- the list of MCP clients to use for discovering tools
- toolFilter- a filter to apply to each discovered tool
 
- 
AsyncMcpToolCallbackProviderCreates a newAsyncMcpToolCallbackProviderinstance with a list of MCP clients.- Parameters:
- mcpClients- the list of MCP clients to use for discovering tools. Each client typically connects to a different MCP server, allowing tool discovery from multiple sources.
- Throws:
- IllegalArgumentException- if mcpClients is null
 
- 
AsyncMcpToolCallbackProviderpublic AsyncMcpToolCallbackProvider(BiPredicate<io.modelcontextprotocol.client.McpAsyncClient, io.modelcontextprotocol.spec.McpSchema.Tool> toolFilter, io.modelcontextprotocol.client.McpAsyncClient... mcpClients) Creates a newAsyncMcpToolCallbackProviderinstance with one or more MCP clients.- Parameters:
- mcpClients- the MCP clients to use for discovering tools
- toolFilter- a filter to apply to each discovered tool
 
- 
AsyncMcpToolCallbackProviderpublic AsyncMcpToolCallbackProvider(io.modelcontextprotocol.client.McpAsyncClient... mcpClients) Creates a newAsyncMcpToolCallbackProviderinstance with one or more MCP clients.- Parameters:
- mcpClients- the MCP clients to use for discovering tools
 
 
- 
- 
Method Details- 
getToolCallbacksDiscovers and returns all available tools from the configured MCP servers.This method: - Retrieves the list of tools from each MCP server asynchronously
- Creates a AsyncMcpToolCallbackfor each discovered tool
- Validates that there are no duplicate tool names across all servers
 Note: While the underlying tool discovery is asynchronous, this method blocks until all tools are discovered from all servers. - Specified by:
- getToolCallbacksin interface- ToolCallbackProvider
- Returns:
- an array of tool callbacks, one for each discovered tool
- Throws:
- IllegalStateException- if duplicate tool names are found
 
- 
asyncToolCallbackspublic static reactor.core.publisher.Flux<ToolCallback> asyncToolCallbacks(List<io.modelcontextprotocol.client.McpAsyncClient> mcpClients) Creates a reactive stream of tool callbacks from multiple MCP clients.This utility method provides a reactive way to work with tool callbacks from multiple MCP clients in a single operation. It: - Takes a list of MCP clients as input
- Creates a provider instance to manage all clients
- Retrieves tools from all clients asynchronously
- Combines them into a single reactive stream
- Ensures there are no naming conflicts between tools from different clients
 Unlike getToolCallbacks(), this method provides a fully reactive way to work with tool callbacks, making it suitable for non-blocking applications. Any errors during tool discovery will be propagated through the returned Flux.- Parameters:
- mcpClients- the list of MCP clients to create callbacks from
- Returns:
- a Flux of tool callbacks from all provided clients
 
 
-