mcp

package
v0.1.0-alpha.1095 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 32 Imported by: 0 modules, 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 serves typed tools to AI clients over the Model Context Protocol. The server is stateless and returns JSON responses at Path.

Create a server with New, register handlers with Tool, and mount Server.Routes. The app must authenticate API keys before the MCP handler runs. The API key middleware needs apikeys.WithRPCPath(Path) to accept read keys on POST. Cookie authentication alone does not permit access to the MCP endpoint.

Read keys can list and call only read-only tools. A tool with roles also requires a user with one of those roles. ToolError returns an error message that is safe for the model. Other handler errors and panics return a fixed message and log the cause. Each tool call preserves the request deadline and sets the audit actor reason. The endpoint rejects duplicate JSON names and invalid UTF-8 before SDK dispatch. Schemas use the string type for custom types that encode as JSON strings.

Server.Command prints tool definitions and inferred schemas without a database. Server.SDK provides access to SDK features outside the typed tool API. The package is under kit/x, so its API can change before v1.

Index

Constants

View Source
const MetadataPath = "/.well-known/oauth-protected-resource" + Path

MetadataPath is the endpoint for the protected resource metadata.

View Source
const Path = "/api/mcp"

Path is the HTTP endpoint for MCP requests.

Variables

This section is empty.

Functions

func Tool

func Tool[In, Out any](s *Server, def ToolDef, h func(context.Context, In) (Out, error))

Tool registers a typed handler. An invalid or duplicate definition causes a panic.

func ToolError

func ToolError(format string, args ...any) error

ToolError makes an error whose message is safe for the model.

Types

type Config

type Config struct {
	// Name identifies the implementation. It is required.
	Name string
	// Version identifies the implementation version. It is required.
	Version string
	// Title is an optional display name.
	Title string
	// Instructions gives the model optional server instructions.
	Instructions string
	// MaxBodyBytes limits request bodies. Zero selects 1 MiB.
	MaxBodyBytes int64
	// MaxResultBytes limits the JSON form of each tool result. Zero selects 256 KiB.
	MaxResultBytes int64
}

Config describes the server and its request and result limits.

type Option

type Option func(*options)

Option configures a Server.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger selects the logger. The default is slog.Default().

func WithMeterProvider

func WithMeterProvider(mp metric.MeterProvider) Option

WithMeterProvider selects the meter provider. The default is otel.GetMeterProvider().

func WithTracerProvider

func WithTracerProvider(tp trace.TracerProvider) Option

WithTracerProvider selects the tracer provider. The default is otel.GetTracerProvider().

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server serves typed tools with the API key and role checks.

func New

func New(cfg Config, opts ...Option) (*Server, error)

New validates cfg and creates a server without I/O.

func (*Server) Command

func (s *Server) Command() func(context.Context, []string) error

Command returns the operator command that prints the tools as JSON. Usage errors include the usage text for the app to print with exit status 2.

func (*Server) Routes

func (s *Server) Routes(publicOrigin string) func(*http.ServeMux)

Routes registers the MCP endpoint and the public metadata endpoint.

func (*Server) SDK

func (s *Server) SDK() *sdkmcp.Server

SDK returns the underlying server for features outside the typed tool API.

func (*Server) Tools

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

Tools returns independent tool definitions sorted by name.

type ToolDef

type ToolDef struct {
	// Name is a unique name that matches [a-z][a-z0-9_]{0,63}.
	Name string
	// Title is an optional display name.
	Title string
	// Description explains the tool to the model. It is required.
	Description string
	// ReadOnly permits read keys to use this tool.
	ReadOnly bool
	// Destructive marks a write tool that can remove data.
	Destructive bool
	// Idempotent marks a tool whose repeated calls have no additional effect.
	Idempotent bool
	// Roles restricts the tool to users with one of these roles.
	Roles []string
}

ToolDef describes a typed tool and the callers that can use it.

Jump to

Keyboard shortcuts

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