Class: SwarmCLI::Commands::McpServe

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_cli/commands/mcp_serve.rb

Overview

McpServe command starts an MCP server that exposes the swarm's lead agent as a tool.

Usage:

swarm mcp serve config.yml

The server uses stdio transport and exposes a "swarm" tool that executes tasks through the configured lead agent.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ McpServe

Returns a new instance of McpServe.



15
16
17
# File 'lib/swarm_cli/commands/mcp_serve.rb', line 15

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/swarm_cli/commands/mcp_serve.rb', line 13

def options
  @options
end

Instance Method Details

#executeObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/swarm_cli/commands/mcp_serve.rb', line 19

def execute
  # Validate options
  options.validate!

  # Load swarm configuration to validate it
  config_path = options.config_file
  unless File.exist?(config_path)
    $stderr.puts "Error: Configuration file not found: #{config_path}"
    exit(1)
  end

  # Validate the swarm configuration
  begin
    SwarmSDK.load_file(config_path)
  rescue SwarmSDK::ConfigurationError => e
    $stderr.puts "Error: Invalid swarm configuration: #{e.message}"
    exit(1)
  end

  # MCP servers should be quiet - stdout is reserved for MCP protocol
  # Errors will still be logged to stderr

  # Start the MCP server
  start_mcp_server(config_path)
rescue Interrupt
  # User cancelled (Ctrl+C) - silent exit
  exit(130)
rescue StandardError => e
  # Unexpected errors - always log to stderr
  $stderr.puts "Fatal error: #{e.message}"
  $stderr.puts e.backtrace.first(5).join("\n") if ENV["DEBUG"]
  exit(1)
end