Package com.tnsai.actions.executors
Class McpToolExecutor
java.lang.Object
com.tnsai.actions.executors.McpToolExecutor
- All Implemented Interfaces:
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:
- Connect to the MCP endpoint
- Discover available tools via tools/list
- Execute the requested tool via tools/call
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes all cached MCP clients.discoverTools(String endpoint, String apiKeyEnv) Discovers tools from an MCP endpoint and returns them as LLM tool definitions.execute(ActionMetadata action, Role role, Map<String, Object> parameters, Map<String, Object> context) Executes an action with the given parameters and context.getEndpointForTool(String toolName) Checks if this executor handles a specific tool name.
-
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:TypedActionExecutorExecutes an action with the given parameters and context.- Specified by:
executein interfaceTypedActionExecutor- Parameters:
action- The action metadata containing annotation detailsrole- The role instance that contains the methodparameters- 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
Discovers tools from an MCP endpoint and returns them as LLM tool definitions.- Parameters:
endpoint- the MCP server URLapiKeyEnv- environment variable name for API key (optional)- Returns:
- list of tool definitions in LLM format
-
getEndpointForTool
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.
-