jsonrpc

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package jsonrpc implements JSON-RPC 2.0 request parsing and method dispatch.

Index

Constants

View Source
const (
	ErrorCodeParseError     = -32700
	ErrorCodeInvalidRequest = -32600
	ErrorCodeMethodNotFound = -32601
	ErrorCodeInvalidParams  = -32602
	ErrorCodeInternalError  = -32603
)

Standard JSON-RPC error codes.

View Source
const Version = "2.0"

Version is the JSON-RPC 2.0 version identifier.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

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

Engine handles JSON-RPC 2.0 request processing.

func NewEngine

func NewEngine(logger *slog.Logger) *Engine

NewEngine creates a new JSON-RPC engine.

func (*Engine) ProcessRequest

func (engine *Engine) ProcessRequest(requestData []byte) []byte

ProcessRequest processes a JSON-RPC request payload and returns a response payload.

func (*Engine) ProcessRequestDirect

func (engine *Engine) ProcessRequestDirect(request *Request) *Response

ProcessRequestDirect processes a JSON-RPC request object and returns the response object.

func (*Engine) RegisterMethod

func (engine *Engine) RegisterMethod(name string, handler MethodHandler)

RegisterMethod registers a method handler with the JSON-RPC engine.

func (*Engine) RegisteredMethods added in v0.33.0

func (engine *Engine) RegisteredMethods() []string

RegisteredMethods returns a sorted list of registered method names.

func (*Engine) SetLogger added in v1.5.0

func (engine *Engine) SetLogger(logger *slog.Logger)

SetLogger replaces the logger used by the engine. Passing nil restores the process default. Configure the logger before processing requests.

type ErrorDetails

type ErrorDetails struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

ErrorDetails represents a JSON-RPC 2.0 error object.

type MethodHandler

type MethodHandler func(params any) (any, error)

MethodHandler defines the signature for JSON-RPC method handlers.

type Request

type Request struct {
	JSONRPC string `json:"jsonrpc"`
	Method  string `json:"method"`
	Params  any    `json:"params,omitempty"`
	ID      any    `json:"id,omitempty"`
	// contains filtered or unexported fields
}

Request represents a JSON-RPC 2.0 request message.

func (*Request) IsNotification added in v1.1.0

func (r *Request) IsNotification() bool

IsNotification reports whether this request omitted the id member.

func (*Request) UnmarshalJSON added in v1.1.0

func (r *Request) UnmarshalJSON(data []byte) error

UnmarshalJSON preserves whether "id" was present. JSON-RPC notifications omit id entirely; an explicit `"id": null` is still a request and must get a response with a null id.

type Response

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

Response represents a JSON-RPC 2.0 response message.

Jump to

Keyboard shortcuts

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