jsonrpc

package
v1.5.30 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jsonrpc implements a minimal JSON-RPC 2.0 client over a subprocess's stdio (US-014/#116). It is the shared transport foundation reused by the plugin system (#132/#133) and process-isolated sub-agents (#135): each spawns an external executable and speaks line-delimited JSON-RPC 2.0 over the child's stdin/stdout.

The wire format follows the spec: every message carries "jsonrpc":"2.0". A request has an id and expects a matching response; a notification omits the id and expects none. Requests and responses are correlated by id, so concurrent requests from different goroutines are safe — each waits only on its own reply.

This file defines the message envelope and (de)serialization; transport.go implements the subprocess client.

This file implements the subprocess JSON-RPC client (US-014/#116). A Client launches an external executable, writes requests to its stdin and reads newline-delimited JSON-RPC messages from its stdout on a background reader goroutine. Requests are correlated to responses by id through a pending-call map, so Call is safe for concurrent use: each caller blocks only on its own response channel until the reply arrives, the context is cancelled, or the child exits.

Index

Constants

View Source
const Version = "2.0"

Version is the only JSON-RPC protocol version this package speaks.

Variables

View Source
var ErrClosed = errors.New("jsonrpc: client closed")

ErrClosed is returned by Call once the client has been closed or the child process has exited.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a JSON-RPC 2.0 client bound to a subprocess over its stdio.

func NewClient

func NewClient(cfg Config) (*Client, error)

NewClient starts the subprocess and begins reading its stdout. The caller must Close the client to terminate the child and release resources.

func (*Client) Call

func (c *Client) Call(ctx context.Context, method string, params any) (json.RawMessage, error)

Call sends a request and waits for the matching response. It returns the decoded Result on success, the server Error as error on an error response, or a transport/context error. Call is safe for concurrent use.

func (*Client) Close

func (c *Client) Close() error

Close closes the child's stdin (signalling graceful shutdown), waits briefly for the process to exit, and kills it if it does not. It is idempotent.

func (*Client) Notify

func (c *Client) Notify(method string, params any) error

Notify sends a notification (no id, no response expected).

type Config

type Config struct {
	// Process optionally supplies a host-prepared, not-yet-started command,
	// including its sandbox, context cancellation and process-group policy.
	Process *exec.Cmd
	// Command is the executable path.
	Command string
	// Args are the process arguments (excluding the command itself).
	Args []string
	// Env is the child's environment (os/exec form: "KEY=value"). When nil the
	// child inherits the parent environment.
	Env []string
	// Dir is the child's working directory; empty means the parent's.
	Dir string
	// Stderr optionally receives the child's stderr (e.g. for logging). When nil
	// the child's stderr is discarded.
	Stderr io.Writer
}

Config describes the subprocess to launch.

type Error

type Error struct {
	Code    int             `json:"code"`
	Message string          `json:"message"`
	Data    json.RawMessage `json:"data,omitempty"`
}

Error is a JSON-RPC error object.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface so a peer error can be returned directly.

type ID

type ID struct {
	// contains filtered or unexported fields
}

ID is a JSON-RPC request identifier. The spec allows a string or a number; this client only ever generates numeric ids, but ID round-trips whatever a peer sends back so response correlation still works against servers that echo string ids.

func NumID

func NumID(n int64) ID

NumID returns a numeric request id.

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

MarshalJSON emits the id as its underlying JSON scalar (number or string).

func (ID) String

func (id ID) String() string

String renders the id for use as a map key when correlating responses.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts either a JSON number or string id.

type Request

type Request struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      *ID             `json:"id,omitempty"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params,omitempty"`
}

Request is an outgoing JSON-RPC request or notification. When ID is nil the message is a notification (no response expected).

type Response

type Response struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      *ID             `json:"id,omitempty"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *Error          `json:"error,omitempty"`
}

Response is an incoming JSON-RPC response. Exactly one of Result / Error is set on a well-formed reply.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL