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 ¶
- func ReadMessage(r *bufio.Reader) ([]byte, error)
- func ReadMessageNDJSON(r *bufio.Reader) ([]byte, error)
- func SendNotification(w io.Writer, mu *sync.Mutex, method string, params interface{})
- func SendResponse(w io.Writer, mu *sync.Mutex, id interface{}, result interface{}, rpcErr *Error)
- func SendResponseNDJSON(w io.Writer, mu *sync.Mutex, id interface{}, result interface{}, rpcErr *Error)
- func SetLogger(l logger.Logger)
- func WriteMessage(w io.Writer, mu *sync.Mutex, msg interface{})
- func WriteMessageNDJSON(w io.Writer, mu *sync.Mutex, msg interface{})
- type Error
- type Notification
- type Request
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReadMessage ¶
ReadMessage reads one Content-Length-framed JSON-RPC message from r.
func ReadMessageNDJSON ¶
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 ¶
SendNotification sends a JSON-RPC 2.0 notification (no ID).
func SendResponse ¶
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 WriteMessage ¶
WriteMessage serializes msg as JSON and writes it with Content-Length framing. 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.