Class: AgentHarness::McpServer

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_harness/mcp_server.rb

Overview

Canonical representation of an MCP server for request-time execution.

Provider-agnostic value object that can be translated by each provider adapter into its CLI-specific configuration.

Examples:

stdio server

McpServer.new(
  name: "filesystem",
  transport: "stdio",
  command: ["npx", "-y", "@modelcontextprotocol/server-filesystem", "/workspace"],
  env: { "DEBUG" => "0" }
)

HTTP/URL server

McpServer.new(
  name: "playwright",
  transport: "http",
  url: "http://mcp-playwright:3000/mcp"
)

Constant Summary collapse

VALID_TRANSPORTS =
%w[stdio http sse].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, transport:, command: nil, args: nil, env: nil, url: nil) ⇒ McpServer

Returns a new instance of McpServer.

Parameters:

  • name (String)

    unique name for this MCP server

  • transport (String)

    one of “stdio”, “http”, “sse”

  • command (Array<String>, nil) (defaults to: nil)

    command to launch (stdio only)

  • args (Array<String>, nil) (defaults to: nil)

    additional args for the command

  • env (Hash<String,String>, nil) (defaults to: nil)

    environment variables for the server process

  • url (String, nil) (defaults to: nil)

    URL for HTTP/SSE transport



34
35
36
37
38
39
40
41
42
43
# File 'lib/agent_harness/mcp_server.rb', line 34

def initialize(name:, transport:, command: nil, args: nil, env: nil, url: nil)
  @name = name
  @transport = transport.to_s
  @command = command
  @args = args || []
  @env = env || {}
  @url = url

  validate!
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



26
27
28
# File 'lib/agent_harness/mcp_server.rb', line 26

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



26
27
28
# File 'lib/agent_harness/mcp_server.rb', line 26

def command
  @command
end

#envObject (readonly)

Returns the value of attribute env.



26
27
28
# File 'lib/agent_harness/mcp_server.rb', line 26

def env
  @env
end

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/agent_harness/mcp_server.rb', line 26

def name
  @name
end

#transportObject (readonly)

Returns the value of attribute transport.



26
27
28
# File 'lib/agent_harness/mcp_server.rb', line 26

def transport
  @transport
end

#urlObject (readonly)

Returns the value of attribute url.



26
27
28
# File 'lib/agent_harness/mcp_server.rb', line 26

def url
  @url
end

Class Method Details

.from_hash(hash) ⇒ McpServer

Build from a plain Hash (e.g. from user input or serialized config)

Parameters:

  • hash (Hash)

    server definition

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/agent_harness/mcp_server.rb', line 49

def self.from_hash(hash)
  unless hash.is_a?(Hash)
    raise McpConfigurationError, "MCP server definition must be a Hash, got #{hash.class}"
  end

  begin
    hash = hash.transform_keys(&:to_sym)
  rescue NoMethodError, TypeError => e
    raise McpConfigurationError, "MCP server hash contains invalid keys: #{e.message}"
  end

  new(
    name: hash[:name],
    transport: hash[:transport],
    command: hash[:command],
    args: hash[:args],
    env: hash[:env],
    url: hash[:url]
  )
end

Instance Method Details

#http?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/agent_harness/mcp_server.rb', line 74

def http?
  %w[http sse].include?(@transport)
end

#stdio?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/agent_harness/mcp_server.rb', line 70

def stdio?
  @transport == "stdio"
end

#to_hObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/agent_harness/mcp_server.rb', line 78

def to_h
  h = {name: @name, transport: @transport}
  if stdio?
    h[:command] = @command
    h[:args] = @args unless @args.empty?
  else
    h[:url] = @url
  end
  h[:env] = @env unless @env.empty?
  h
end