JSON-RPC 2.0 Over Newline-Delimited Stdio
The transport between a model client and a tool server is JSON-RPC over stdio. Hand-rolling it once teaches you what every framing layer is paying for. Speak JSON-RPC 2.0 framed as newline-delimited JSON over stdin and stdout. Map the five standard error codes (-32700, -32600, -32601, -32602, -32603) and surface them with the right semantics. Distinguish requests, responses, notifications, and batches without inventing new envelope keys. Handle one parse error per line without poisoning the rest of the stream. Build a self-terminating demo using io.BytesIO so the lesson runs without spawning a child process. A coding agent in 2026 talks to maybe twelve tool servers in a single session. Each server is a separate process or a remote endpoint. The wire format has been the same since 2013. JSON-RPC 2.0 is two-page spec. It survives because the alternatives (gRPC, HTTP per call, custom binary) all impose a tradeoff JSON-RPC does not: they pick either streaming or batching or transport-coupling. JSON-RPC is symmetric across stdio, sockets, websockets, and HTTP, and a client can drive a server it has never seen if both honor the spec. This lesson builds the stdio variant. Newline-delimited JSON. Each request is one line. Each response is one line. The transport boundary is \n. Four envelope shapes exist. Two are spoken by the client. Two are spoken…
JSON-RPC 2.0 Over Newline-Delimited Stdio: The transport between a model client and a tool server is JSON-RPC over stdio. Hand-rolling it once teaches you…
This free lesson is part of the AI Engineering from Scratch curriculum. Read the full explanation, run the lesson code, and verify the result in the interactive reader or from the repository source.
Browse the complete course catalog or open this lesson on GitHub.