mcpwire

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 5 Imported by: 0

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

View Source
const (
	CodeParseError     = -32700
	CodeInvalidRequest = -32600
	CodeMethodNotFound = -32601
	CodeInvalidParams  = -32602
)

JSON-RPC error codes.

View Source
const Version = "2.0"

Version is the JSON-RPC version of every message.

Variables

View Source
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

func AgreeVersion(version string) string

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

func Marshal(v any) ([]byte, error)

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

func MarshalResponse(r Response) ([]byte, error)

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 Content

type Content struct {
	Type string `json:"type"`
	Text string `json:"text"`
}

Content is a content of a tool's result.

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

type Implementation struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

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

func Decode(body []byte) (*Message, error)

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

func (m *Message) Argument(name string) any

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

func (m *Message) InitializeVersion() string

InitializeVersion returns the protocolVersion of the params of an initialize, or "" when there is none.

func (*Message) IsRequest

func (m *Message) IsRequest() bool

IsRequest reports a message with a method and an id, which takes an answer.

func (*Message) IsToolCall

func (m *Message) IsToolCall() bool

IsToolCall reports a tools/call request.

func (*Message) ToolName

func (m *Message) ToolName() string

ToolName returns the name of the tool a tools/call calls, or "" when its params don't name one.

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.

func Fail

func Fail(id jsontext.Value, code int64, message string, data any) Response

Fail returns the response with an error to the request with id. An empty id is written as null, and a nil data is left out.

func Result

func Result(id jsontext.Value, result any) Response

Result returns the response with result to the request with id. An empty id is written as null.

type Tool

type Tool struct {
	Name        string         `json:"name"`
	Description string         `json:"description,omitzero"`
	InputSchema jsontext.Value `json:"inputSchema"`
}

Tool is a tool of a server, as tools/list lists it.

Jump to

Keyboard shortcuts

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