Class: SwarmSDK::V3::MCP::Connector
- Inherits:
-
Object
- Object
- SwarmSDK::V3::MCP::Connector
- Defined in:
- lib/swarm_sdk/v3/mcp/connector.rb
Overview
Manages the full MCP client lifecycle
Connects to an MCP server, discovers available tools, and provides methods to call tools and convert them to RubyLLM::Tool instances. Accepts an optional ‘transport:` for dependency injection in tests.
Instance Attribute Summary collapse
-
#available_tools ⇒ Array<MCP::Client::Tool>
readonly
Discovered MCP tools.
-
#server_definition ⇒ ServerDefinition
readonly
Server configuration.
Instance Method Summary collapse
-
#call_tool(name, **arguments) ⇒ String
Call an MCP tool by name.
-
#connect! ⇒ self
Connect to the MCP server and discover tools.
-
#connected? ⇒ Boolean
Whether the connector is currently connected.
-
#disconnect! ⇒ void
Disconnect from the MCP server.
-
#initialize(server_definition, transport: nil) ⇒ Connector
constructor
Create a new connector.
-
#to_ruby_llm_tools ⇒ Array<RubyLLM::Tool>
Convert MCP tools to RubyLLM::Tool instances.
Constructor Details
#initialize(server_definition, transport: nil) ⇒ Connector
Create a new connector
40 41 42 43 44 45 46 |
# File 'lib/swarm_sdk/v3/mcp/connector.rb', line 40 def initialize(server_definition, transport: nil) @server_definition = server_definition @injected_transport = transport @client = nil @transport = nil @available_tools = [] end |
Instance Attribute Details
#available_tools ⇒ Array<MCP::Client::Tool> (readonly)
Returns Discovered MCP tools.
28 29 30 |
# File 'lib/swarm_sdk/v3/mcp/connector.rb', line 28 def available_tools @available_tools end |
#server_definition ⇒ ServerDefinition (readonly)
Returns Server configuration.
25 26 27 |
# File 'lib/swarm_sdk/v3/mcp/connector.rb', line 25 def server_definition @server_definition end |
Instance Method Details
#call_tool(name, **arguments) ⇒ String
Call an MCP tool by name
83 84 85 86 87 88 89 |
# File 'lib/swarm_sdk/v3/mcp/connector.rb', line 83 def call_tool(name, **arguments) tool = @available_tools.find { |t| t.name == name.to_s } raise McpError, "Unknown MCP tool: #{name}" unless tool result = @client.call_tool(tool: tool, arguments: arguments) extract_content(result) end |
#connect! ⇒ self
Connect to the MCP server and discover tools
Builds or uses the injected transport, creates an MCP::Client, and fetches the list of available tools from the server.
59 60 61 62 63 64 |
# File 'lib/swarm_sdk/v3/mcp/connector.rb', line 59 def connect! @transport = @injected_transport || build_transport @client = ::MCP::Client.new(transport: @transport) @available_tools = @client.tools self end |
#connected? ⇒ Boolean
Whether the connector is currently connected
69 70 71 |
# File 'lib/swarm_sdk/v3/mcp/connector.rb', line 69 def connected? !@client.nil? end |
#disconnect! ⇒ void
This method returns an undefined value.
Disconnect from the MCP server
Closes the transport and clears all state. Safe to call multiple times or when not connected.
111 112 113 114 115 116 |
# File 'lib/swarm_sdk/v3/mcp/connector.rb', line 111 def disconnect! @transport.close if @transport.respond_to?(:close) @client = nil @transport = nil @available_tools = [] end |
#to_ruby_llm_tools ⇒ Array<RubyLLM::Tool>
Convert MCP tools to RubyLLM::Tool instances
Filters tools based on the server definition’s tool list, then creates RubyLLM::Tool proxies for each.
101 102 103 |
# File 'lib/swarm_sdk/v3/mcp/connector.rb', line 101 def to_ruby_llm_tools tools_to_expose.map { |mcp_tool| ToolProxy.create(mcp_tool, self) } end |