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
AsyncMcpToolCallback
instances 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();
Example usage with multiple clients:
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 Summary
ConstructorsConstructorDescriptionAsyncMcpToolCallbackProvider
(io.modelcontextprotocol.client.McpAsyncClient... mcpClients) AsyncMcpToolCallbackProvider
(List<io.modelcontextprotocol.client.McpAsyncClient> mcpClients) Creates a newAsyncMcpToolCallbackProvider
instance with a list of MCP clients. -
Method Summary
Modifier 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
-
AsyncMcpToolCallbackProvider
Creates a newAsyncMcpToolCallbackProvider
instance 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
-
AsyncMcpToolCallbackProvider
public AsyncMcpToolCallbackProvider(io.modelcontextprotocol.client.McpAsyncClient... mcpClients)
-
-
Method Details
-
getToolCallbacks
Discovers and returns all available tools from the configured MCP servers.This method:
- Retrieves the list of tools from each MCP server asynchronously
- Creates a
AsyncMcpToolCallback
for 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:
getToolCallbacks
in interfaceToolCallbackProvider
- Returns:
- an array of tool callbacks, one for each discovered tool
- Throws:
IllegalStateException
- if duplicate tool names are found
-
asyncToolCallbacks
public 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
-