arizona_jsonrpc (arizona v0.4.0)

View Source

JSON-RPC 2.0 envelope codec.

Pure, transport-free helpers shared by the MCP transport (arizona_mcp_handler): decode a request body into a normalized message, and build the success / error response objects.

A decoded message is #{method := binary(), params := map(), id := id()}. An absent (or null) id decodes to undefined, marking a notification -- a message the server must never reply to. A present id that is neither a string nor an integer (MCP's two request-id types) is an invalid request, not a notification, so its sender still gets an answer.

Only single messages are decoded; a top-level JSON array (a JSON-RPC batch) decodes to {error, invalid_request}. Batch support was dropped from the MCP protocol, so a single-message codec is current.

Error codes follow the JSON-RPC 2.0 spec:

CodeMeaning
-32700Parse error -- invalid JSON
-32600Invalid Request -- not a well-formed request object
-32601Method not found
-32602Invalid params
-32603Internal error

Summary

Functions

Decode a request body into a normalized JSON-RPC message.

Build a JSON-RPC error response object (no data member).

Build a JSON-RPC notification object -- a request with no id, which the peer never answers. Used for server-initiated notifications/* messages.

Build a JSON-RPC success response object.

Types

id()

-nominal id() :: binary() | integer() | null.

request()

-nominal request() :: #{method := binary(), params := map(), id := id() | undefined}.

Functions

decode(Body)

-spec decode(Body) -> {ok, request()} | {error, parse_error | invalid_request} when Body :: iodata().

Decode a request body into a normalized JSON-RPC message.

Returns {error, parse_error} for invalid JSON, or {error, invalid_request} for valid JSON that is not a well-formed JSON-RPC 2.0 request object (wrong/absent jsonrpc version, a non-binary or absent method, a non-object params, a present id that is neither a string nor an integer, or a top-level array).

error(Id, Code, Message)

-spec error(id(), Code, Message) -> map() when Code :: integer(), Message :: binary().

Build a JSON-RPC error response object (no data member).

notification(Method, Params)

-spec notification(Method, Params) -> map() when Method :: binary(), Params :: map().

Build a JSON-RPC notification object -- a request with no id, which the peer never answers. Used for server-initiated notifications/* messages.

result(Id, Result)

-spec result(id(), Result) -> map() when Result :: term().

Build a JSON-RPC success response object.