JSON-RPC 2.0 client for HTTP transports. Supports standard request/response and SSE streaming.

Functions

notification

fn (method: Str, params: Map?): ::json-rpc::types/Notification

Build a JSON-RPC 2.0 notification (fire-and-forget, no response expected).

Unlike requests, notifications have no id field and the server does not send a response.

Example

::rpc ::json-rpc::client
notif ::rpc/notification("notifications/initialized", null)
notif.jsonrpc // => "2.0"
notif.method  // => "notifications/initialized"

notify

fn (url: Str, headers: Map, notif: ::json-rpc::types/Notification): Bool

Send a JSON-RPC notification over HTTP (fire-and-forget, no response expected).

Returns true if the HTTP request was successful. The server does not send a response body for notifications.

Example

::rpc ::json-rpc::client
notif ::rpc/notification("notifications/initialized", null)
headers {"Mcp-Protocol-Version": "2025-11-25"}
result ::rpc/notify("https://server.example.com/rpc", headers, notif)
// result => true

parse-http-response

fn (response: Result): ::json-rpc::types/Response

Parse a full HttpResponse as JSON-RPC.

When the body contains a JSON-RPC response, its result or error is preserved regardless of the HTTP status. Non-JSON-RPC HTTP failures become an internal JSON-RPC client error whose data contains the response body.

Example

::rpc ::json-rpc::client
::http ::hot::http
ok-body {jsonrpc: "2.0", id: "1", result: {tools: []}}
::rpc/parse-http-response(::http/HttpResponse({status: 200, headers: {}, body: ok-body})).result  // {tools: []}

// Without a JSON-RPC body, a failed status becomes an Err
failed ::rpc/parse-http-response(::http/HttpResponse({status: 503, headers: {}, body: "down"}))
err-value(failed).message  // "HTTP 503"

parse-response

fn (data: Map): ::json-rpc::types/Response

Parse a raw response map into a typed Response.

Returns err() if the response carries a JSON-RPC error object. Successful responses have a result field; error responses trigger automatic error propagation via Hot's Result system.

Example

::rpc ::json-rpc::client

// Successful response
raw {jsonrpc: "2.0", id: "abc-123", result: {tools: []}}
response ::rpc/parse-response(raw)
response.result // => {tools: []}

// Error response returns err()
raw-err {jsonrpc: "2.0", id: "abc-123", error: {code: -32601, message: "Method not found"}}
result ::rpc/parse-response(raw-err)
is-err(result) // => true

request

fn (method: Str, params: Map?): ::json-rpc::types/Request

Build a JSON-RPC 2.0 request message with an auto-generated UUIDv7 id.

Each request gets a unique id for response correlation. Pass null for params if the method takes no arguments.

Example

::rpc ::json-rpc::client
req ::rpc/request("tools/list", {})
req.jsonrpc // => "2.0"
req.id      // => auto-generated UUIDv7
req.method  // => "tools/list"

// With null params
req ::rpc/request("ping", null)

send

fn (url: Str, headers: Map, req: ::json-rpc::types/Request): ::json-rpc::types/Response

Send a JSON-RPC request over HTTP and return the parsed response.

Posts the request as JSON and parses the response. Returns err() for HTTP errors or JSON-RPC error responses. Pass additional headers as the second argument (e.g. session or auth headers).

Example

::rpc ::json-rpc::client
url "https://server.example.com/rpc"
req ::rpc/request("initialize", {
  protocolVersion: "2025-11-25",
  capabilities: {},
  clientInfo: {name: "my-app", version: "1.0.0"}
})
response ::rpc/send(url, {}, req)
response.result.serverInfo // => {name: "...", version: "..."}

// With session headers
headers {"Mcp-Protocol-Version": "2025-11-25"}
response ::rpc/send(url, headers, req)

send-raw

fn (url: Str, headers: Map, req: ::json-rpc::types/Request): ::hot::http/HttpResponse

Send a JSON-RPC request over HTTP and return the full HttpResponse.

Unlike send, this function does not interpret HTTP status codes or JSON-RPC errors. Use it when transport headers or status-aware negotiation are needed.

Example

::rpc ::json-rpc::client
req ::rpc/request("initialize", {
  protocolVersion: "2025-11-25",
  capabilities: {},
  clientInfo: {name: "my-app", version: "1.0.0"}
})
response ::rpc/send-raw("https://server.example.com/rpc", {}, req)
response.status
session get(response.headers, "mcp-session-id")
parsed ::rpc/parse-http-response(response)
parsed.result.serverInfo

send-stream

fn (url: Str, headers: Map, req: ::json-rpc::types/Request): ::hot::http/StreamingHttpResponse

Send a JSON-RPC request expecting an SSE stream response.

Returns a StreamingHttpResponse whose .body is an iterator yielding parsed SSE events with .event, .data, and .id fields. Use collect to gather all events or iterate to process in real time.

Example

::rpc ::json-rpc::client
req ::rpc/request("tools/call", {name: "long_task", arguments: {}})
response ::rpc/send-stream("https://server.example.com/rpc", {}, req)

// Collect all SSE events
events collect(response.body)
// Each event => {event: "message", data: "<json-rpc-json>", id: "..."}

sse-json-rpc-response

fn (body: Str): Map?

Extract the final JSON-RPC result or error from a buffered SSE response body.

Example

::rpc ::json-rpc::client
body "event: message\ndata: {\"jsonrpc\":\"2.0\",\"id\":\"1\",\"result\":{\"ok\":true}}\n\n"
::rpc/sse-json-rpc-response(body).result  // {ok: true}
::rpc/sse-json-rpc-response(": keep-alive\n\n")  // null