Module: SwarmSDK::V3::MCP::ToolProxy

Extended by:
ToolProxy
Included in:
ToolProxy
Defined in:
lib/swarm_sdk/v3/mcp/tool_proxy.rb

Overview

Bridges MCP tools into RubyLLM::Tool instances

Creates anonymous RubyLLM::Tool subclasses that delegate execution to an MCP connector. Each MCP tool gets its own Tool subclass with the correct name, description, and JSON Schema parameters.

Examples:

mcp_tool = connector.available_tools.first
ruby_llm_tool = ToolProxy.create(mcp_tool, connector)
ruby_llm_tool.name        #=> "echo"
ruby_llm_tool.call(message: "hi")  #=> "hi"

Instance Method Summary collapse

Instance Method Details

#create(mcp_tool, connector) ⇒ RubyLLM::Tool

Create a RubyLLM::Tool instance from an MCP tool

Builds an anonymous subclass of RubyLLM::Tool with the MCP tool’s name, description, and input schema. The ‘execute` method delegates to the connector’s ‘call_tool`.

Examples:

tool = ToolProxy.create(mcp_tool, connector)
chat.with_tools(tool)

Parameters:

  • mcp_tool (MCP::Client::Tool)

    MCP tool descriptor

  • connector (Connector)

    MCP connector for tool execution

Returns:

  • (RubyLLM::Tool)

    Instantiated tool ready for use with RubyLLM::Chat



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/swarm_sdk/v3/mcp/tool_proxy.rb', line 33

def create(mcp_tool, connector)
  tool_name = mcp_tool.name
  tool_desc = mcp_tool.description || "MCP tool: #{tool_name}"
  schema = mcp_tool.input_schema

  klass = Class.new(RubyLLM::Tool) do
    description tool_desc
    params schema if schema
  end

  klass.define_method(:name) { tool_name }
  klass.define_method(:execute) do |**args|
    connector.call_tool(tool_name, **args)
  end

  klass.new
end