Class: Tools::McpTool

Inherits:
Object
  • Object
show all
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.

Examples:

wrapper = Tools::McpTool.new(
  server_name: "mythonix",
  mcp_client: client,
  mcp_tool: client.tools.first
)
wrapper.tool_name  # => "mythonix__create_image"
wrapper.execute({"prompt" => "a red dragon"})

Constant Summary collapse

NAMESPACE_SEPARATOR =

Separator between server name and tool name in namespaced identifiers.

"__"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_name:, mcp_client:, mcp_tool:) ⇒ McpTool

Returns a new instance of McpTool.

Parameters:

  • server_name (String)

    MCP server name from config

  • mcp_client (MCP::Client)

    the client instance for this server

  • mcp_tool (MCP::Client::Tool)

    tool metadata from the server



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_nameString (readonly)

Returns namespaced tool identifier (<server>__<tool>).

Returns:

  • (String)

    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

#descriptionString

Returns tool description from the MCP server.

Returns:

  • (String)

    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.

Parameters:

  • input (Hash)

    tool input parameters from the LLM

Returns:

  • (String)

    normalized tool output

  • (Hash)

    with :error key on failure



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.message}"}
end

#input_schemaHash

Returns JSON Schema for tool input parameters.

Returns:

  • (Hash)

    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

#schemaHash

Builds the schema hash expected by the Anthropic tools API.

Returns:

  • (Hash)

    with :name, :description, and :input_schema keys



56
57
58
# File 'lib/tools/mcp_tool.rb', line 56

def schema
  {name: tool_name, description: description, input_schema: input_schema}
end