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 ¶
const MetadataPath = "/.well-known/oauth-protected-resource" + Path
MetadataPath is the endpoint for the protected resource metadata.
const Path = "/api/mcp"
Path is the HTTP endpoint for MCP requests.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 (*Server) Command ¶
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.
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.