Class AsyncMcpToolCallback

java.lang.Object
org.springframework.ai.mcp.AsyncMcpToolCallback
All Implemented Interfaces:
FunctionCallback, ToolCallback

public class AsyncMcpToolCallback extends Object implements ToolCallback
Implementation of ToolCallback that adapts MCP tools to Spring AI's tool interface with asynchronous execution support.

This class acts as a bridge between the Model Context Protocol (MCP) and Spring AI's tool system, allowing MCP tools to be used seamlessly within Spring AI applications. It:

  • Converts MCP tool definitions to Spring AI tool definitions
  • Handles the asynchronous execution of tool calls through the MCP client
  • Manages JSON serialization/deserialization of tool inputs and outputs

Example usage:


 McpAsyncClient mcpClient = // obtain MCP client
 Tool mcpTool = // obtain MCP tool definition
 ToolCallback callback = new AsyncMcpToolCallback(mcpClient, mcpTool);

 // Use the tool through Spring AI's interfaces
 ToolDefinition definition = callback.getToolDefinition();
 String result = callback.call("{\"param\": \"value\"}");
 
Author:
Christian Tzolov
See Also:
  • Constructor Details

    • AsyncMcpToolCallback

      public AsyncMcpToolCallback(io.modelcontextprotocol.client.McpAsyncClient mcpClient, io.modelcontextprotocol.spec.McpSchema.Tool tool)
      Creates a new AsyncMcpToolCallback instance.
      Parameters:
      mcpClient - the MCP client to use for tool execution
      tool - the MCP tool definition to adapt
  • Method Details

    • getToolDefinition

      public ToolDefinition getToolDefinition()
      Returns a Spring AI tool definition adapted from the MCP tool.

      The tool definition includes:

      • The tool's name from the MCP definition
      • The tool's description from the MCP definition
      • The input schema converted to JSON format
      Specified by:
      getToolDefinition in interface ToolCallback
      Returns:
      the Spring AI tool definition
    • call

      public String call(String functionInput)
      Executes the tool with the provided input asynchronously.

      This method:

      1. Converts the JSON input string to a map of arguments
      2. Calls the tool through the MCP client asynchronously
      3. Converts the tool's response content to a JSON string
      Specified by:
      call in interface FunctionCallback
      Specified by:
      call in interface ToolCallback
      Parameters:
      functionInput - the tool input as a JSON string
      Returns:
      the tool's response as a JSON string
    • call

      public String call(String toolArguments, ToolContext toolContext)
      Description copied from interface: ToolCallback
      Execute tool with the given input and context, and return the result to send back to the AI model.
      Specified by:
      call in interface FunctionCallback
      Specified by:
      call in interface ToolCallback
      Parameters:
      toolArguments - JSON string with the function arguments to be passed to the function. The arguments are defined as JSON schema usually registered with the model. Arguments are provided by the AI model.
      Returns:
      String containing the function call response.