ZigJR - JSON-RPC 2.0 Library for Zig

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.
11 Likes

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.

2 Likes

Nice library. I really like the API design!

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.

1 Like

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.

2 Likes

I just did a post on doing dynamic dispatching with compile time reflection in this library.

Very nice, makes me want to write a Zig version of emacs-lsp-booster (Rust)

Even Emacs native parser arguably (Reddit) beats it now, so it would be an interesting comparison both in terms of your library and Zig’s overall I/O

Release 1.1 has been released, with the following changes.

  • Universal message handling.
    • Message-based parsing, for both request and response.
    • Message-based execution via rpc_pipeline.runMessage(), for both request and response.
    • Message-based streaming, handling both request and response in the stream.
  • RpcRegistry supports extended handlers: pre-dispatch, post-dispatch, and fallback handler.
  • Fallback handler for handling any unregistered request methods.
  • RpcRequest.parseRpcRequestOwned() to handle memory ownership of passed in JSON string.
  • RpcResponse.parseRpcResponseOwned() to handle memory ownership of passed in JSON string.
  • Remove error union from the return value of response.parseRpcResponse().
  • Add readHttpHeaders() to parse all HTTP-style headers, not just Content-Length.
  • Add a LSP client example.

The README has been updated with the new changes. https://github.com/williamw520/zigjr

1 Like

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.

1 Like

Hi, thanks for the release, I was just looking for a libs to build a LSP so I’m gonna give yours a go :slight_smile:

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.