JSON-RPC API Reference¶
Auto-generated API documentation for the JSON-RPC module.
jsonrpc
¶
JSON-RPC 2.0 -- Zero-dependency Python implementation.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
A pure-stdlib implementation of the JSON-RPC 2.0 specification (https://www.jsonrpc.org/specification). Provides core data types, an exception hierarchy, an auto-incrementing ID generator, a method dispatcher, and an async transport for newline-delimited JSON streams.
Requires
Python >= 3.10, no external packages.
Sections
- Constants - protocol version and standard error codes
- Core Types - JSONRPCError, JSONRPCRequest, JSONRPCResponse
- Exception - JSONRPCException wrapping JSONRPCError
- ID Generation - thread-safe auto-incrementing counter
- Dispatcher - method routing with streaming support
- Async Transport - newline-delimited JSON-RPC over asyncio streams
JSONRPCError
dataclass
¶
A JSON-RPC 2.0 error object.
Attributes:
| Name | Type | Description |
|---|---|---|
code |
int
|
Numeric error code. |
message |
str
|
Human-readable error message. |
data |
Any
|
Optional additional error data. |
Source code in jsonrpc/jsonrpc.py
JSONRPCRequest
dataclass
¶
A JSON-RPC 2.0 request object.
Attributes:
| Name | Type | Description |
|---|---|---|
method |
str
|
The RPC method name. |
params |
dict[str, Any] | None
|
Method parameters. |
id |
Union[str, int, None]
|
Request identifier ( |
jsonrpc |
str
|
Protocol version (always |
Source code in jsonrpc/jsonrpc.py
JSONRPCResponse
dataclass
¶
A JSON-RPC 2.0 response object.
Exactly one of result or error should be set.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
Union[str, int, None]
|
Matching request identifier. |
result |
Any
|
Successful result payload. |
error |
JSONRPCError | None
|
Error details on failure. |
jsonrpc |
str
|
Protocol version (always |
Source code in jsonrpc/jsonrpc.py
to_dict()
¶
Serialize to a dictionary.
from_dict(d)
classmethod
¶
Deserialize from a dictionary.
Source code in jsonrpc/jsonrpc.py
success(request_id, result)
classmethod
¶
from_error(request_id, error)
classmethod
¶
Create an error response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_id
|
Any
|
The id of the original request. |
required |
error
|
Any
|
A |
required |
Source code in jsonrpc/jsonrpc.py
JSONRPCException
¶
Bases: Exception
Exception wrapper around a JSONRPCError data object.
Attributes:
| Name | Type | Description |
|---|---|---|
error |
The underlying |
Source code in jsonrpc/jsonrpc.py
JSONRPCDispatcher
¶
Routes JSON-RPC method calls to registered handler functions.
Example::
dispatcher = JSONRPCDispatcher()
@dispatcher.register("SendMessage")
def handle_send(params):
...
return result_dict
Source code in jsonrpc/jsonrpc.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
register(method)
¶
Decorator to register a handler for method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
JSON-RPC method name (e.g. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[MethodHandler], MethodHandler]
|
The original handler function, unmodified. |
Source code in jsonrpc/jsonrpc.py
dispatch(request)
¶
Dispatch a parsed JSON-RPC request to the appropriate handler.
Catches JSONRPCException (and subclasses) raised by handlers and
converts them to error responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
JSONRPCRequest
|
The parsed JSON-RPC request. |
required |
Returns:
| Type | Description |
|---|---|
Union[JSONRPCResponse, Iterator[JSONRPCResponse]]
|
A single |
Union[JSONRPCResponse, Iterator[JSONRPCResponse]]
|
generator yielding |
Source code in jsonrpc/jsonrpc.py
JSONRPCTransport
¶
Async JSON-RPC 2.0 transport over newline-delimited JSON streams.
Each message is a single JSON object terminated by \n.
Messages must not contain embedded newlines.
Attributes:
| Name | Type | Description |
|---|---|---|
reader |
Async stream to read incoming messages from. |
|
writer |
Async stream to write outgoing messages to. |
Source code in jsonrpc/jsonrpc.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | |
is_closed
property
¶
Whether the writer has been closed.
read_message()
async
¶
Read the next JSON-RPC message.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
Parsed JSON object, or |
Source code in jsonrpc/jsonrpc.py
write_message(msg)
async
¶
Write a JSON-RPC message followed by a newline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
dict[str, Any]
|
JSON-serializable dictionary to send. |
required |
Source code in jsonrpc/jsonrpc.py
send_request(method, params=None, req_id=None)
async
¶
Build and send a JSON-RPC request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
The RPC method name. |
required |
params
|
Any
|
Parameters for the method. |
None
|
req_id
|
Union[int, str, None]
|
Optional explicit request id. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The message dictionary that was sent. |
Source code in jsonrpc/jsonrpc.py
send_notification(method, params=None)
async
¶
Build and send a JSON-RPC notification (no id).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
The RPC method name. |
required |
params
|
Any
|
Parameters for the notification. |
None
|
Source code in jsonrpc/jsonrpc.py
send_result(req_id, result)
async
¶
Send a JSON-RPC success response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
req_id
|
Union[int, str]
|
The id of the original request. |
required |
result
|
Any
|
The result payload. |
required |
Source code in jsonrpc/jsonrpc.py
send_error(req_id, error)
async
¶
Send a JSON-RPC error response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
req_id
|
Union[int, str, None]
|
The id of the original request (may be |
required |
error
|
JSONRPCError
|
The error object. |
required |