Class: Tools::McpTool
- Inherits:
-
Object
- Object
- Tools::McpTool
- Defined in:
- lib/tools/mcp_tool.rb
Overview
Wraps a single MCP server tool for use with Registry. Registered as an instance (not a class) — the Registry calls #execute directly without instantiation, since MCP tools carry their own client reference and are effectively stateless from the LLM’s perspective.
Implements the same duck-typed interface as Base subclasses:
-
#tool_name— unique identifier -
#description— human-readable description -
#input_schema— JSON Schema for parameters -
#schema— Anthropic API tool definition -
#execute(input) — run the tool
Tool names are namespaced as ‘<server_name>__<tool_name>` to prevent collisions between servers and with built-in tools.
Constant Summary collapse
- NAMESPACE_SEPARATOR =
Separator between server name and tool name in namespaced identifiers.
"__"
Instance Attribute Summary collapse
-
#tool_name ⇒ String
readonly
Namespaced tool identifier (<server>__<tool>).
Instance Method Summary collapse
-
#description ⇒ String
Tool description from the MCP server.
-
#execute(input) ⇒ String, Hash
Calls the MCP server tool and normalizes the response.
-
#initialize(server_name:, mcp_client:, mcp_tool:) ⇒ McpTool
constructor
A new instance of McpTool.
-
#input_schema ⇒ Hash
JSON Schema for tool input parameters.
-
#schema ⇒ Hash
Builds the schema hash expected by the Anthropic tools API.
Constructor Details
#initialize(server_name:, mcp_client:, mcp_tool:) ⇒ McpTool
Returns a new instance of McpTool.
38 39 40 41 42 |
# File 'lib/tools/mcp_tool.rb', line 38 def initialize(server_name:, mcp_client:, mcp_tool:) @tool_name = "#{server_name}#{NAMESPACE_SEPARATOR}#{mcp_tool.name}" @mcp_client = mcp_client @mcp_tool = mcp_tool end |
Instance Attribute Details
#tool_name ⇒ String (readonly)
Returns namespaced tool identifier (<server>__<tool>).
33 34 35 |
# File 'lib/tools/mcp_tool.rb', line 33 def tool_name @tool_name end |
Instance Method Details
#description ⇒ String
Returns tool description from the MCP server.
45 46 47 |
# File 'lib/tools/mcp_tool.rb', line 45 def description @mcp_tool.description end |
#execute(input) ⇒ String, Hash
Calls the MCP server tool and normalizes the response.
65 66 67 68 69 70 |
# File 'lib/tools/mcp_tool.rb', line 65 def execute(input) response = @mcp_client.call_tool(tool: @mcp_tool, arguments: input) normalize_response(response) rescue MCP::Client::RequestHandlerError => error {error: "#{tool_name}: #{error.}"} end |
#input_schema ⇒ Hash
Returns JSON Schema for tool input parameters.
50 51 52 |
# File 'lib/tools/mcp_tool.rb', line 50 def input_schema @mcp_tool.input_schema end |
#schema ⇒ Hash
Builds the schema hash expected by the Anthropic tools API.
56 57 58 |
# File 'lib/tools/mcp_tool.rb', line 56 def schema {name: tool_name, description: description, input_schema: input_schema} end |