Class: RubynCode::MCP::Client

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, transport:) ⇒ Client

Returns a new instance of Client.

Parameters:

  • name (String)

    human-readable name for this MCP server connection

  • transport (StdioTransport, SSETransport)

    the underlying transport



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

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/rubyn_code/mcp/client.rb', line 15

def name
  @name
end

#transportObject (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.

Parameters:

  • server_config (Hash)

    configuration hash with :name, :command/:url, :args, :env

Returns:



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.

Parameters:

  • tool_name (String)

    the name of the tool to call

  • arguments (Hash) (defaults to: {})

    the arguments to pass to the tool

Returns:

  • (Hash)

    the tool’s result

Raises:



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.

Raises:



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.message}"
end

#connected?Boolean

Returns whether the client is connected and the transport is alive.

Returns:

  • (Boolean)


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

#toolsArray<Hash>

Returns the list of tool definitions from the MCP server. Each tool is a Hash with “name”, “description”, and “inputSchema” keys.

Returns:

  • (Array<Hash>)

    tool definitions in JSON Schema format



44
45
46
# File 'lib/rubyn_code/mcp/client.rb', line 44

def tools
  @tools ||= discover_tools
end