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 MCP client (#130/#131), 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 ¶
const Version = "2.0"
Version is the only JSON-RPC protocol version this package speaks.
Variables ¶
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 ¶
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 ¶
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.
type Config ¶
type Config struct {
// 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.
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 (ID) MarshalJSON ¶
MarshalJSON emits the id as its underlying JSON scalar (number or string).
func (*ID) UnmarshalJSON ¶
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).