Class: RobotLab::MCP::Client
- Inherits:
-
Object
- Object
- RobotLab::MCP::Client
- Defined in:
- lib/robot_lab/mcp/client.rb
Overview
MCP client for communicating with Model Context Protocol servers
Uses actionmcp gem for MCP protocol implementation. Supports multiple transport types: StdIO, SSE, WebSocket, HTTP.
Instance Attribute Summary collapse
-
#connected ⇒ Boolean
readonly
Whether currently connected.
-
#server ⇒ Server
readonly
The MCP server configuration.
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
Instance Method Summary collapse
-
#call_tool(name, arguments = {}) ⇒ Object
Call a tool on the server.
-
#connect ⇒ self
Connect to the MCP server.
-
#connected? ⇒ Boolean
Checks if the client is connected to the server.
-
#disconnect ⇒ self
Disconnect from the server.
-
#get_prompt(name, arguments = {}) ⇒ Hash
Get a prompt.
-
#initialize(server_or_config, poller: nil) ⇒ Client
constructor
Creates a new MCP Client instance.
-
#list_prompts ⇒ Array<Hash>
List available prompts.
-
#list_resources ⇒ Array<Hash>
List available resources.
-
#list_tools ⇒ Array<Hash>
List available tools from the server.
-
#read_resource(uri) ⇒ Object
Read a resource.
-
#to_h ⇒ Hash
Converts the client to a hash representation.
Constructor Details
#initialize(server_or_config, poller: nil) ⇒ Client
Creates a new MCP Client instance.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/robot_lab/mcp/client.rb', line 29 def initialize(server_or_config, poller: nil) @server = case server_or_config when Server server_or_config when Hash Server.new(**server_or_config.transform_keys(&:to_sym)) else raise ArgumentError, "Invalid server config" end @connected = false @transport = nil @request_id = 0 @poller = poller end |
Instance Attribute Details
#connected ⇒ Boolean (readonly)
Returns whether currently connected.
21 |
# File 'lib/robot_lab/mcp/client.rb', line 21 attr_reader :server, :connected, :transport |
#server ⇒ Server (readonly)
Returns the MCP server configuration.
21 22 23 |
# File 'lib/robot_lab/mcp/client.rb', line 21 def server @server end |
#transport ⇒ Object (readonly)
Returns the value of attribute transport.
21 22 23 |
# File 'lib/robot_lab/mcp/client.rb', line 21 def transport @transport end |
Instance Method Details
#call_tool(name, arguments = {}) ⇒ Object
Call a tool on the server
96 97 98 99 100 101 102 103 |
# File 'lib/robot_lab/mcp/client.rb', line 96 def call_tool(name, arguments = {}) ensure_connected! response = request( method: "tools/call", params: { name: name, arguments: arguments } ) response[:content] || response end |
#connect ⇒ self
Connect to the MCP server
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/robot_lab/mcp/client.rb', line 48 def connect return self if @connected @transport = create_transport @transport.connect if @transport.respond_to?(:connect) @connected = true # Register with shared poller after the transport is connected @poller.register(self) if @poller self rescue StandardError => e RobotLab.config.logger.warn("MCP connection failed for #{@server.name}: #{e.}") @connected = false self end |
#connected? ⇒ Boolean
Checks if the client is connected to the server.
151 152 153 |
# File 'lib/robot_lab/mcp/client.rb', line 151 def connected? @connected end |
#disconnect ⇒ self
Disconnect from the server
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/robot_lab/mcp/client.rb', line 69 def disconnect return self unless @connected @poller.unregister(self) if @poller @transport.close if @transport.respond_to?(:close) @connected = false @transport = nil self end |
#get_prompt(name, arguments = {}) ⇒ Hash
Get a prompt
142 143 144 145 146 |
# File 'lib/robot_lab/mcp/client.rb', line 142 def get_prompt(name, arguments = {}) ensure_connected! response = request(method: "prompts/get", params: { name: name, arguments: arguments }) response end |
#list_prompts ⇒ Array<Hash>
List available prompts
130 131 132 133 134 |
# File 'lib/robot_lab/mcp/client.rb', line 130 def list_prompts ensure_connected! response = request(method: "prompts/list") response[:prompts] || [] end |
#list_resources ⇒ Array<Hash>
List available resources
109 110 111 112 113 |
# File 'lib/robot_lab/mcp/client.rb', line 109 def list_resources ensure_connected! response = request(method: "resources/list") response[:resources] || [] end |
#list_tools ⇒ Array<Hash>
List available tools from the server
84 85 86 87 88 |
# File 'lib/robot_lab/mcp/client.rb', line 84 def list_tools ensure_connected! response = request(method: "tools/list") response[:tools] || [] end |
#read_resource(uri) ⇒ Object
Read a resource
120 121 122 123 124 |
# File 'lib/robot_lab/mcp/client.rb', line 120 def read_resource(uri) ensure_connected! response = request(method: "resources/read", params: { uri: uri }) response[:contents] || response end |
#to_h ⇒ Hash
Converts the client to a hash representation.
158 159 160 161 162 163 |
# File 'lib/robot_lab/mcp/client.rb', line 158 def to_h { server: @server.to_h, connected: @connected } end |