Skip to main content

JSON-RPC Server for Python

A JSON-RPC 2.0 server in ~200 lines of pure standard library — no dependencies, at install time or at runtime. pip install jsonrpc-server-py pulls in nothing else, and CI asserts that on every push.

That is the whole pitch. If you want middleware, async, or an ecosystem, use jsonrpcserver. If you want a spec-compliant endpoint you can read start to finish and vendor into a project that cannot take on dependencies, use this.

Features

  • Zero dependencies — stdlib http.server and json, nothing more.
  • Spec-compliant error codes: -32700 parse, -32600 invalid request, -32601 method not found, -32602 invalid params, -32603 internal error.
  • Single requests, batches, and notifications (including all-notification batches, which correctly return no body).
  • Threaded by default (ThreadingHTTPServer), so one slow method does not block every other client.
  • Errors travel in the JSON body with HTTP 200, as the spec intends — the transport succeeded even when the call did not.

Refer here for details: https://www.jsonrpc.org/specification

Installation

To install the JSON-RPC Server, simply use pip:

pip install jsonrpc-server-py

Quick Start

Define the methods you want to expose through JSON-RPC, register your methods with the server, and run the server

from jsonrpc_server import register_method, run

def add(a, b):
    return a + b

register_method('add', add)

if __name__ == "__main__":
    run(address='localhost', port=8000)

Example

Request:

{
  "jsonrpc": "2.0",
  "method": "add",
  "params": [1, 2],
  "id": 1
}

Result:

{
  "jsonrpc": "2.0",
  "result": 3,
  "id": 1
}

Batch Requests

Send a JSON array to process multiple calls in one HTTP request. Each call gets a matching response, in order:

[
  {"jsonrpc": "2.0", "method": "add", "params": [1, 2], "id": 1},
  {"jsonrpc": "2.0", "method": "add", "params": [3, 4], "id": 2}
]

Result:

[
  {"jsonrpc": "2.0", "result": 3, "id": 1},
  {"jsonrpc": "2.0", "result": 7, "id": 2}
]

Notifications

Omit id to send a notification: the method still runs, but no response is sent (HTTP 204 No Content). Use this for fire-and-forget calls where you don't need the result:

{"jsonrpc": "2.0", "method": "add", "params": [1, 2]}

A batch made up entirely of notifications also gets no response body.

Limitations

This project favors a small, readable, stdlib-only implementation over feature completeness:

  • No HTTPS - put a reverse proxy (nginx, Caddy) in front if you need TLS.
  • No authentication or authorization - add your own layer if the server is exposed beyond a trusted network.
  • Threaded, not async - each request runs in its own thread (ThreadingHTTPServer), which is enough to avoid one slow call blocking others, but it isn't an async event loop and won't scale to very high concurrency.

Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues to improve the project.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Release files for jsonrpc-server-py 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsonrpc-server-py 0.3.0
File Size Uploaded
jsonrpc_server_py-0.3.0.tar.gz 11.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsonrpc-server-py 0.3.0
File Interpreter ABI Platform
jsonrpc_server_py-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.0 kB

Release files / jsonrpc_server_py-0.3.0.tar.gz

Download URL jsonrpc_server_py-0.3.0.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
36f5119ee08f2ea4dbe865a78d69413b4fa2da3170baef3f5a9ba92593f6c9b8
BLAKE2b-256 checksum
How to use checksums
a50c1f73f4e5e8819f458edcdcc999349c239097ab48267aba74f640f7ced76d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release files / jsonrpc_server_py-0.3.0-py3-none-any.whl

Download URL jsonrpc_server_py-0.3.0-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
536d71e153951ecd21b8305555a6f744734342c1b3dc63eff50a584fbffe1db0
BLAKE2b-256 checksum
How to use checksums
3ca89551b324ee0d165b82ee0aa2e3797e62b2b18310954f884b989e4ad4601d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page