Package org.springframework.ai.mcp
Class McpToolCallback
java.lang.Object
org.springframework.ai.mcp.McpToolCallback
- All Implemented Interfaces:
FunctionCallback
,ToolCallback
Implementation of
ToolCallback
that adapts MCP tools to Spring AI's tool
interface.
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 execution of tool calls through the MCP client
- Manages JSON serialization/deserialization of tool inputs and outputs
Example usage:
McpSyncClient mcpClient = // obtain MCP client
Tool mcpTool = // obtain MCP tool definition
ToolCallback callback = new McpToolCallback(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:
-
ToolCallback
McpSyncClient
McpSchema.Tool
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.springframework.ai.model.function.FunctionCallback
FunctionCallback.Builder, FunctionCallback.CommonCallbackInvokingSpec<B extends FunctionCallback.CommonCallbackInvokingSpec<B>>, FunctionCallback.FunctionInvokingSpec<I,
O>, FunctionCallback.MethodInvokingSpec, FunctionCallback.SchemaType -
Constructor Summary
ConstructorsConstructorDescriptionMcpToolCallback
(io.modelcontextprotocol.client.McpSyncClient mcpClient, io.modelcontextprotocol.spec.McpSchema.Tool tool) Creates a newMcpToolCallback
instance. -
Method Summary
Modifier and TypeMethodDescriptionExecutes the tool with the provided input.Returns a Spring AI tool definition adapted from the MCP tool.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.ai.tool.ToolCallback
call, getDescription, getInputTypeSchema, getName, getToolMetadata
-
Constructor Details
-
McpToolCallback
public McpToolCallback(io.modelcontextprotocol.client.McpSyncClient mcpClient, io.modelcontextprotocol.spec.McpSchema.Tool tool) Creates a newMcpToolCallback
instance.- Parameters:
mcpClient
- the MCP client to use for tool executiontool
- the MCP tool definition to adapt
-
-
Method Details
-
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 interfaceToolCallback
- Returns:
- the Spring AI tool definition
-
call
Executes the tool with the provided input.This method:
- Converts the JSON input string to a map of arguments
- Calls the tool through the MCP client
- Converts the tool's response content to a JSON string
- Specified by:
call
in interfaceFunctionCallback
- Specified by:
call
in interfaceToolCallback
- Parameters:
functionInput
- the tool input as a JSON string- Returns:
- the tool's response as a JSON string
-