MCPcopy Index your code
hub / github.com/creachadair/jrpc2

github.com/creachadair/jrpc2 @v1.3.5

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.3.5 ↗ · + Follow
470 symbols 1,893 edges 47 files 263 documented · 56% 3 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

jrpc2

GoDoc CI

This repository provides a Go module that implements a JSON-RPC 2.0 client and server. There is also a working example in the Go playground.

Packages

  • Package jrpc2 implements the base client and server and standard error codes.

  • Package channel defines the communication channel abstraction used by the server & client.

  • Package handler defines support for adapting functions to service methods.

  • Package jhttp allows clients and servers to use HTTP as a transport.

  • Package server provides support for running a server to handle multiple connections, and an in-memory implementation for testing.

Versioning

From v1.0.0 onward, the API of this module is considered stable, and I intend to merge no breaking changes to the API without increasing the major version number. Following the conventions of semantic versioning, the minor version will be used to signify the presence of backward-compatible new features (for example, new methods, options, or types), while the patch version will be reserved for bug fixes, documentation updates, and other changes that do not modify the API surface.

Note, however, that this intent is limited to the package APIs as seen by the Go compiler: Changes to the implementation that change observable behaviour in ways not promised by the documentation, e.g., changing performance characteristics or the order of internal operations, are not protected. Breakage that results from reliance on undocumented side-effects of the current implementation are the caller's responsibility.

Implementation Notes

The following describes some of the implementation choices made by this module.

Batch requests and error reporting

The JSON-RPC 2.0 spec is ambiguous about the semantics of batch requests. Specifically, the definition of notifications says:

A Notification is a Request object without an "id" member. ... The Server MUST NOT reply to a Notification, including those that are within a batch request.

Notifications are not confirmable by definition, since they do not have a Response object to be returned. As such, the Client would not be aware of any errors (like e.g. "Invalid params", "Internal error").

This conflicts with the definition of batch requests, which asserts:

A Response object SHOULD exist for each Request object, except that there SHOULD NOT be any Response objects for notifications. ... The Response objects being returned from a batch call MAY be returned in any order within the Array. ... If the batch rpc call itself fails to be recognized as an valid JSON or as an Array with at least one value, the response from the Server MUST be a single Response object.

and includes examples that contain request values with no ID (which are, perforce, notifications) and report errors back to the client. Since order may not be relied upon, and there are no IDs, the client cannot correctly match such responses back to their originating requests.

This implementation resolves the conflict in favour of the batch rules. Specifically:

  • If a batch is empty or not valid JSON, the server reports error -32700 (Invalid JSON) as a single error Response object.

  • Otherwise, parse or validation errors resulting from any batch member without an ID are mapped to error objects with a null ID, in the same position in the reply as the corresponding request. Preservation of order is not required by the specification, but it ensures the server has stable behaviour.

Because a server is allowed to reorder the results, a client should not depend on this implementation detail.

Extension points exported contracts — how you extend this code

Channel (Interface)
A Channel represents the ability to transmit and receive data records. A channel does not interpret the contents of a r [6 …
channel/channel.go
Service (Interface)
Service is the interface used by the Loop function to start up a server. The methods of this interface allow the instanc [2 …
server/loop.go
ErrCoder (Interface)
An ErrCoder is a value that can report an error code value. [1 implementers]
code.go
RPCLogger (Interface)
An RPCLogger receives callbacks from a server to record the receipt of requests and the delivery of responses. These cal [1 …
opts.go
HTTPClient (Interface)
HTTPClient is the interface to an HTTP client used by a Channel. It is compatible with the standard library http.Client [1 …
jhttp/channel.go
Assigner (Interface)
An Assigner maps method names to [Handler] functions.
base.go
Framing (FuncType)
A Framing converts a reader and a writer into a Channel with a particular message-framing discipline.
channel/channel.go
Accepter (Interface)
An Accepter obtains client connections from an external source and constructs channels from them. [2 implementers]
server/loop.go

Core symbols most depended-on inside this repo

New
called by 66
handler/handler.go
Close
called by 49
channel/channel.go
NewLocal
called by 42
server/local.go
Wait
called by 38
server.go
Error
called by 33
error.go
Close
called by 33
server/local.go
Call
called by 31
client.go
Printf
called by 28
opts.go

Shape

Function 209
Method 174
Struct 60
TypeAlias 13
Interface 11
FuncType 3

Languages

Go100%

Modules by API surface

jrpc2_test.go51 symbols
server.go36 symbols
json.go27 symbols
handler/handler_test.go25 symbols
opts.go24 symbols
base.go22 symbols
client.go20 symbols
handler/handler.go19 symbols
server/loop.go16 symbols
jhttp/getter.go16 symbols
tools/jcall/jcall.go15 symbols
jhttp/bridge.go13 symbols

For agents

$ claude mcp add jrpc2 \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page