mcp

package
v1.98.0 Latest Latest
Warning

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

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

Documentation

Overview

Package mcp is a Model Context Protocol server over Streamable HTTP, as much of one as a server of tools needs: every message a POST, every answer one JSON object, no stream, no session.

It speaks both eras of the protocol on the one endpoint. A request carrying its protocol version in its _meta is of 2026-07-28 -- stateless, every request saying who is asking and how, its headers repeating its method and tool -- and is answered so. Anything else is of the eras before, which open with initialize; with no session to remember what it settled, each request after is answered on what it says itself.

Index

Constants

View Source
const (
	// ModernVersion is the revision served statelessly, its version in every request.
	ModernVersion = "2026-07-28"
	// LatestLegacyVersion is what an initialize asking for a version not served is offered.
	LatestLegacyVersion = "2025-11-25"
)
View Source
const (
	CodeParseError                 = -32700
	CodeInvalidRequest             = -32600
	CodeMethodNotFound             = -32601
	CodeInvalidParams              = -32602
	CodeInternalError              = -32603
	CodeHeaderMismatch             = -32020
	CodeUnsupportedProtocolVersion = -32022
)

JSON-RPC's error codes, and those MCP reserves.

Variables

This section is empty.

Functions

func ObjectSchema

func ObjectSchema(properties map[string]any, required ...string) map[string]any

ObjectSchema is the schema of a tool's arguments: an object of the properties, the required among them, and no others.

func PropertySchema

func PropertySchema(jsonType any, description string, more map[string]any) map[string]any

PropertySchema is the schema of one argument, of the JSON type -- a name, or a list of names for one of several -- with what else it says.

func ReadArguments

func ReadArguments(arguments jsontext.Value, input any) error

ReadArguments reads a call's arguments into input, refusing a member the tool does not take, as a *ToolError the model is shown.

func WithCaller

func WithCaller(ctx context.Context, caller *Caller) context.Context

WithCaller is the context of a call for the caller, which the endpoint sets before handing the request to the server, and a tool reads with CallerOf.

Types

type Annotations

type Annotations struct {
	Title           string `json:"title,omitzero"`
	ReadOnlyHint    bool   `json:"readOnlyHint"`
	DestructiveHint bool   `json:"destructiveHint"`
	IdempotentHint  bool   `json:"idempotentHint"`
	OpenWorldHint   bool   `json:"openWorldHint"`
}

Annotations are what a client is told of a tool's effects. Every hint is sent, whether true or not: Claude requires readOnlyHint and destructiveHint declared, and lets a read-only tool run without asking.

func ReadOnly

func ReadOnly(title string) *Annotations

ReadOnly is the annotations of a tool that changes nothing, which a client may run without asking.

type Caller

type Caller struct {
	Account  string
	ClientId string
}

Caller is whom a tool call is for: the account whose data it reaches, and the client asking, as the request's access token names them.

func CallerOf

func CallerOf(ctx context.Context) (*Caller, error)

CallerOf is the caller the context carries, an error when it carries none with an account: a tool is never run for no one.

type Content added in v1.98.0

type Content struct {
	Type     string `json:"type"`
	Text     string `json:"text,omitzero"`
	Data     string `json:"data,omitzero"`
	MimeType string `json:"mimeType,omitzero"`
}

Content is a block of a tool's result besides the text its structured content is given as: an image the model is shown, say. Data is base64-encoded, as the protocol carries it.

func ImageContent added in v1.98.0

func ImageContent(data []byte, mimeType string) *Content

ImageContent is an image block of the given bytes and type.

type Implementation

type Implementation struct {
	Name    string `json:"name"`
	Version string `json:"version"`
	Title   string `json:"title,omitzero"`
}

type Reply

type Reply struct {
	Status int
	Body   []byte
	Method string
	Tool   string
}

Reply is what answers a POST: a status, and a JSON body unless the status is 202. Method and Tool say what was asked, for the log.

type Result added in v1.98.0

type Result struct {
	Structured any
	Content    []*Content
}

Result is what a tool answers with to give blocks besides its structured content: Structured is what it would have answered with otherwise, and Content goes after the text of it, in order.

type Server

type Server struct {
	Info         *Implementation
	Instructions string
	Tools        []*Tool
	// AllowedOrigins are the origins a request may come with. One coming with no Origin is not a
	// browser's and is let through; one with another origin is refused, which is what keeps a page
	// from reaching the server through its visitor's browser.
	AllowedOrigins []string
	// ListTtl is how long a client may keep the tool list before asking again.
	ListTtl time.Duration
}

func (*Server) Handle

func (s *Server) Handle(ctx context.Context, header http.Header, body []byte) *Reply

Handle answers one POST of the endpoint.

type Tool

type Tool struct {
	Name         string         `json:"name"`
	Title        string         `json:"title,omitzero"`
	Description  string         `json:"description,omitzero"`
	InputSchema  map[string]any `json:"inputSchema"`
	OutputSchema map[string]any `json:"outputSchema,omitzero"`
	Annotations  *Annotations   `json:"annotations,omitzero"`

	Call func(ctx context.Context, arguments jsontext.Value) (any, error) `json:"-"`
}

Tool is a tool the server offers. Call answers with what becomes the result's structured content, a JSON object, and its text, or with a *Result to give more blocks besides; a *ToolError is a refusal, and any other error the server's.

type ToolError

type ToolError struct {
	Message string
}

ToolError is a tool refusing what it was asked, told to the model as the tool's result rather than as a protocol error, so that it can try again otherwise.

func (*ToolError) Error

func (e *ToolError) Error() string

Jump to

Keyboard shortcuts

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