Module: MCPClient::JsonRpcCommon

Included in:
HttpTransportBase, ServerSSE::JsonRpcTransport, ServerStdio::JsonRpcTransport
Defined in:
lib/mcp_client/json_rpc_common.rb

Overview

Shared retry/backoff logic for JSON-RPC transports

Instance Method Summary collapse

Instance Method Details

#build_jsonrpc_notification(method, params) ⇒ Hash

Build a JSON-RPC notification object (no response expected)

Parameters:

  • method (String)

    JSON-RPC method name

  • params (Hash)

    parameters for the notification

Returns:

  • (Hash)

    the JSON-RPC notification object



54
55
56
57
58
59
60
# File 'lib/mcp_client/json_rpc_common.rb', line 54

def build_jsonrpc_notification(method, params)
  {
    'jsonrpc' => '2.0',
    'method' => method,
    'params' => params
  }
end

#build_jsonrpc_request(method, params, id) ⇒ Hash

Build a JSON-RPC request object

Parameters:

  • method (String)

    JSON-RPC method name

  • params (Hash)

    parameters for the request

  • id (Integer)

    request ID

Returns:

  • (Hash)

    the JSON-RPC request object



41
42
43
44
45
46
47
48
# File 'lib/mcp_client/json_rpc_common.rb', line 41

def build_jsonrpc_request(method, params, id)
  {
    'jsonrpc' => '2.0',
    'id' => id,
    'method' => method,
    'params' => params
  }
end

#initialization_paramsHash

Generate initialization parameters for MCP protocol

Returns:

  • (Hash)

    the initialization parameters



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

def initialization_params
  {
    'protocolVersion' => MCPClient::PROTOCOL_VERSION,
    'capabilities' => {},
    'clientInfo' => { 'name' => 'ruby-mcp-client', 'version' => MCPClient::VERSION }
  }
end

#pingHash

Ping the server to keep the connection alive

Returns:

  • (Hash)

    the result of the ping request

Raises:



32
33
34
# File 'lib/mcp_client/json_rpc_common.rb', line 32

def ping
  rpc_request('ping')
end

#process_jsonrpc_response(response) ⇒ Object

Process JSON-RPC response

Parameters:

  • response (Hash)

    the parsed JSON-RPC response

Returns:

  • (Object)

    the result field from the response

Raises:



76
77
78
79
80
# File 'lib/mcp_client/json_rpc_common.rb', line 76

def process_jsonrpc_response(response)
  raise MCPClient::Errors::ServerError, response['error']['message'] if response['error']

  response['result']
end

#with_retry { ... } ⇒ Object

Execute the block with retry/backoff for transient errors

Yields:

  • block to execute

Returns:

  • (Object)

    result of block

Raises:

  • original exception if max retries exceeded



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mcp_client/json_rpc_common.rb', line 10

def with_retry
  attempts = 0
  begin
    yield
  rescue MCPClient::Errors::ServerError, MCPClient::Errors::TransportError, IOError, Errno::ETIMEDOUT,
         Errno::ECONNRESET => e
    attempts += 1
    if attempts <= @max_retries
      delay = @retry_backoff * (2**(attempts - 1))
      @logger.debug("Retry attempt #{attempts} after error: #{e.message}, sleeping #{delay}s")
      sleep(delay)
      retry
    end
    raise
  end
end