Class McpToolExecutor

java.lang.Object
com.tnsai.actions.executors.McpToolExecutor
All Implemented Interfaces:
TypedActionExecutor

public class McpToolExecutor extends Object implements TypedActionExecutor
Executor for MCP_TOOL actions. Executes tools on remote MCP (Model Context Protocol) servers.

MCP_TOOL actions are defined in Role classes with @ActionSpec(type = ActionType.MCP_TOOL). The endpoint URL specifies the MCP server to connect to.

Key features:

  • Automatic tool discovery from MCP servers
  • Connection caching per endpoint
  • API key support via environment variables

Example Role:


 @ActionSpec(
     type = ActionType.MCP_TOOL,
     endpoint = "https://mcp.api.coingecko.com/mcp",
     description = "Access cryptocurrency data"
 )
 public String cryptoData(String query) {
     return null; // Executed via MCP
 }
 

The executor will:

  1. Connect to the MCP endpoint
  2. Discover available tools via tools/list
  3. Execute the requested tool via tools/call
  • Constructor Details

    • McpToolExecutor

      public McpToolExecutor()
  • Method Details

    • execute

      public Object execute(ActionMetadata action, Role role, Map<String,Object> parameters, Map<String,Object> context)
      Description copied from interface: TypedActionExecutor
      Executes an action with the given parameters and context.
      Specified by:
      execute in interface TypedActionExecutor
      Parameters:
      action - The action metadata containing annotation details
      role - The role instance that contains the method
      parameters - The parameters for the action (from message or executeAction call)
      context - Additional context (e.g., LLM client reference, agent reference)
      Returns:
      The execution result
    • discoverTools

      public List<Map<String,Object>> discoverTools(String endpoint, String apiKeyEnv)
      Discovers tools from an MCP endpoint and returns them as LLM tool definitions.
      Parameters:
      endpoint - the MCP server URL
      apiKeyEnv - environment variable name for API key (optional)
      Returns:
      list of tool definitions in LLM format
    • getEndpointForTool

      public String getEndpointForTool(String toolName)
      Checks if this executor handles a specific tool name.
      Parameters:
      toolName - the tool name to check
      Returns:
      the endpoint URL if this tool is handled, null otherwise
    • close

      public void close()
      Closes all cached MCP clients.