jsonrpc: expose encoding and decoding functions (#114)
Expose `jsonrpc2.MakeID`, `jsonrpc2.EncodeMessage` and `jsonrpc2.DecodeMessage` functions to allow implementing custom `mcp.Transport`. Add an example that demonstrates a custom transport implementation. Fixes #110.
This commit is contained in:
parent
aa9d4b28be
commit
fe208c144f
1 changed files with 129 additions and 1 deletions
|
|
@ -18,3 +18,22 @@ type (
|
|||
// Response is a JSON-RPC response.
|
||||
Response = jsonrpc2.Response
|
||||
)
|
||||
|
||||
// MakeID coerces the given Go value to an ID. The value is assumed to be the
|
||||
// default JSON marshaling of a Request identifier -- nil, float64, or string.
|
||||
//
|
||||
// Returns an error if the value type was not a valid Request ID type.
|
||||
func MakeID(v any) (ID, error) {
|
||||
return jsonrpc2.MakeID(v)
|
||||
}
|
||||
|
||||
// EncodeMessage serializes a JSON-RPC message to its wire format.
|
||||
func EncodeMessage(msg Message) ([]byte, error) {
|
||||
return jsonrpc2.EncodeMessage(msg)
|
||||
}
|
||||
|
||||
// DecodeMessage deserializes JSON-RPC wire format data into a Message.
|
||||
// It returns either a Request or Response based on the message content.
|
||||
func DecodeMessage(data []byte) (Message, error) {
|
||||
return jsonrpc2.DecodeMessage(data)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue