Class: SwarmSDK::Swarm::McpConfigurator

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/swarm/mcp_configurator.rb

Overview

Handles MCP (Model Context Protocol) server configuration and client management

Responsibilities:

  • Register MCP servers for agents
  • Initialize MCP clients (stdio, SSE, streamable transports)
  • Build transport-specific configurations
  • Track clients for cleanup

This encapsulates all MCP-related logic that was previously in Swarm.

Instance Method Summary collapse

Constructor Details

#initialize(swarm) ⇒ McpConfigurator

Returns a new instance of McpConfigurator.



15
16
17
18
# File 'lib/swarm_sdk/swarm/mcp_configurator.rb', line 15

def initialize(swarm)
  @swarm = swarm
  @mcp_clients = swarm.mcp_clients
end

Instance Method Details

#build_transport_config(transport_type, config) ⇒ Hash

Build transport-specific configuration for MCP client

This method is public for testing delegation from Swarm.

Parameters:

  • transport_type (Symbol)

    Transport type (:stdio, :sse, :streamable)

  • config (Hash)

    MCP server configuration

Returns:

  • (Hash)

    Transport-specific configuration



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/swarm_sdk/swarm/mcp_configurator.rb', line 59

def build_transport_config(transport_type, config)
  case transport_type
  when :stdio
    build_stdio_config(config)
  when :sse
    build_sse_config(config)
  when :streamable
    build_streamable_config(config)
  else
    raise ArgumentError, "Unsupported transport type: #{transport_type}"
  end
end

#register_mcp_servers(chat, mcp_server_configs, agent_name:) ⇒ Object

Register MCP servers for an agent

Connects to MCP servers and registers their tools with the agent's chat instance. Supports stdio, SSE, and HTTP (streamable) transports.

Parameters:

  • chat (AgentChat)

    The agent's chat instance

  • mcp_server_configs (Array<Hash>)

    MCP server configurations

  • agent_name (Symbol)

    Agent name for tracking clients



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/swarm_sdk/swarm/mcp_configurator.rb', line 28

def register_mcp_servers(chat, mcp_server_configs, agent_name:)
  return if mcp_server_configs.nil? || mcp_server_configs.empty?

  # Ensure MCP logging is configured before creating clients
  Swarm.apply_mcp_logging_configuration

  mcp_server_configs.each do |server_config|
    client = initialize_mcp_client(server_config)

    # Store client for cleanup
    @mcp_clients[agent_name] << client

    # Fetch tools from MCP server and register with chat
    # Tools are already in RubyLLM::Tool format
    tools = client.tools
    tools.each { |tool| chat.add_tool(tool) }

    RubyLLM.logger.debug("SwarmSDK: Registered #{tools.size} tools from MCP server '#{server_config[:name]}' for agent #{agent_name}")
  rescue StandardError => e
    RubyLLM.logger.error("SwarmSDK: Failed to initialize MCP server '#{server_config[:name]}' for agent #{agent_name}: #{e.message}")
    raise ConfigurationError, "Failed to initialize MCP server '#{server_config[:name]}': #{e.message}"
  end
end