ZigJR is a lightweight Zig library providing a full implementation of the JSON-RPC 2.0 protocol, with message streaming on top, and a smart function dispatcher that turns native Zig functions into RPC handlers. It aims to make building JSON-RPC applications in Zig simple and straightforward.
This small library is packed with the following features:
Parsing and composing JSON-RPC 2.0 messages.
Support for Request, Response, Notification, and Error JSON-RPC 2.0 messages.
Support for batch requests and batch responses in JSON-RPC 2.0.
Message streaming via delimiter based streams (\n, etc.).
Message streaming via Content-Length header-based streams.
RPC pipeline to process the full request-to-response lifecycle.
Native Zig functions as message handlers with automatic type mapping.
Flexible logging mechanism for inspecting the JSON-RPC messages.
I wanted to write a MCP server in Zig some time ago, but there was no JSON-RPC support in Zig. I ended up building a JSON-RPC library instead. I did put in a MCP server example. It was raw-dogged from scratch, from the MCP message schema directly.
ZigJR is likely perfect for writing language server protocol clients and servers right? You might want to mention that in the README.md and github tags.
Thanks. Yes. It can be used for LSP client or server. The Content-Length based streaming is in anticipation for LSP. May be I’ll put in LSP example for illustration.
I added a LSP example to the project in the form of lsp_client. I figured it’s easier to do a client than dealing with language compiler integration on the server side. ZLS is a great lsp server for testing. See the Run the LSP Client Example section in the README for the LSP messages it can send.
The lsp_client actually helped a lot in flushing out problems in biulding JSON RPC clients with ZigJR. Dogfooding one’s own API is really a requirement for building a library.
That’s excellent. Let me know if any issues come up.
BTW. You might want to look at the LSP message definition file in the zig-lsp-codegen project. The file has LSP messages defined in Zig structs and enums, so you don’t have to hand rolled them from the LSP schema. It is a generated file, a bit hard to find. Do a search for the ‘lsp_types.zig’ file in the .zig-cache after a build and copy it to your project. Also the schema file is ‘metaModel.json’ and it is in the root directory of zig-lsp-codegen.
Edit: Correction. Do a search for ‘lsp_types.zig’ in the .zig-cache after building the ZLS project, which links in the zig-lsp-codegen project.