jsonrpc

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodeParseError     = -32700
	CodeInvalidRequest = -32600
	CodeMethodNotFound = -32601
	CodeInvalidParams  = -32602
	CodeInternalError  = -32603
	CodeAuthFailed     = -32000
)

Error codes as defined by JSON-RPC 2.0 and MCP extensions.

View Source
const Version = "2.0"

Version is the JSON-RPC 2.0 version string.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v any) ([]byte, error)

Marshal serializes v to JSON.

Types

type Error

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

Error represents a JSON-RPC 2.0 error object.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

type ID

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

ID represents a JSON-RPC request ID, which can be a string, number, or null.

func NullID

func NullID() *ID

NullID creates a null ID.

func NumberID

func NumberID(n int64) *ID

NumberID creates an ID from an integer value.

func StringID

func StringID(s string) *ID

StringID creates an ID from a string value.

func (*ID) IsNull

func (id *ID) IsNull() bool

IsNull returns true if the ID is null.

func (*ID) MarshalJSON

func (id *ID) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*ID) String

func (id *ID) String() string

String returns a human-readable representation of the ID.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Message

type Message struct {
	Raw     []byte
	Request *Request
	Resp    *Response
	IsJSON  bool
}

Message is a parsed JSON-RPC frame. Exactly one of Request or Resp will be set when IsJSON is true.

type ParseMode

type ParseMode int

ParseMode controls how the parser splits the byte stream into messages.

const (
	// ModeAuto defaults to newline-delimited parsing.
	ModeAuto ParseMode = iota
	// ModeNewline expects one JSON message per line.
	ModeNewline
	// ModeContentLength expects HTTP-style Content-Length headers.
	ModeContentLength
)

type Parser

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

Parser reads JSON-RPC messages from a stream.

func NewParser

func NewParser(r io.Reader, mode ParseMode) *Parser

NewParser creates a new Parser reading from r in the given mode.

func (*Parser) Next

func (p *Parser) Next() (Message, error)

Next reads the next message from the stream. Returns io.EOF when the stream ends cleanly.

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 represents a JSON-RPC 2.0 request or notification.

func (*Request) IsNotification

func (r *Request) IsNotification() bool

IsNotification returns true if the request has no ID (i.e., it is a notification).

type Response

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

Response represents a JSON-RPC 2.0 response.

func ErrorResponse

func ErrorResponse(id *ID, code int, message string) Response

ErrorResponse constructs a Response representing an error.

Jump to

Keyboard shortcuts

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