jsonrpcv2

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package jsonrpcv2 provides minimal types for reading JSON-RPC 2.0 messages on the wire. Generated from the JSON-RPC 2.0 specification (jsonrpcv2.md), this package includes only the types and validation logic needed to parse and log messages for observability purposes. It is NOT a full JSON-RPC client/server implementation - it does not handle method dispatch, parameter validation, or response generation. The types exist solely to enable structured logging of request/response metadata (method names, IDs, error flags) without inspecting or modifying the actual message payloads.

Index

Constants

View Source
const JSONRPCVersion = "2.0"

JSONRPCVersion is the version string for JSON-RPC 2.0

View Source
const ReservedMethodPrefix = "rpc."

ReservedMethodPrefix is the prefix reserved for RPC-internal methods and extensions. Method names beginning with this prefix MUST NOT be used for anything else.

Variables

This section is empty.

Functions

This section is empty.

Types

type Accumulator

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

func (*Accumulator) AccumulateRequest

func (ra *Accumulator) AccumulateRequest(data []byte) (request *Request, err error)

AccumulateRequest buffers incoming data and returns a complete JSON-RPC request when available. MCP messages are line-delimited, so data is accumulated until a complete line is received. Any additional complete lines in the buffer are discarded to maintain protocol synchronization.

NOTE: Use either AccumulateRequest OR AccumulateResponse on a given Accumulator instance, never both. Mixing requests and responses in the same buffer would cause protocol desynchronization since they are different message types.

func (*Accumulator) AccumulateResponse

func (ra *Accumulator) AccumulateResponse(data []byte) (response *Response, err error)

AccumulateResponse buffers incoming data and returns a complete JSON-RPC response when available. MCP messages are line-delimited, so data is accumulated until a complete line is received. Any additional complete lines in the buffer are discarded to maintain protocol synchronization.

NOTE: Use either AccumulateRequest OR AccumulateResponse on a given Accumulator instance, never both. Mixing requests and responses in the same buffer would cause protocol desynchronization since they are different message types.

type Batch

type Batch []Request

Batch represents a JSON-RPC 2.0 batch request. A batch is an array of Request objects. See https://www.jsonrpc.org/specification#batch

func (Batch) Valid

func (b Batch) Valid() bool

Valid checks if the Batch is a valid JSON-RPC 2.0 batch request. It validates that the batch is not empty and each Request is valid.

type BatchResponse

type BatchResponse []Response

BatchResponse represents a JSON-RPC 2.0 batch response. A batch response is an array of Response objects. See https://www.jsonrpc.org/specification#batch

func (BatchResponse) Valid

func (br BatchResponse) Valid() bool

Valid checks if the BatchResponse is a valid JSON-RPC 2.0 batch response. It validates that each Response is valid.

type Error

type Error struct {
	// Code indicates the error type that occurred.
	Code ErrorCode `json:"code"`
	// Message provides a short description of the error.
	Message string `json:"message"`
	// Data contains additional information about the error (optional).
	Data json.RawMessage `json:"data,omitempty"`
}

Error represents a JSON-RPC 2.0 error object. See https://www.jsonrpc.org/specification#error_object

func (Error) Valid

func (e Error) Valid() bool

Valid checks if the Error is a valid JSON-RPC 2.0 error. It validates that the code is set and message is not empty.

type ErrorCode

type ErrorCode int

ErrorCode represents a JSON-RPC error code. See https://www.jsonrpc.org/specification#error_object

const (
	ErrorCodeParseError     ErrorCode = -32700 // Invalid JSON was received
	ErrorCodeInvalidRequest ErrorCode = -32600 // The JSON sent is not a valid Request object
	ErrorCodeMethodNotFound ErrorCode = -32601 // The method does not exist / is not available
	ErrorCodeInvalidParams  ErrorCode = -32602 // Invalid method parameter(s)
	ErrorCodeInternalError  ErrorCode = -32603 // Internal JSON-RPC error
	ErrorCodeServerErrorMin ErrorCode = -32000 // Reserved for implementation-defined server-errors
	ErrorCodeServerErrorMax ErrorCode = -32099
)

Pre-defined error codes as per JSON-RPC 2.0 specification

func (ErrorCode) String

func (e ErrorCode) String() string

String returns the standard message for pre-defined error codes.

type Request

type Request struct {
	// JSONRPC specifies the version of the JSON-RPC protocol. Must be "2.0".
	JSONRPC string `json:"jsonrpc"`
	// Method contains the name of the method to be invoked.
	Method string `json:"method"`
	// Params holds the parameter values to be used during the invocation.
	// This may be an Array (by-position) or an Object (by-name), or omitted.
	Params json.RawMessage `json:"params,omitempty"`
	// ID is an identifier established by the Client.
	// If omitted, the request is a notification and no response is expected.
	// Can be a string, number, or null.
	ID any `json:"id,omitempty"`
}

Request represents a JSON-RPC 2.0 request object. See https://www.jsonrpc.org/specification#request_object

func (Request) IsNotification

func (r Request) IsNotification() bool

IsNotification returns true if the request is a notification (no ID).

func (Request) IsReservedMethod

func (r Request) IsReservedMethod() bool

IsReservedMethod returns true if the method name is reserved for RPC-internal methods. Method names beginning with "rpc." are reserved per the JSON-RPC 2.0 specification.

func (Request) Valid

func (r Request) Valid() bool

Valid checks if the Request is a valid JSON-RPC 2.0 request. It validates that the jsonrpc field is "2.0" and the method is not empty.

type Response

type Response struct {
	// JSONRPC specifies the version of the JSON-RPC protocol. Must be "2.0".
	JSONRPC string `json:"jsonrpc"`
	// Result contains the result of the method invocation on success.
	// This must not exist if there was an error.
	Result json.RawMessage `json:"result,omitempty"`
	// Error contains error information if the method invocation failed.
	// This must not exist if there was no error.
	Error *Error `json:"error,omitempty"`
	// ID matches the id from the corresponding Request object.
	// This field is always present in responses.
	// It can be a string, number, or null (null when the request ID couldn't be detected).
	ID any `json:"id"`
}

Response represents a JSON-RPC 2.0 response object. See https://www.jsonrpc.org/specification#response_object

func (Response) IsError

func (r Response) IsError() bool

IsError returns true if the response contains an error.

func (Response) IsSuccess

func (r Response) IsSuccess() bool

IsSuccess returns true if the response contains a result (no error).

func (Response) Valid

func (r Response) Valid() bool

Valid checks if the Response is a valid JSON-RPC 2.0 response. It validates that the jsonrpc field is "2.0", ID is present, and either Result or Error exists (not both).

Jump to

Keyboard shortcuts

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