Module: MCPClient
- Defined in:
- lib/mcp_client.rb,
lib/mcp_client/auth.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_http.rb,
lib/mcp_client/oauth_client.rb,
lib/mcp_client/server_stdio.rb,
lib/mcp_client/config_parser.rb,
lib/mcp_client/server_factory.rb,
lib/mcp_client/json_rpc_common.rb,
lib/mcp_client/auth/oauth_provider.rb,
lib/mcp_client/http_transport_base.rb,
lib/mcp_client/server_sse/sse_parser.rb,
lib/mcp_client/server_streamable_http.rb,
lib/mcp_client/server_sse/reconnect_monitor.rb,
lib/mcp_client/server_sse/json_rpc_transport.rb,
lib/mcp_client/server_http/json_rpc_transport.rb,
lib/mcp_client/server_stdio/json_rpc_transport.rb,
lib/mcp_client/server_streamable_http/json_rpc_transport.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: Auth, Errors, HttpTransportBase, JsonRpcCommon Classes: Client, ConfigParser, OAuthClient, ServerBase, ServerFactory, ServerHTTP, ServerSSE, ServerStdio, ServerStreamableHTTP, Tool
Constant Summary collapse
- VERSION =
Current version of the MCP client gem
'0.7.2'- PROTOCOL_VERSION =
MCP protocol version (date-based) - unified across all transports
'2025-03-26'
Class Method Summary collapse
-
.create_client(mcp_server_configs: [], server_definition_file: nil, logger: nil) ⇒ MCPClient::Client
Create a new MCPClient client.
-
.http_config(base_url:, endpoint: '/rpc', headers: {}, read_timeout: 30, retries: 3, retry_backoff: 1, name: nil, logger: nil) ⇒ Hash
Create a standard server configuration for HTTP.
-
.sse_config(base_url:, headers: {}, read_timeout: 30, ping: 10, retries: 0, retry_backoff: 1, name: nil, logger: nil) ⇒ Hash
Create a standard server configuration for SSE.
-
.stdio_config(command:, name: nil, logger: nil, env: {}) ⇒ Hash
Create a standard server configuration for stdio.
-
.streamable_http_config(base_url:, endpoint: '/rpc', headers: {}, read_timeout: 30, retries: 3, retry_backoff: 1, name: nil, logger: nil) ⇒ Hash
Create configuration for Streamable HTTP transport This transport uses HTTP POST requests but expects Server-Sent Event formatted responses.
Class Method Details
.create_client(mcp_server_configs: [], server_definition_file: nil, logger: nil) ⇒ MCPClient::Client
Create a new MCPClient client
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mcp_client.rb', line 28 def self.create_client(mcp_server_configs: [], server_definition_file: nil, logger: 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, logger: logger) parsed = parser.parse parsed.each_value do |cfg| case cfg[:type].to_s when 'stdio' cmd_list = [cfg[:command]] + Array(cfg[:args]) configs << MCPClient.stdio_config( command: cmd_list, name: cfg[:name], logger: logger, env: cfg[:env] ) when 'sse' configs << MCPClient.sse_config(base_url: cfg[:url], headers: cfg[:headers] || {}, name: cfg[:name], logger: logger) when 'http' configs << MCPClient.http_config(base_url: cfg[:url], endpoint: cfg[:endpoint], headers: cfg[:headers] || {}, name: cfg[:name], logger: logger) when 'streamable_http' configs << MCPClient.streamable_http_config(base_url: cfg[:url], endpoint: cfg[:endpoint], headers: cfg[:headers] || {}, name: cfg[:name], logger: logger) end end end MCPClient::Client.new(mcp_server_configs: configs, logger: logger) end |
.http_config(base_url:, endpoint: '/rpc', headers: {}, read_timeout: 30, retries: 3, retry_backoff: 1, name: nil, logger: nil) ⇒ Hash
Create a standard server configuration for HTTP
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/mcp_client.rb', line 112 def self.http_config(base_url:, endpoint: '/rpc', headers: {}, read_timeout: 30, retries: 3, retry_backoff: 1, name: nil, logger: nil) { type: 'http', base_url: base_url, endpoint: endpoint, headers: headers, read_timeout: read_timeout, retries: retries, retry_backoff: retry_backoff, name: name, logger: logger } end |
.sse_config(base_url:, headers: {}, read_timeout: 30, ping: 10, retries: 0, retry_backoff: 1, name: nil, logger: nil) ⇒ Hash
Create a standard server configuration for SSE
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mcp_client.rb', line 87 def self.sse_config(base_url:, headers: {}, read_timeout: 30, ping: 10, retries: 0, retry_backoff: 1, name: nil, logger: nil) { type: 'sse', base_url: base_url, headers: headers, read_timeout: read_timeout, ping: ping, retries: retries, retry_backoff: retry_backoff, name: name, logger: logger } end |
.stdio_config(command:, name: nil, logger: nil, env: {}) ⇒ Hash
Create a standard server configuration for stdio
67 68 69 70 71 72 73 74 75 |
# File 'lib/mcp_client.rb', line 67 def self.stdio_config(command:, name: nil, logger: nil, env: {}) { type: 'stdio', command: command, name: name, logger: logger, env: env || {} } end |
.streamable_http_config(base_url:, endpoint: '/rpc', headers: {}, read_timeout: 30, retries: 3, retry_backoff: 1, name: nil, logger: nil) ⇒ Hash
Create configuration for Streamable HTTP transport This transport uses HTTP POST requests but expects Server-Sent Event formatted responses
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/mcp_client.rb', line 138 def self.streamable_http_config(base_url:, endpoint: '/rpc', headers: {}, read_timeout: 30, retries: 3, retry_backoff: 1, name: nil, logger: nil) { type: 'streamable_http', base_url: base_url, endpoint: endpoint, headers: headers, read_timeout: read_timeout, retries: retries, retry_backoff: retry_backoff, name: name, logger: logger } end |