Module: MCPClient::JsonRpcCommon
- Defined in:
- lib/mcp_client/json_rpc_common.rb
Overview
Shared retry/backoff logic for JSON-RPC transports
Instance Method Summary collapse
-
#build_jsonrpc_notification(method, params) ⇒ Hash
Build a JSON-RPC notification object (no response expected).
-
#build_jsonrpc_request(method, params, id) ⇒ Hash
Build a JSON-RPC request object.
-
#initialization_params ⇒ Hash
Generate initialization parameters for MCP protocol.
-
#ping ⇒ Hash
Ping the server to keep the connection alive.
-
#process_jsonrpc_response(response) ⇒ Object
Process JSON-RPC response.
-
#with_retry { ... } ⇒ Object
Execute the block with retry/backoff for transient errors.
Instance Method Details
#build_jsonrpc_notification(method, params) ⇒ Hash
Build a JSON-RPC notification object (no response expected)
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
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_params ⇒ Hash
Generate initialization parameters for MCP protocol
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 |
#ping ⇒ Hash
Ping the server to keep the connection alive
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
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
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 |