JSON-RPC 2.0 message encoding/decoding for the Model Context Protocol.
Handles both client-side (requests, notifications) and server-side (responses, error responses) message construction. All MCP communication flows through this module.
Summary
Functions
Decode a JSON string into a message map with atom keys for known fields.
Encode a message to a JSON string with newline delimiter.
Encode a message, raising on failure.
Check if a decoded message is an error response.
Build a JSON-RPC error response.
JSON-RPC internal error code (-32603).
JSON-RPC invalid params code (-32602).
JSON-RPC invalid request code (-32600).
MCP protocol version string.
JSON-RPC method not found code (-32601).
Build a JSON-RPC notification (no id, no response expected).
Check if a decoded message is a notification (no id).
JSON-RPC parse error code (-32700).
Build a JSON-RPC request.
Check if a decoded message is a request (has id + method).
Build a JSON-RPC success response.
Check if a decoded message is a response (has id + result or error).
Types
@type error_response() :: %{ jsonrpc: String.t(), id: pos_integer() | nil, error: %{code: integer(), message: String.t()} }
@type request() :: %{ jsonrpc: String.t(), id: pos_integer(), method: String.t(), params: map() }
@type response() :: %{jsonrpc: String.t(), id: pos_integer(), result: term()}
Functions
Decode a JSON string into a message map with atom keys for known fields.
Encode a message to a JSON string with newline delimiter.
Encode a message, raising on failure.
Returns iodata (JSON + newline).
Check if a decoded message is an error response.
@spec error_response(pos_integer() | nil, integer(), String.t(), term()) :: error_response()
Build a JSON-RPC error response.
@spec internal_error() :: integer()
JSON-RPC internal error code (-32603).
@spec invalid_params() :: integer()
JSON-RPC invalid params code (-32602).
@spec invalid_request() :: integer()
JSON-RPC invalid request code (-32600).
@spec mcp_protocol_version() :: String.t()
MCP protocol version string.
@spec method_not_found() :: integer()
JSON-RPC method not found code (-32601).
@spec notification(String.t(), map()) :: notification()
Build a JSON-RPC notification (no id, no response expected).
Check if a decoded message is a notification (no id).
@spec parse_error() :: integer()
JSON-RPC parse error code (-32700).
@spec request(pos_integer(), String.t(), map()) :: request()
Build a JSON-RPC request.
Check if a decoded message is a request (has id + method).
@spec response(pos_integer(), term()) :: response()
Build a JSON-RPC success response.
Check if a decoded message is a response (has id + result or error).