Documentation
¶
Overview ¶
Package mcpwire holds the JSON-RPC 2.0 messages of the Model Context Protocol that MCP mocks read and write, over Streamable HTTP. It covers what an MCP server mock answers: the lifecycle, ping and tools.
Index ¶
Constants ¶
const ( CodeParseError = -32700 CodeInvalidRequest = -32600 CodeMethodNotFound = -32601 CodeInvalidParams = -32602 )
JSON-RPC error codes.
const Version = "2.0"
Version is the JSON-RPC version of every message.
Variables ¶
var ( // ErrParse is the error of Decode for a body that isn't JSON. ErrParse = errors.New("mcpwire: the body is not JSON") // ErrInvalidRequest is the error of Decode for JSON that isn't one JSON-RPC 2.0 object, such as a batch. ErrInvalidRequest = errors.New("mcpwire: expected one JSON-RPC 2.0 object") )
Functions ¶
func AgreeVersion ¶
AgreeVersion returns the MCP version an endpoint mock answers a client that asks for version: version, when the mock knows it, and the newest it knows otherwise.
func Marshal ¶
Marshal encodes v as JSON, with the keys of maps in order, so equal values give equal bytes. A string that isn't UTF-8 is an error.
func MarshalResponse ¶
MarshalResponse encodes r as Marshal does, but a string that isn't UTF-8, such as a mock's name or a rendered text, has the replacement character in place of its bad bytes: the client still gets an answer it can decode.
Types ¶
type CallToolResult ¶
type CallToolResult struct {
Content []Content `json:"content"`
IsError bool `json:"isError,omitzero"`
}
CallToolResult is the result of a tools/call.
type Error ¶
type Error struct {
Code int64 `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitzero"`
}
Error is the error of a Response.
type Implementation ¶
Implementation names a server.
type InitializeResult ¶
type InitializeResult struct {
ProtocolVersion string `json:"protocolVersion"`
Capabilities map[string]any `json:"capabilities"`
ServerInfo Implementation `json:"serverInfo"`
}
InitializeResult is the result of an initialize.
type ListToolsResult ¶
type ListToolsResult struct {
Tools []Tool `json:"tools"`
}
ListToolsResult is the result of a tools/list.
type Message ¶
type Message struct {
JSONRPC string `json:"jsonrpc"`
ID jsontext.Value `json:"id,omitzero"`
Method string `json:"method,omitzero"`
Params jsontext.Value `json:"params,omitzero"`
// contains filtered or unexported fields
}
Message is a JSON-RPC message a client sends: a request, a notification, or a response to the server.
A Message never changes once Decode made it, so goroutines may share it.
func Decode ¶
Decode decodes one JSON-RPC message. It fails with ErrParse for a body that isn't JSON, and with ErrInvalidRequest for JSON that isn't one object with "jsonrpc": "2.0".
func (*Message) Argument ¶
Argument returns the argument name of a tools/call, decoded anew on each call: a number is an int64 when it is whole and fits, and a float64 otherwise, objects map[string]any and lists []any. It is nil when the call hasn't the argument.
func (*Message) InitializeVersion ¶
InitializeVersion returns the protocolVersion of the params of an initialize, or "" when there is none.
func (*Message) IsRequest ¶
IsRequest reports a message with a method and an id, which takes an answer.
func (*Message) IsToolCall ¶
IsToolCall reports a tools/call request.
type Response ¶
type Response struct {
JSONRPC string `json:"jsonrpc"`
ID jsontext.Value `json:"id"`
Result any `json:"result,omitzero"`
Error *Error `json:"error,omitzero"`
}
Response is the answer to a request: a result or an error.