Skip to content

MCP Client

Connects to MCP servers and discovers tools.

Class: RobotLab::MCP::Client

client = RobotLab::MCP::Client.new(
  name: "filesystem",
  transport: { type: "stdio", command: "mcp-server-filesystem" }
)

client.connect
tools = client.list_tools

Constructor

Client.new(name:, transport:)

Parameters:

Name Type Description
name String Server identifier
transport Hash Transport configuration

Attributes

name

client.name  # => String

Server identifier.

transport

client.transport  # => Transport

The underlying transport connection.

connected?

client.connected?  # => Boolean

Whether the client is connected.

Methods

connect

client.connect

Establish connection to the MCP server.

disconnect

client.disconnect

Close the connection.

list_tools

client.list_tools  # => Array<Tool>

Discover available tools from the server.

call_tool

result = client.call_tool(name, params)

Execute a tool on the server.

Parameters:

Name Type Description
name String Tool name
params Hash Tool parameters

list_resources

client.list_resources  # => Array<Resource>

List available resources (if supported).

read_resource

client.read_resource(uri)  # => Resource

Read a resource by URI.

Transport Configuration

Stdio

client = Client.new(
  name: "local",
  transport: {
    type: "stdio",
    command: "npx",
    args: ["@modelcontextprotocol/server-filesystem", "/path"]
  }
)

WebSocket

client = Client.new(
  name: "remote",
  transport: {
    type: "websocket",
    url: "wss://mcp.example.com/ws"
  }
)

SSE

client = Client.new(
  name: "streaming",
  transport: {
    type: "sse",
    url: "https://mcp.example.com/sse"
  }
)

HTTP

client = Client.new(
  name: "http",
  transport: {
    type: "http",
    url: "https://mcp.example.com/mcp"
  }
)

Examples

Basic Usage

client = Client.new(
  name: "github",
  transport: { type: "stdio", command: "mcp-server-github" }
)

client.connect

# List available tools
tools = client.list_tools
tools.each { |t| puts "#{t.name}: #{t.description}" }

# Call a tool
result = client.call_tool("search_repositories", { query: "ruby mcp" })
puts result

client.disconnect

In Robot

robot = RobotLab.build do
  name "assistant"

  mcp [
    { name: "fs", transport: { type: "stdio", command: "mcp-fs" } }
  ]
end

# MCP tools are automatically available
robot.tools.each { |t| puts t.name }

Error Handling

begin
  client.connect
  result = client.call_tool("unknown_tool", {})
rescue RobotLab::MCP::ConnectionError => e
  puts "Failed to connect: #{e.message}"
rescue RobotLab::MCP::ToolError => e
  puts "Tool error: #{e.message}"
ensure
  client.disconnect
end

See Also