| 1 | // Copyright 2025 The Go MCP SDK Authors. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package jsonrpc exposes part of a JSON-RPC v2 implementation |
| 6 | // for use by mcp transport authors. |
| 7 | package jsonrpc |
| 8 | |
| 9 | import "github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2" |
| 10 | |
| 11 | type ( |
| 12 | // ID is a JSON-RPC request ID. |
| 13 | ID = jsonrpc2.ID |
| 14 | // Message is a JSON-RPC message. |
| 15 | Message = jsonrpc2.Message |
| 16 | // Request is a JSON-RPC request. |
| 17 | Request = jsonrpc2.Request |
| 18 | // Response is a JSON-RPC response. |
| 19 | Response = jsonrpc2.Response |
| 20 | // Error is a structured error in a JSON-RPC response. |
| 21 | Error = jsonrpc2.WireError |
| 22 | ) |
| 23 | |
| 24 | // MakeID coerces the given Go value to an ID. The value should be the |
| 25 | // default JSON marshaling of a Request identifier: nil, float64, or string. |
| 26 | // |
| 27 | // Returns an error if the value type was not a valid Request ID type. |
| 28 | func MakeID(v any) (ID, error) { |
| 29 | return jsonrpc2.MakeID(v) |
| 30 | } |
| 31 | |
| 32 | // EncodeMessage serializes a JSON-RPC message to its wire format. |
| 33 | func EncodeMessage(msg Message) ([]byte, error) { |
| 34 | return jsonrpc2.EncodeMessage(msg) |
| 35 | } |
| 36 | |
| 37 | // DecodeMessage deserializes JSON-RPC wire format data into a Message. |
| 38 | // It returns either a Request or Response based on the message content. |
| 39 | func DecodeMessage(data []byte) (Message, error) { |
| 40 | return jsonrpc2.DecodeMessage(data) |
| 41 | } |
| 42 | |
| 43 | // Standard JSON-RPC 2.0 error codes. |
| 44 | // See https://www.jsonrpc.org/specification#error_object |
| 45 | const ( |
| 46 | // CodeParseError indicates invalid JSON was received by the server. |
| 47 | CodeParseError = -32700 |
| 48 | // CodeInvalidRequest indicates the JSON sent is not a valid Request object. |
| 49 | CodeInvalidRequest = -32600 |
| 50 | // CodeMethodNotFound indicates the method does not exist or is not available. |
| 51 | CodeMethodNotFound = -32601 |
| 52 | // CodeInvalidParams indicates invalid method parameter(s). |
| 53 | CodeInvalidParams = -32602 |
| 54 | // CodeInternalError indicates an internal JSON-RPC error. |
| 55 | CodeInternalError = -32603 |
| 56 | ) |