json-rpc-rs
An async Rust implementation of JSON-RPC 2.0. This library provides a simple, user-friendly API for creating JSON-RPC servers with async/await support, multiple transport options, and full JSON-RPC 2.0 compliance.
Features
- Builder Pattern: Configure methods with a fluent, intuitive API
- Type-Safe Methods: Register methods using closures with automatic parameter deserialization
- Async/Await: Built on tokio for efficient async request handling
- Multiple Transports: Use Stdio, HTTP, or InMemory transports, or implement custom ones
- JSON-RPC 2.0 Compliant: Full support for requests, responses, notifications, and errors
- Batch Requests: Full JSON-RPC 2.0 batch request support
- Error Handling: Simple error creation with JSON-RPC compliant error codes
Installation
Add this to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
Quick Start
Create a simple JSON-RPC server with one method and run it using stdin/stdout:
use ;
use Value;
async
Send a request:
|
Response:
Usage
Creating a Server
Create a server with default configuration:
use ;
let methods = new;
let transport = new;
serve.await?;
Registering Methods
Register methods with tuple parameters:
methods.add;
Register methods with a single parameter:
methods.add;
Register methods with struct parameters:
use Deserialize;
methods.add;
Running the Server
Run with the default Stdio transport:
let transport = new;
serve.await?;
Run with HTTP transport:
use Http;
use SocketAddr;
let addr: SocketAddr = "127.0.0.1:3000".parse.unwrap;
let transport = new;
serve.await?;
Error Handling
Return protocol errors:
use Error;
methods.add;
Return JSON-RPC standard errors:
methods.add;
Batch Requests
The library handles batch requests automatically. Since the stdio transport uses NDJSON (newline-delimited JSON), batch request arrays must be on a single line:
|
Response:
Transports
The library provides three built-in transports:
- Stdio: NDJSON (newline-delimited JSON) over stdin/stdout. This is the default and ideal for local servers.
- HTTP: HTTP POST requests using axum web framework. Perfect for web-based APIs.
- InMemory: In-memory transport for testing and in-process communication.
Implement custom transports by implementing the
Transport trait. See the
transport module for examples.
Stdio Transport
The Stdio transport reads newline-delimited JSON from stdin and writes newline-terminated JSON to stdout:
use Stdio;
let transport = new;
serve.await?;
HTTP Transport
The HTTP transport accepts POST requests at /jsonrpc:
use Http;
use SocketAddr;
let addr: SocketAddr = "127.0.0.1:3000".parse.unwrap;
let transport = new;
serve.await?;
Send requests using curl:
InMemory Transport
The InMemory transport is useful for testing:
use InMemory;
let = unconnected;
spawn;
sender.send.await?;
Limitations
- This library requires the tokio async runtime
- The default Stdio transport works with NDJSON (newline-delimited JSON) only
- This library is designed for local JSON-RPC servers, not distributed systems
- Custom transports require implementing the
Transporttrait
JSON-RPC 2.0 Compliance
This library implements the JSON-RPC 2.0 specification, including:
- Request objects with
jsonrpc,method,params, andidfields - Notification objects (requests without
id) - Response objects with
resultorerrorfields - Error objects with
code,message, and optionaldatafields - Batch requests (arrays of requests)
- Standard error codes (-32700 to -32099)
See JSON-RPC 2.0 Specification for details.
Examples
The examples directory contains working examples:
echo_stdio_server.rs: Simple echo server using stdioecho_http_server.rs: Simple echo server using HTTPbasic_stdio_server.rs: Demonstrates error handling with stdiobasic_http_server.rs: Demonstrates error handling with HTTP
Run an example:
# Stdio examples (pipe JSON to stdin)
|
# HTTP examples (start server, then use curl)
Documentation
Generate and view API documentation:
License
MIT