python-jsonrpc-server

raw JSON →
0.4.0 verified Fri May 01 auth: no python

A JSON RPC 2.0 server library for Python, used primarily by language servers like the Python Language Server. Current version 0.4.0, with infrequent updates. Supports concurrent request handling via futures.

pip install python-jsonrpc-server
error ModuleNotFoundError: No module named 'ujson'
cause On Windows or Python installations without ujson, the library fails to import ujson.
fix
Install ujson: pip install ujson. If you cannot install it, pin to version 0.2.0 or patch the code to fall back to json.
error AttributeError: module 'jsonrpc' has no attribute 'manager'
cause Incorrect import path. Many users try from jsonrpc import manager or from jsonrpc.manager import ..., but the module is likely not installed or the import is misspelled.
fix
Use correct import: from jsonrpc.manager import JSONRPC2Protocol
breaking In version 0.3.0, the built-in `json` module was replaced with `ujson`. This may cause compatibility issues if your code depends on custom JSON encoders/decoders or uses different JSON library features. `ujson` is not available by default on Windows; install it separately or use a workaround.
fix If you need the standard json module, pin to version 0.2.0 or manually monkey-patch. On Windows, install ujson with pip install ujson.
gotcha The library's protocol expects a specific JSON RPC 2.0 format. If you send malformed requests (e.g., missing 'jsonrpc' field or invalid id), the server may hang or raise cryptic errors.
fix Ensure your JSON RPC messages strictly follow the spec: include 'jsonrpc':'2.0', 'method', 'id' (or null for notifications), and proper 'params'.

Basic JSON RPC 2.0 server that reads from stdin and writes to stdout, handling a 'hello' method.

import asyncio
from jsonrpc.server import JSONRPC2Server
from jsonrpc.manager import JSONRPC2Protocol

async def handle_request(protocol, data):
    request = protocol.parse_request(data)
    if request.method == "hello":
        response = protocol.create_response(request, "world")
        return response.serialize()
    else:
        error = protocol.create_error_response(request, -32601, "Method not found")
        return error.serialize()

server = JSONRPC2Server(handle_request)
asyncio.run(server.run())
print("Server running on stdin/stdout")