Help us shape the future of Go! Take the 2026 Go Developer Survey

mcphost

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package mcphost provides MCP registration and lifecycle without application roles or workflow policy. Handlers and their collaborators belong to callers.

Index

Constants

View Source
const EndpointPath = "/mcp"

EndpointPath is the URL path Start serves on. The handler answers on any path; the endpoint names this one.

Variables

View Source
var ErrLeaseClosed = errors.New("mcphost: lease closed")

ErrLeaseClosed is returned by Lease.Connect once the lease is closed.

Functions

func AddDone

func AddDone(r *Registry, opts DoneOptions, roles ...string)

AddDone registers done using the standard input shape and default work.

func AddDoneTool

func AddDoneTool[In any](r *Registry, opts DoneOptions, decode func(In, work.Ref) agent.Outcome, roles ...string)

AddDoneTool registers done with a caller-defined input shape containing a status property. Decode maps that shape onto a generic outcome. Each request receives its own copy of the default work tags, including under concurrency.

func AddTool

func AddTool[In, Out any](r *Registry, tool *mcp.Tool, handler mcp.ToolHandlerFor[In, Out], roles ...string)

AddTool registers a typed handler. No roles means every known role. Duplicate names (even across disjoint roles), unknown scopes and invalid metadata panic, like the SDK's typed registration. Metadata is copied; caller edits cannot alter a registration. An unregistered or hidden tool gets the SDK's unknown tool error and never reaches a handler.

func BearerTransport

func BearerTransport(token string, base http.RoundTripper) http.RoundTripper

BearerTransport returns an http.RoundTripper that presents token as the bearer credential on every request through base (http.DefaultTransport when nil): the client side of an Endpoint.

func Connect

func Connect(ctx context.Context, server *mcp.Server, client mcp.Implementation) (*mcp.ClientSession, *mcp.ServerSession, error)

Connect returns a client session connected to server over an in-memory transport. Callers close the client session and wait for the server one. Client metadata is supplied by the caller.

func HTTPHandler

func HTTPHandler(srv *mcp.Server, token string) http.Handler

HTTPHandler serves srv over streamable HTTP to a client presenting token as its bearer credential, and answers 401 to any other request. The SDK's own guard, which refuses a request to a loopback listener whose Host header is not a loopback name, is switched off: it protects a local server from a browser tricked into reaching it (DNS rebinding), which the token does here, and the container reaches the loopback listener through the host's alias, which is exactly such a Host header.

func IsCleanShutdown

func IsCleanShutdown(err error) bool

IsCleanShutdown reports whether an error from mcp.Server.Run is an ordinary end of session rather than a failure. A client closes the server's stdin when it is done with it and kills the process on shutdown; neither is worth a nonzero exit status, which the client would record as the server having crashed.

func Run

func Run(ctx context.Context, srv *mcp.Server, transport mcp.Transport) error

Run serves any SDK transport, including StdioTransport, until closure or cancellation. Normal client closure is successful shutdown.

func SchemaFor

func SchemaFor[In any](enums map[string][]string) *jsonschema.Schema

SchemaFor infers a typed input schema and constrains named properties. Empty value lists leave properties unconstrained. Invalid types/properties panic.

func ServeHTTP

func ServeHTTP(ctx context.Context, srv *mcp.Server, ln net.Listener, token string) error

ServeHTTP serves an already-bound listener until cancellation or failure. It owns and closes the listener. An empty token is refused.

func Start

func Start(ctx context.Context, srv *mcp.Server) (Endpoint, Lease, error)

Start serves srv to one turn over streamable HTTP on a fresh loopback port with a fresh bearer token, until the lease is closed or ctx ends. Each call has its own listener and token, so one turn cannot reach another's server. A host turn reaches the loopback; so does a container under Docker Desktop through Endpoint.Via. On Linux a container reaches the host only on the bridge gateway: use StartOn with that address.

func StartMemory

func StartMemory(ctx context.Context, srv *mcp.Server) (Endpoint, Lease, error)

StartMemory is Start without a listener, for tests: the endpoint's URL is a memory:// name nothing outside this process can open, and Lease.Connect reaches the server over an in-memory transport, with no token. Closing the lease, or the end of ctx, closes every session it connected and refuses further ones.

func StartOn

func StartOn(ctx context.Context, srv *mcp.Server, addr string) (Endpoint, Lease, error)

StartOn is Start on a given listen address, such as the Docker bridge gateway with port 0.

func Tools

func Tools(ctx context.Context, server *mcp.Server, client mcp.Implementation) ([]*mcp.Tool, error)

Tools lists the tools a server exposes, in the order the server offers them. It connects a client over an in-memory transport, so it exercises the same path a session does.

Types

type DoneInput

type DoneInput struct {
	Status string `json:"status" jsonschema:"the outcome to report"`
	Note   string `json:"note,omitempty" jsonschema:"one short line describing the outcome"`
}

DoneInput is the tracker-independent wire format. Callers with an existing wire contract can use their own input type with AddDoneTool.

type DoneOptions

type DoneOptions struct {
	Title       string
	Description string
	Statuses    []string
	Work        work.Ref
	Report      func(context.Context, agent.Outcome) (agent.Outcome, error)
}

DoneOptions supplies workflow policy and default work context. Report must validate the outcome and persist/report it; its returned status is acknowledged to the client. Statuses constrain the schema (empty means unconstrained). Description is the caller's prose; the host appends the valid status list.

type Endpoint

type Endpoint struct {
	// URL is the endpoint as the host reaches it.
	URL string
	// Host and Port are the address the listener is bound to.
	Host, Port string
	Token      string
}

Endpoint is where one turn reaches its server: what an agent's MCP configuration points at. Token is the bearer credential the endpoint requires; hand it to the turn in an environment variable, not an argument.

func (Endpoint) Via

func (e Endpoint) Via(host string) string

Via returns the endpoint's URL with the host replaced, keeping the port and path: a container reaches a host listener by the host's alias (Docker's host.docker.internal), not by the address it is bound to.

type Lease

type Lease interface {
	Connect(ctx context.Context, client mcp.Implementation) (*mcp.ClientSession, error)
	Close() error
}

Lease is one turn's hold on a started server. Close stops the server, frees its endpoint and returns the error serving ended with, if any; it is safe to call more than once. Connect returns a client session of the endpoint for callers that talk to it themselves; over HTTP it presents the endpoint's token.

type Registry

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

Registry holds typed registrations. Servers are independent snapshots; later registrations do not change existing servers. Handlers must synchronize their own collaborators, but request decoding is private to each call.

func NewRegistry

func NewRegistry(roles []string, empty, unknown RolePolicy) *Registry

NewRegistry takes the complete role set and explicit fallback policies. Invalid policies and empty or duplicate role names are programming errors.

func (*Registry) NewServer

func (r *Registry) NewServer(role string, metadata mcp.Implementation) (*mcp.Server, error)

NewServer constructs a server for role using caller-provided MCP metadata.

type RolePolicy

type RolePolicy int

RolePolicy determines what an empty or unknown role may see.

const (
	RejectRole RolePolicy = iota
	AllTools
	NoTools
)

type StartFunc

type StartFunc func(ctx context.Context, srv *mcp.Server) (Endpoint, Lease, error)

StartFunc is the shape of Start and StartMemory, so an embedder can take either and tests can run without opening listeners.

Jump to

Keyboard shortcuts

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