Module: MCPClient

Defined in:
lib/mcp_client.rb,
lib/mcp_client/tool.rb,
lib/mcp_client/client.rb,
lib/mcp_client/errors.rb,
lib/mcp_client/version.rb,
lib/mcp_client/server_sse.rb,
lib/mcp_client/server_base.rb,
lib/mcp_client/server_stdio.rb,
lib/mcp_client/config_parser.rb,
lib/mcp_client/server_factory.rb

Overview

Model Context Protocol (MCP) Client module Provides a standardized way for agents to communicate with external tools and services through a protocol-based approach

Defined Under Namespace

Modules: Errors Classes: Client, ConfigParser, ServerBase, ServerFactory, ServerSSE, ServerStdio, Tool

Constant Summary collapse

VERSION =

Current version of the MCP client gem

'0.5.1'

Class Method Summary collapse

Class Method Details

.create_client(mcp_server_configs: [], server_definition_file: nil) ⇒ MCPClient::Client

Create a new MCPClient client

Parameters:

  • mcp_server_configs (Array<Hash>) (defaults to: [])

    configurations for MCP servers

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

    optional path to a JSON file defining server configurations The JSON may be a single server object or an array of server objects.

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mcp_client.rb', line 23

def self.create_client(mcp_server_configs: [], server_definition_file: nil)
  require 'json'
  # Start with any explicit configs provided
  configs = Array(mcp_server_configs)
  # Load additional configs from a JSON file if specified
  if server_definition_file
    # Parse JSON definitions into clean config hashes
    parser = MCPClient::ConfigParser.new(server_definition_file)
    parsed = parser.parse
    parsed.each_value do |cfg|
      case cfg[:type].to_s
      when 'stdio'
        # Build command list with args
        cmd_list = [cfg[:command]] + Array(cfg[:args])
        configs << MCPClient.stdio_config(command: cmd_list)
      when 'sse'
        # Use 'url' from parsed config as 'base_url' for SSE config
        configs << MCPClient.sse_config(base_url: cfg[:url], headers: cfg[:headers] || {})
      end
    end
  end
  MCPClient::Client.new(mcp_server_configs: configs)
end

.sse_config(base_url:, headers: {}, read_timeout: 30, retries: 0, retry_backoff: 1) ⇒ Hash

Create a standard server configuration for SSE

Parameters:

  • base_url (String)

    base URL for the server

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

    HTTP headers to include in requests

  • read_timeout (Integer) (defaults to: 30)

    read timeout in seconds (default: 30)

  • retries (Integer) (defaults to: 0)

    number of retry attempts (default: 0)

  • retry_backoff (Integer) (defaults to: 1)

    backoff delay in seconds (default: 1)

Returns:

  • (Hash)

    server configuration



64
65
66
67
68
69
70
71
72
73
# File 'lib/mcp_client.rb', line 64

def self.sse_config(base_url:, headers: {}, read_timeout: 30, retries: 0, retry_backoff: 1)
  {
    type: 'sse',
    base_url: base_url,
    headers: headers,
    read_timeout: read_timeout,
    retries: retries,
    retry_backoff: retry_backoff
  }
end

.stdio_config(command:) ⇒ Hash

Create a standard server configuration for stdio

Parameters:

  • command (String, Array<String>)

    command to execute

Returns:

  • (Hash)

    server configuration



50
51
52
53
54
55
# File 'lib/mcp_client.rb', line 50

def self.stdio_config(command:)
  {
    type: 'stdio',
    command: command
  }
end