Module: MCPClient::ServerSSE::JsonRpcTransport

Includes:
JsonRpcCommon
Included in:
MCPClient::ServerSSE
Defined in:
lib/mcp_client/server_sse/json_rpc_transport.rb

Overview

JSON-RPC request/notification plumbing for SSE transport

Instance Method Summary collapse

Methods included from JsonRpcCommon

#build_jsonrpc_notification, #build_jsonrpc_request, #build_named_request_params, #cancellable_request?, #client_capabilities, #client_info_payload, #declare_sampling_tools, #initialization_params, #ping, #process_jsonrpc_response, #registered_callback?, #sampling_tools_supported?, #split_request_meta, #validate_protocol_version!, #with_retry

Instance Method Details

#rpc_notify(method, params = {}) ⇒ void

This method returns an undefined value.

Send a JSON-RPC notification (no response expected)

Parameters:

  • method (String)

    JSON-RPC method name

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

    parameters for the notification



52
53
54
55
56
57
58
# File 'lib/mcp_client/server_sse/json_rpc_transport.rb', line 52

def rpc_notify(method, params = {})
  ensure_initialized
  notif = build_jsonrpc_notification(method, params)
  post_json_rpc_request(notif)
rescue MCPClient::Errors::ServerError, MCPClient::Errors::ConnectionError, Faraday::ConnectionFailed => e
  raise MCPClient::Errors::TransportError, "Failed to send notification: #{e.message}"
end

#rpc_request(method, params = {}, timeout: nil) ⇒ Object

Generic JSON-RPC request: send method with params and return result

Parameters:

  • method (String)

    JSON-RPC method name

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

    parameters for the request

Returns:

  • (Object)

    result from JSON-RPC response

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mcp_client/server_sse/json_rpc_transport.rb', line 19

def rpc_request(method, params = {}, timeout: nil)
  ensure_initialized

  with_retry do
    request_id = @mutex.synchronize { @request_id += 1 }
    request = build_jsonrpc_request(method, params, request_id)
    begin
      send_jsonrpc_request(request, timeout: timeout)
    rescue MCPClient::Errors::RequestTimeoutError
      # MCP lifecycle: on timeout the sender SHOULD issue a cancellation
      # notification for the abandoned request and stop waiting.
      send_cancellation_notification(request_id) if cancellable_request?(method, params)
      raise
    end
  end
end

#send_cancellation_notification(request_id) ⇒ void

This method returns an undefined value.

Best-effort notifications/cancelled for a request the client stopped waiting on. Failures are swallowed.

Parameters:

  • request_id (Integer)

    id of the abandoned request



40
41
42
43
44
45
46
# File 'lib/mcp_client/server_sse/json_rpc_transport.rb', line 40

def send_cancellation_notification(request_id)
  notif = build_jsonrpc_notification('notifications/cancelled',
                                     { 'requestId' => request_id, 'reason' => 'Request timed out' })
  post_json_rpc_request(notif)
rescue StandardError => e
  @logger.debug("Failed to send cancellation notification: #{e.message}")
end