类 McpTool

java.lang.Object
io.agentscope.core.tool.mcp.McpTool
所有已实现的接口:
AgentTool

public class McpTool extends Object implements AgentTool
AgentTool implementation that wraps an MCP (Model Context Protocol) tool. This class bridges MCP tools to the AgentScope tool system, allowing agents to invoke remote MCP tools seamlessly.

Features:

  • Converts AgentScope tool calls to MCP protocol calls
  • Handles parameter merging with preset arguments
  • Converts MCP results to AgentScope ToolResultBlocks
  • Supports reactive execution with Mono

Example usage:


 McpTool tool = new McpTool(
     "get_weather",
     "Get current weather for a location",
     parametersSchema,
     mcpClientWrapper,
     Map.of("units", "celsius")  // preset args
 );

 ToolResultBlock result = tool.callAsync(Map.of("location", "Beijing")).block();
 
  • 构造器详细资料

    • McpTool

      public McpTool(String name, String description, Map<String,Object> parameters, McpClientWrapper clientWrapper)
      Constructs a new McpTool without preset arguments.
      参数:
      name - the tool name
      description - the tool description
      parameters - the JSON schema for tool parameters
      clientWrapper - the MCP client wrapper
    • McpTool

      public McpTool(String name, String description, Map<String,Object> parameters, McpClientWrapper clientWrapper, Map<String,Object> presetArguments)
      Constructs a new McpTool with preset arguments.
      参数:
      name - the tool name
      description - the tool description
      parameters - the JSON schema for tool parameters
      clientWrapper - the MCP client wrapper
      presetArguments - preset arguments to merge with each call (can be null)
    • McpTool

      public McpTool(String name, String description, Map<String,Object> parameters, Map<String,Object> outputSchema, McpClientWrapper clientWrapper, Map<String,Object> presetArguments)
      Constructs a new McpTool with an optional output schema and preset arguments.
      参数:
      name - the tool name
      description - the tool description
      parameters - the JSON schema for tool parameters
      outputSchema - the JSON schema for tool outputs (can be null)
      clientWrapper - the MCP client wrapper
      presetArguments - preset arguments to merge with each call (can be null)
  • 方法详细资料

    • getName

      public String getName()
      Returns the name of this MCP tool.
      指定者:
      getName 在接口中 AgentTool
      返回:
      the tool name
    • getDescription

      public String getDescription()
      Returns the description of this MCP tool.
      指定者:
      getDescription 在接口中 AgentTool
      返回:
      the tool description
    • getParameters

      public Map<String,Object> getParameters()
      Returns the JSON schema parameters for this MCP tool.
      指定者:
      getParameters 在接口中 AgentTool
      返回:
      the parameters schema map
    • getOutputSchema

      public Map<String,Object> getOutputSchema()
      从接口复制的说明: AgentTool
      Gets the optional output schema for this tool in JSON Schema format.

      Most tools do not expose a structured output schema to models, so the default implementation returns null. MCP tools can override this to surface the server-provided outputSchema definition.

      指定者:
      getOutputSchema 在接口中 AgentTool
      返回:
      Map representing the JSON Schema for tool outputs, or null if unsupported
    • callAsync

      public reactor.core.publisher.Mono<ToolResultBlock> callAsync(ToolCallParam param)
      Executes this MCP tool asynchronously with the given parameters.

      This method merges any preset arguments with the input arguments (input takes precedence), calls the remote MCP tool via the client wrapper, and converts the result to a ToolResultBlock. If an error occurs, it returns an error result instead of failing.

      指定者:
      callAsync 在接口中 AgentTool
      参数:
      param - The tool call parameters containing toolUseBlock, input, and agent
      返回:
      a Mono that emits the tool result when the MCP call completes
    • getClientName

      public String getClientName()
      Gets the name of the MCP client that provides this tool.
      返回:
      the MCP client name
    • getPresetArguments

      public Map<String,Object> getPresetArguments()
      Gets the preset arguments configured for this tool.
      返回:
      the preset arguments, or null if none configured
    • convertMcpSchemaToParameters

      public static Map<String,Object> convertMcpSchemaToParameters(io.modelcontextprotocol.spec.McpSchema.JsonSchema inputSchema, Set<String> excludeParams)
      Converts MCP JsonSchema to AgentScope parameters format.
      参数:
      inputSchema - the MCP JsonSchema
      返回:
      parameters map in AgentScope format