jsonrpc2

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 19 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

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrParse          = &Error{Code: -32700, Message: "parse error"}
	ErrInvalidRequest = &Error{Code: -32600, Message: "invalid request"}
	ErrMethodNotFound = &Error{Code: -32601, Message: "method not found"}
	ErrInvalidParams  = &Error{Code: -32602, Message: "invalid params"}
	ErrInternal       = &Error{Code: -32603, Message: "internal error"}
)

Standard JSON-RPC 2.0 error codes.

Functions

This section is empty.

Types

type Client

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

Client is a minimal JSON-RPC 2.0 client for stdio transport.

func NewClient

func NewClient(stdin io.WriteCloser, stdout io.ReadCloser) *Client

NewClient creates a new JSON-RPC client.

func (*Client) Close

func (c *Client) Close()

Close stops the client like Stop without waiting for the read loop, which exits once the handler it is running returns. It cancels in-flight request handlers and closes ConnectionClosed now. The close callback does not run afterwards; if the read loop had already started it, Close waits for it, so the callback's cleanup cannot reach whatever the caller sets up next.

func (*Client) ConnectionClosed

func (c *Client) ConnectionClosed() <-chan struct{}

ConnectionClosed returns a channel that closes when the JSON-RPC read loop exits or the client is stopped.

func (*Client) Request

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

Request sends a JSON-RPC request and waits for the response

func (*Client) RequestWithInlineResponse

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

RequestWithInlineResponse sends a JSON-RPC request and waits for the response, invoking an optional callback synchronously from the read loop the instant a successful response is parsed — before the response is delivered to the awaiter and before the read loop dispatches the next message. Use this when client-side state must be visible (for example, a session id assigned by the server in the response) before any subsequent notification on the same connection is dispatched. If the callback returns an error, that error is returned to the awaiter in place of the response.

func (*Client) SetOnClose

func (c *Client) SetOnClose(fn func())

SetOnClose sets a callback invoked when the read loop exits unexpectedly (e.g. the underlying connection or process was lost).

func (*Client) SetProcessDone

func (c *Client) SetProcessDone(done chan struct{}, errPtr *error)

SetProcessDone sets a channel that will be closed when the process exits, and stores the error pointer that should be returned to pending/future requests. The error is read directly from the pointer after the channel closes, avoiding a race between an async goroutine and callers checking the error.

func (*Client) SetRequestContextHandler

func (c *Client) SetRequestContextHandler(method string, handler RequestContextHandler)

SetRequestContextHandler registers a handler for incoming requests from the server. The supplied context is cancelled when a numeric $/cancelRequest for this request ID is received.

func (*Client) SetRequestHandler

func (c *Client) SetRequestHandler(method string, handler RequestHandler)

SetRequestHandler registers a handler for incoming requests from the server

func (*Client) SetRequestHandlerWithPreparation

func (c *Client) SetRequestHandlerWithPreparation(method string, handler RequestHandler, prepare func(json.RawMessage) *Error)

SetRequestHandlerWithPreparation registers request state in read order before later notifications can run. Preparation must not block or invoke user code.

func (*Client) SetRequestScope

func (c *Client) SetRequestScope(scope func(json.RawMessage) func())

SetRequestScope brackets server-to-client calls with connection-owned admission state. The returned function runs when the handler returns, including during panic unwinding.

func (*Client) Start

func (c *Client) Start()

Start begins listening for messages in a background goroutine

func (*Client) Stop

func (c *Client) Stop()

Stop stops the client and cleans up, waiting for the read loop to finish. A handler running on the read loop must not call it; use Close instead.

type Error

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

Error represents a JSON-RPC error response.

func (*Error) Error

func (e *Error) Error() string

type NotificationHandler

type NotificationHandler func(method string, params json.RawMessage)

NotificationHandler handles incoming notifications

type Request

type Request struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      json.RawMessage `json:"id"` // nil for notifications
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params"`
}

Request represents a JSON-RPC 2.0 request

func (*Request) IsCall

func (r *Request) IsCall() bool

type RequestContextHandler

type RequestContextHandler func(ctx context.Context, params json.RawMessage) (json.RawMessage, *Error)

RequestContextHandler handles incoming server requests with a per-request context that is cancelled by JSON-RPC $/cancelRequest for numeric IDs.

type RequestHandler

type RequestHandler func(params json.RawMessage) (json.RawMessage, *Error)

RequestHandler handles incoming server requests and returns a result or error

func NotificationHandlerFor

func NotificationHandlerFor[In any](handler func(params In)) RequestHandler

func RequestHandlerFor

func RequestHandlerFor[In, Out any](handler func(params In) (Out, *Error)) RequestHandler

RequestHandlerFor creates a RequestHandler from a typed function

type Response

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

Response represents a JSON-RPC 2.0 response

Jump to

Keyboard shortcuts

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