Module: MCPClient::ServerSSE::JsonRpcTransport
- Includes:
- JsonRpcCommon, OriginPolicy
- Included in:
- MCPClient::ServerSSE
- Defined in:
- lib/mcp_client/server_sse/json_rpc_transport.rb
Overview
JSON-RPC request/notification plumbing for SSE transport
Constant Summary
Constants included from JsonRpcCommon
JsonRpcCommon::NON_IDEMPOTENT_METHODS
Instance Method Summary collapse
-
#rpc_notify(method, params = {}) ⇒ void
Send a JSON-RPC notification (no response expected).
-
#rpc_request(method, params = {}, timeout: nil) ⇒ Object
Generic JSON-RPC request: send method with params and return result.
-
#send_cancellation_notification(request_id) ⇒ void
Best-effort notifications/cancelled for a request the client stopped waiting on.
Methods included from JsonRpcCommon
#build_jsonrpc_notification, #build_jsonrpc_request, #build_named_request_params, #cancellable_request?, #client_capabilities, #client_info_payload, #declare_sampling_tools, #describe_body_size, #describe_jsonrpc_message, #describe_parse_error, #initialization_params, #ping, #process_jsonrpc_response, #registered_callback?, #sampling_tools_supported?, #split_request_meta, #validate_protocol_version!, #with_retry
Methods included from OriginPolicy
#origin_of, #reject_cross_origin_redirect!, #same_origin?
Instance Method Details
#rpc_notify(method, params = {}) ⇒ void
This method returns an undefined value.
Send a JSON-RPC notification (no response expected)
55 56 57 58 59 60 61 |
# File 'lib/mcp_client/server_sse/json_rpc_transport.rb', line 55 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.}" end |
#rpc_request(method, params = {}, timeout: nil) ⇒ Object
Generic JSON-RPC request: send method with params and return result
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mcp_client/server_sse/json_rpc_transport.rb', line 22 def rpc_request(method, params = {}, timeout: nil) ensure_initialized with_retry(method) 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.
43 44 45 46 47 48 49 |
# File 'lib/mcp_client/server_sse/json_rpc_transport.rb', line 43 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.}") end |