Skip to main content

Simple, yet solid - Type-safe JSON-RPC 1.0/2.0 with OpenAPI support

Project description

python-jsonrpc-lib

Simple, yet solid. JSON-RPC 1.0/2.0 for Python.

JSON-RPC is a small protocol: a method name, some parameters, a result. python-jsonrpc-lib keeps it that way. You write ordinary Python functions and dataclasses; the library handles validation, routing, error responses, and API documentation. No framework lock-in, no external dependencies, no boilerplate.

Install

pip install python-jsonrpc-lib

Quickstart

Define methods as classes with typed parameters. The library validates inputs, routes calls, and builds responses automatically.

from dataclasses import dataclass
from jsonrpc import JSONRPC, Method, MethodGroup

@dataclass
class AddParams:
    a: int
    b: int

class Add(Method):
    def execute(self, params: AddParams) -> int:
        return params.a + params.b

@dataclass
class GreetParams:
    name: str
    greeting: str = 'Hello'

class Greet(Method):
    def execute(self, params: GreetParams) -> str:
        return f'{params.greeting}, {params.name}!'

rpc = JSONRPC(version='2.0')
rpc.register('add', Add())
rpc.register('greet', Greet())

response = rpc.handle('{"jsonrpc": "2.0", "method": "add", "params": {"a": 5, "b": 3}, "id": 1}')
# '{"jsonrpc": "2.0", "result": 8, "id": 1}'

Pass in a JSON string, get a JSON string back. What carries it over the wire is up to you.

If a is "five" instead of 5, the caller receives a -32602 Invalid params error immediately — no exception handling on your end.

The same AddParams dataclass drives validation, IDE autocomplete, and the OpenAPI schema.

Why python-jsonrpc-lib?

  • Zero dependencies — pure Python 3.11+. Nothing to pin, nothing to audit beyond the library itself.
  • Type validation from dataclasses — declare parameters as a dataclass, get automatic validation and clear error messages for free.
  • OpenAPI docs auto-generated — type hints and docstrings you already wrote become a full OpenAPI 3.0 spec. Point any Swagger-compatible UI at it and your API is self-documented.
  • Transport-agnosticrpc.handle(json_string) returns a string. HTTP, WebSocket, TCP, message queue: your choice.
  • Spec-compliant by default — v1.0 and v2.0 rules enforced out of the box, configurable when you need to support legacy clients.

Namespacing and Middleware

Use MethodGroup to organize methods into namespaces and add cross-cutting concerns:

math = MethodGroup()
math.register('add', Add())

rpc = JSONRPC(version='2.0')
rpc.register('math', math)

# "math.add" is now available

Quick Prototyping

For scripts and throwaway code, the @rpc.method decorator registers functions directly (v2.0 only):

rpc = JSONRPC(version='2.0')

@rpc.method
def add(a: int, b: int) -> int:
    return a + b

For production use, prefer Method classes — they support context, middleware, and groups.

Documentation

Full documentation with tutorials, integration guides, and API reference:

Claude Code Integration

If you use Claude Code, a skill for this library is available. It gives Claude built-in knowledge of jsonrpc-lib's API: creating methods, registering them, organizing with groups, handling errors, and adding context and middleware — without having to look up docs.

To use it, add the skill file to your project's .claude/skills/ directory.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_jsonrpc_lib-0.3.1.tar.gz (96.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_jsonrpc_lib-0.3.1-py3-none-any.whl (28.6 kB view details)

Uploaded Python 3

File details

Details for the file python_jsonrpc_lib-0.3.1.tar.gz.

File metadata

  • Download URL: python_jsonrpc_lib-0.3.1.tar.gz
  • Upload date:
  • Size: 96.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for python_jsonrpc_lib-0.3.1.tar.gz
Algorithm Hash digest
SHA256 ce0713509ce16c4728f7bc20455db029801a4d6e7578c8d5680e15f92393b270
MD5 8ccf2e25c7342c3d3278cc2be790f5b0
BLAKE2b-256 977a3a5074bb1aa5e9220a0fec0cc1a43dbe760cab857dd93d09eed5cb7ddcd6

See more details on using hashes here.

File details

Details for the file python_jsonrpc_lib-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_jsonrpc_lib-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dc68f224785a9f79f8652a669ce5b0be1927e72bf7fe95deda380002bc63a1a9
MD5 c405c745320967badb1cc0a5097a8550
BLAKE2b-256 7fb6e4c51c7d77adb496cc64aa2407705afa325dcf3eea7cd001d2d8b87e9320

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page