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 ¶
const JSONRPCVersion = "2.0"
JSONRPCVersion is the version string for JSON-RPC 2.0
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
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
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
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 ¶
IsNotification returns true if the request is a notification (no ID).
func (Request) IsReservedMethod ¶
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.
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