Skip to content

jsonrpc

Pure JSON-RPC 2.0 Protocol Implementation

This module provides a pure implementation of JSON-RPC 2.0 types and structures optimized for WebAssembly environments. It follows the JSON-RPC 2.0 specification exactly without any agent-specific extensions.

JSON-RPC 2.0 is a stateless, light-weight remote procedure call (RPC) protocol. It defines two types of messages:

Have an id field and expect a response:

{
"jsonrpc": "2.0",
"id": "request-123",
"method": "ping",
"params": {"timestamp": "2024-01-01T00:00:00Z"}
}

No id field, fire-and-forget:

{
"jsonrpc": "2.0",
"method": "log.info",
"params": {"message": "Task completed", "level": "info"}
}
const JSONRPC_VERSION: &str = ;

JSON-RPC 2.0 version constant

type JsonRpcId = serde_json::Value;

JSON-RPC 2.0 request ID type

Can be a string, number, or null according to the JSON-RPC 2.0 specification. The ID is used to correlate requests with responses and must be echoed back in the response exactly as received.

  • String: "request-123", "abc-def-456"
  • Number: 1, 42, 1234567890
  • Null: null (though discouraged for traceability)

JSON-RPC 2.0 Request structure

Represents a JSON-RPC 2.0 request message that expects a response. All requests must include the jsonrpc, id, and method fields. The params field is optional and defaults to null.

Fields

FieldTypeDescription
jsonrpcStringJSON-RPC version (must be “2.0”)
idJsonRpcIdRequest ID (string, number, or null)
methodStringMethod name to invoke
paramsserde_json::ValueMethod parameters (optional, defaults to null)

Methods

fn new(id: JsonRpcId, method: String, params: serde_json::Value) -> Self

Create a new JSON-RPC 2.0 request

fn is_valid(&self) -> bool

Check if this is a valid JSON-RPC 2.0 request

JSON-RPC 2.0 Notification structure

Represents a JSON-RPC 2.0 notification message that does not expect a response. Notifications are “fire-and-forget” messages used for operations where the caller doesn’t need to know the result.

Fields

FieldTypeDescription
jsonrpcStringJSON-RPC version (must be “2.0”)
methodStringMethod name to invoke
paramsserde_json::ValueMethod parameters (optional, defaults to null)

Methods

fn new(method: String, params: serde_json::Value) -> Self

Create a new JSON-RPC 2.0 notification

fn is_valid(&self) -> bool

Check if this is a valid JSON-RPC 2.0 notification

JSON-RPC 2.0 Response structure

Represents a JSON-RPC 2.0 response message sent back to the client. A response must contain either a result (for success) or an error (for failure), but never both. The id field must match the request ID.

Fields

FieldTypeDescription
jsonrpcStringJSON-RPC version (always “2.0”)
resultOption<serde_json::Value>Successful result (mutually exclusive with error)
errorOption<JsonRpcError>Error object (mutually exclusive with result)
idJsonRpcIdRequest ID (copied from request)

Methods

fn success(id: JsonRpcId, result: serde_json::Value) -> Self

Create a successful JSON-RPC 2.0 response

fn error(id: JsonRpcId, code: i64, message: String) -> Self

Create an error JSON-RPC 2.0 response

fn error_with_data(id: JsonRpcId, code: i64, message: String, data: serde_json::Value) -> Self

Create an error JSON-RPC 2.0 response with additional data

fn is_success(&self) -> bool

Check if this is a success response

fn is_error(&self) -> bool

Check if this is an error response

JSON-RPC 2.0 Error Object structure

Represents detailed error information in JSON-RPC 2.0 error responses. Includes a standard error code, human-readable message, and optional additional data for debugging and error handling.

Fields

FieldTypeDescription
codei64Error code (standard or application-defined)
messageStringError message
dataOption<serde_json::Value>Optional additional error data

Methods

fn new(code: i64, message: String) -> Self

Create a new JSON-RPC 2.0 error

fn with_data(code: i64, message: String, data: serde_json::Value) -> Self

Create a new JSON-RPC 2.0 error with additional data

Incoming JSON-RPC message types

Represents all possible incoming JSON-RPC 2.0 messages that can be received and parsed. The enum uses serde’s untagged feature to automatically detect the message type based on the presence of the id field.

  • Request: Has id field - expects a response
  • Notification: No id field - fire-and-forget
  • Batch: Array of requests/notifications (optional feature)

Variants

VariantDescription
Request(JsonRpcRequest)Standard JSON-RPC 2.0 request with ID
Notification(JsonRpcNotification)JSON-RPC 2.0 notification (no ID, no response expected)