Class AsyncMcpToolCallbackProvider

java.lang.Object
org.springframework.ai.mcp.AsyncMcpToolCallbackProvider
All Implemented Interfaces:
ToolCallbackProvider

public class AsyncMcpToolCallbackProvider extends Object implements 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:
  • Constructor Summary

    Constructors
    Constructor
    Description
    AsyncMcpToolCallbackProvider(io.modelcontextprotocol.client.McpAsyncClient... mcpClients)
    Creates a new AsyncMcpToolCallbackProvider instance 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 new AsyncMcpToolCallbackProvider instance 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 new AsyncMcpToolCallbackProvider instance with a list of MCP clients.
    AsyncMcpToolCallbackProvider(List<io.modelcontextprotocol.client.McpAsyncClient> mcpClients)
    Creates a new AsyncMcpToolCallbackProvider instance with a list of MCP clients.
  • Method Summary

    Modifier and Type
    Method
    Description
    static 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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • AsyncMcpToolCallbackProvider

      public AsyncMcpToolCallbackProvider(BiPredicate<io.modelcontextprotocol.client.McpAsyncClient,io.modelcontextprotocol.spec.McpSchema.Tool> toolFilter, List<io.modelcontextprotocol.client.McpAsyncClient> mcpClients)
      Creates a new AsyncMcpToolCallbackProvider instance 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
    • AsyncMcpToolCallbackProvider

      public AsyncMcpToolCallbackProvider(List<io.modelcontextprotocol.client.McpAsyncClient> mcpClients)
      Creates a new AsyncMcpToolCallbackProvider 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(BiPredicate<io.modelcontextprotocol.client.McpAsyncClient,io.modelcontextprotocol.spec.McpSchema.Tool> toolFilter, io.modelcontextprotocol.client.McpAsyncClient... mcpClients)
      Creates a new AsyncMcpToolCallbackProvider instance 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
    • AsyncMcpToolCallbackProvider

      public AsyncMcpToolCallbackProvider(io.modelcontextprotocol.client.McpAsyncClient... mcpClients)
      Creates a new AsyncMcpToolCallbackProvider instance with one or more MCP clients.
      Parameters:
      mcpClients - the MCP clients to use for discovering tools
  • Method Details

    • getToolCallbacks

      public ToolCallback[] getToolCallbacks()
      Discovers and returns all available tools from the configured MCP servers.

      This method:

      1. Retrieves the list of tools from each MCP server asynchronously
      2. Creates a AsyncMcpToolCallback for each discovered tool
      3. 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 interface ToolCallbackProvider
      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:

      1. Takes a list of MCP clients as input
      2. Creates a provider instance to manage all clients
      3. Retrieves tools from all clients asynchronously
      4. Combines them into a single reactive stream
      5. 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