jsonrpc

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package jsonrpc implements JSON-RPC 2.0 message transport shared by the LSP and MCP servers. LSP uses Content-Length-header framing (ReadMessage/WriteMessage); MCP-over-stdio uses newline-delimited JSON per the MCP spec (ReadMessageNDJSON/WriteMessageNDJSON).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadMessage

func ReadMessage(r *bufio.Reader) ([]byte, error)

ReadMessage reads one Content-Length-framed JSON-RPC message from r.

func ReadMessageNDJSON

func ReadMessageNDJSON(r *bufio.Reader) ([]byte, error)

ReadMessageNDJSON reads one newline-delimited JSON-RPC message from r. Per the MCP stdio transport spec, each message is a single line of JSON terminated by \n, with no embedded newlines and no Content-Length header. Blank lines are skipped so a stray CRLF between messages does not error.

func SendNotification

func SendNotification(w io.Writer, mu *sync.Mutex, method string, params interface{})

SendNotification sends a JSON-RPC 2.0 notification (no ID).

func SendResponse

func SendResponse(w io.Writer, mu *sync.Mutex, id interface{}, result interface{}, rpcErr *Error)

SendResponse sends a JSON-RPC 2.0 success or error response.

A non-nil rpcErr produces an error response (no `result` field). Otherwise produces a success response with a `result` field that is always emitted — `null` if result is nil. Never produces a response with both or neither.

func SendResponseNDJSON

func SendResponseNDJSON(w io.Writer, mu *sync.Mutex, id interface{}, result interface{}, rpcErr *Error)

SendResponseNDJSON is the NDJSON counterpart of SendResponse.

func SetLogger

func SetLogger(l logger.Logger)

SetLogger replaces the package-level Logger. Intended for tests.

func WriteMessage

func WriteMessage(w io.Writer, mu *sync.Mutex, msg interface{})

WriteMessage serializes msg as JSON and writes it with Content-Length framing. The write is protected by mu.

func WriteMessageNDJSON

func WriteMessageNDJSON(w io.Writer, mu *sync.Mutex, msg interface{})

WriteMessageNDJSON serializes msg as JSON and writes it as a single line terminated by \n. The write is protected by mu.

Types

type Error

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

Error represents a JSON-RPC 2.0 error object.

type Notification

type Notification struct {
	JSONRPC string      `json:"jsonrpc"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params,omitempty"`
}

Notification is a JSON-RPC 2.0 notification from the server (no ID).

type Request

type Request struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      interface{}     `json:"id,omitempty"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params,omitempty"`
}

Request is a JSON-RPC 2.0 request or notification from the client.

type Response

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

Response is a JSON-RPC 2.0 response from the server.

Per JSON-RPC 2.0, a response MUST contain either `result` or `error`, never both and never neither. The `omitempty` on Result was a bug — it dropped the field when Result was nil, producing responses with neither field, which modern LSP clients reject. Use successResponse / errorResponse via SendResponse instead of constructing Response directly for new code.

Jump to

Keyboard shortcuts

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