Class: RubynCode::MCP::Client
- Inherits:
-
Object
- Object
- RubynCode::MCP::Client
- Defined in:
- lib/rubyn_code/mcp/client.rb
Overview
High-level MCP client that manages the connection lifecycle, tool discovery, and tool invocation for a single MCP server.
Defined Under Namespace
Classes: ClientError
Constant Summary collapse
- INITIALIZE_TIMEOUT =
10
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
Class Method Summary collapse
-
.from_config(server_config) ⇒ Client
Factory method that creates a Client with the appropriate transport based on the server configuration.
Instance Method Summary collapse
-
#call_tool(tool_name, arguments = {}) ⇒ Hash
Invokes a tool on the MCP server.
-
#connect! ⇒ void
Starts the transport, performs the MCP initialize handshake, and discovers available tools.
-
#connected? ⇒ Boolean
Returns whether the client is connected and the transport is alive.
-
#disconnect! ⇒ void
Gracefully disconnects from the MCP server.
-
#initialize(name:, transport:) ⇒ Client
constructor
A new instance of Client.
-
#tools ⇒ Array<Hash>
Returns the list of tool definitions from the MCP server.
Constructor Details
#initialize(name:, transport:) ⇒ Client
Returns a new instance of Client.
19 20 21 22 23 24 |
# File 'lib/rubyn_code/mcp/client.rb', line 19 def initialize(name:, transport:) @name = name @transport = transport @tools_cache = nil @initialized = false end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/rubyn_code/mcp/client.rb', line 15 def name @name end |
#transport ⇒ Object (readonly)
Returns the value of attribute transport.
15 16 17 |
# File 'lib/rubyn_code/mcp/client.rb', line 15 def transport @transport end |
Class Method Details
.from_config(server_config) ⇒ Client
Factory method that creates a Client with the appropriate transport based on the server configuration.
Configs with a :url key use SSETransport; all others use StdioTransport.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rubyn_code/mcp/client.rb', line 87 def from_config(server_config) name = server_config[:name] transport = if server_config[:url] SSETransport.new( url: server_config[:url], timeout: server_config[:timeout] || SSETransport::DEFAULT_TIMEOUT ) else StdioTransport.new( command: server_config[:command], args: server_config[:args] || [], env: server_config[:env] || {}, timeout: server_config[:timeout] || StdioTransport::DEFAULT_TIMEOUT ) end new(name: name, transport: transport) end |
Instance Method Details
#call_tool(tool_name, arguments = {}) ⇒ Hash
Invokes a tool on the MCP server.
54 55 56 57 58 59 60 61 |
# File 'lib/rubyn_code/mcp/client.rb', line 54 def call_tool(tool_name, arguments = {}) ensure_connected! @transport.send_request('tools/call', { name: tool_name, arguments: arguments }) end |
#connect! ⇒ void
This method returns an undefined value.
Starts the transport, performs the MCP initialize handshake, and discovers available tools.
31 32 33 34 35 36 37 38 |
# File 'lib/rubyn_code/mcp/client.rb', line 31 def connect! @transport.start! perform_initialize @initialized = true rescue StandardError => e @transport.stop! raise ClientError, "Failed to connect to MCP server '#{@name}': #{e.}" end |
#connected? ⇒ Boolean
Returns whether the client is connected and the transport is alive.
75 76 77 |
# File 'lib/rubyn_code/mcp/client.rb', line 75 def connected? @initialized && @transport.alive? end |
#disconnect! ⇒ void
This method returns an undefined value.
Gracefully disconnects from the MCP server.
66 67 68 69 70 |
# File 'lib/rubyn_code/mcp/client.rb', line 66 def disconnect! @transport.stop! @initialized = false @tools = nil end |
#tools ⇒ Array<Hash>
Returns the list of tool definitions from the MCP server. Each tool is a Hash with “name”, “description”, and “inputSchema” keys.
44 45 46 |
# File 'lib/rubyn_code/mcp/client.rb', line 44 def tools @tools ||= discover_tools end |