mcp

package
v1.0.31-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 19 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mcp is a small, dependency-free Model Context Protocol server: tools described by Go functions, served over Streamable HTTP or stdio. Giode uses it to expose a running app to AI agents (see Config.MCP in the root package), and it can be used on its own:

srv := mcp.NewServer("my-app", "1.0.0")
srv.AddTool(mcp.Func("greet", "Greets someone.",
	func(ctx context.Context, in struct {
		Name string `json:"name" description:"Who to greet"`
	}) (any, error) {
		return "Hello, " + in.Name, nil
	}))
http.Handle("/mcp", srv)

Only the tools capability is implemented; resources and prompts are advertised as empty lists.

Index

Constants

View Source
const LatestProtocolVersion = "2025-06-18"

LatestProtocolVersion is the newest MCP revision this server speaks.

Variables

This section is empty.

Functions

func SchemaFor

func SchemaFor(t reflect.Type) map[string]any

SchemaFor derives a JSON Schema from a Go type, the way Func does for tool inputs. Structs become objects (see Func for the tags honored), slices arrays and maps objects; interface and json.RawMessage values accept anything.

Types

type Content

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

Content is one item of a tool result.

type Result

type Result struct {
	Content []Content `json:"content"`
	// IsError reports a tool-level failure the model should see (as
	// opposed to a protocol error).
	IsError bool `json:"isError,omitempty"`
}

Result is a tool call's result.

func Errorf

func Errorf(format string, args ...any) *Result

Errorf returns a tool-level error result.

func Image

func Image(data []byte, mimeType string) *Result

Image returns a result holding an image.

func JSON

func JSON(v any) (*Result, error)

JSON returns a result holding v encoded as indented JSON.

func Text

func Text(s string) *Result

Text returns a result holding s.

type Server

type Server struct {

	// Instructions, when set, are sent to clients on initialize to
	// explain how to use the server.
	Instructions string
	// Token, when set, must be sent by HTTP clients as
	// "Authorization: Bearer <token>".
	Token string
	// AllowedOrigins lists browser origins allowed to call the HTTP
	// endpoint, besides localhost ones. Requests without an Origin
	// header (non-browser clients) are always allowed.
	AllowedOrigins []string
	// contains filtered or unexported fields
}

Server is an MCP server. Its zero value is not usable; create one with NewServer. A Server is safe for concurrent use.

func NewServer

func NewServer(name, version string) *Server

NewServer returns a server identifying itself as name and version.

func (*Server) AddTool

func (s *Server) AddTool(t Tool)

AddTool registers (or replaces) a tool.

func (*Server) Handle

func (s *Server) Handle(ctx context.Context, msg []byte) []byte

Handle processes one JSON-RPC message and returns the response to send, or nil for notifications.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(ctx context.Context, addr string, ready func(addr string)) error

ListenAndServe serves the HTTP transport at addr under the path "/mcp" until ctx is canceled. It returns the address actually bound (useful with port 0) through ready, if non-nil, once listening.

func (*Server) RemoveTool

func (s *Server) RemoveTool(name string)

RemoveTool unregisters a tool.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the Streamable HTTP transport: each POST carries one JSON-RPC message and gets its response as application/json. Server-initiated streams (GET) aren't offered, which the specification allows.

Requests from browsers are only accepted from localhost origins and AllowedOrigins, guarding a local server against DNS rebinding; when Token is set, every request must carry it as a bearer token.

func (*Server) ServeStdio

func (s *Server) ServeStdio(ctx context.Context, r io.Reader, w io.Writer) error

ServeStdio serves newline-delimited JSON-RPC messages from r, writing responses to w, until r is exhausted or ctx is canceled - the stdio transport, for clients that launch the app as a subprocess. Requests are handled concurrently, so a slow tool doesn't block others.

func (*Server) Tools

func (s *Server) Tools() []Tool

Tools returns the registered tools, sorted by name.

type Tool

type Tool struct {
	// Name identifies the tool; letters, digits, '_' and '-'.
	Name string
	// Description tells the model what the tool does and when to use
	// it.
	Description string
	// InputSchema is the JSON Schema of the arguments object. Nil
	// means no arguments.
	InputSchema map[string]any
	// ReadOnly hints that the tool doesn't modify anything.
	ReadOnly bool
	// Handler runs the tool with the raw arguments object.
	Handler func(ctx context.Context, args json.RawMessage) (*Result, error)
}

Tool is a callable tool.

func Func

func Func[In any](name, description string, fn func(ctx context.Context, in In) (any, error)) Tool

Func builds a Tool from a typed function. The input struct's exported fields become the arguments: their JSON names come from json tags, a field is required unless tagged omitempty, and description and enum ("a,b,c") tags document it. The output is returned as text if it's a string, as-is if it's a *Result, and as JSON otherwise.

Jump to

Keyboard shortcuts

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