mcpserver

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package mcpserver bridges core DML tools to the MCP go-sdk. It registers the enabled tools with an mcp.Server, adapts CallToolRequest to tool.Run, and maps core errors to MCP results: business errors (unauthorized, cost-exceeded, unsafe-write, not-found) become IsError results with actionable text, while overload/circuit/internal errors become protocol-level errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler added in v0.1.5

func Handler(s *mcp.Server, cfg HTTPConfig) (http.Handler, error)

Handler returns the fully hardened HTTP handler (token auth, body caps, proxy trust, session identity binding, /mcp, /healthz, optional /metrics) without binding a listener. ServeHTTP serves this same handler; tests and embedders can mount it on their own server so the middleware chain under test is identical to production.

func NewRuntimeServer

func NewRuntimeServer(runtime *bootstrap.Runtime) *mcp.Server

NewRuntimeServer builds a server whose handlers acquire the current app snapshot for every request. The tools it lists follow every snapshot a reload publishes; clients are told the list changed.

func NewServer

func NewServer(app *bootstrap.App) *mcp.Server

NewServer builds an mcp.Server with the app's enabled tools registered.

func ServeHTTP

func ServeHTTP(ctx context.Context, s *mcp.Server, cfg HTTPConfig) error

ServeHTTP runs the server on streamable HTTP with authentication, request hardening (timeouts, header/body caps), a /healthz check, and an optional /metrics endpoint. See HTTPConfig for the security model.

func ServeStdio

func ServeStdio(ctx context.Context, s *mcp.Server) error

ServeStdio runs the server on stdio.

func WithSubject

func WithSubject(ctx context.Context, role string, attrs map[string]any) context.Context

WithSubject attaches a per-request caller identity (role + attributes) to ctx. A trusted transport or gateway sets it after authenticating the caller; ServeHTTP derives it from request headers. Tool handlers read it to build a role- and tenant-scoped context, so a single process is no longer pinned to one role. Exported so custom transports can inject an authenticated subject.

Types

type HTTPAuth added in v0.1.12

type HTTPAuth struct {
	Token             string
	TrustProxyHeaders bool
	TrustedProxyCIDRs []string
	TLSCert           string
	TLSKey            string
	ClientCA          string
	Users             UserDirectory
}

HTTPAuth is how HTTP callers authenticate: the part of HTTPConfig a reload may change while serving (see HTTPConfig.AuthChanges). The address, and serving TLS or not, are fixed for the listener.

type HTTPConfig

type HTTPConfig struct {
	Addr              string
	Token             string // shared-secret bearer token; empty disables token auth
	TrustProxyHeaders bool   // trust X-MCP-Role/X-MCP-Subject identity headers
	TrustedProxyCIDRs []string
	TLSCert           string
	TLSKey            string
	ClientCA          string // PEM bundle; when set, require+verify a client cert (mTLS)
	ReadHeaderTimeout time.Duration
	ReadTimeout       time.Duration
	WriteTimeout      time.Duration
	IdleTimeout       time.Duration
	MaxHeaderBytes    int
	MaxBodyBytes      int64
	Metrics           http.Handler
	SessionTimeout    time.Duration
	OnSessionClosed   func(string)
	// Users resolves configured users; nil means no users are configured.
	// With users, bearer tokens may identify individual users and trusted
	// proxies may send X-MCP-User.
	Users UserDirectory
	// RevokedPrincipals registers a callback receiving users removed or
	// disabled by a reload; their sessions are dropped and rolled back, and
	// the callback returns those sessions.
	RevokedPrincipals func(func([]string) []string)
	// SnapshotReady backs /readyz/snapshot: it returns nil when a
	// configuration snapshot is published and servable. DatabaseReady backs
	// /readyz/db: it returns nil when the configured databases are reachable.
	// Probes fail closed: a nil probe or a probe error yields 503.
	SnapshotReady func(context.Context) error
	DatabaseReady func(context.Context) error
	// Admin, when set, serves /admin/ with its own authentication; MCP bearer
	// tokens do not apply there and admin sessions do not apply to /mcp.
	Admin http.Handler
	// SnapshotStale returns the ID of a published store revision that is not
	// applied (0 when none); it adds X-Snapshot-Stale to /readyz/snapshot.
	SnapshotStale func() int64
	// AuthChanges, when set, receives a function switching to a new
	// authentication while serving, prepared with PrepareHTTPAuth: switching
	// reads no file, so it cannot fail half way. It fails, keeping the
	// current one, only when it would switch TLS on or off.
	AuthChanges func(apply func(PreparedAuth) error)
}

HTTPConfig configures the streamable HTTP transport, including authentication and hardening. Zero-valued timeout/size fields receive safe defaults. Metrics is an optional handler mounted at /metrics.

type PreparedAuth added in v0.1.12

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

PreparedAuth is an authentication PrepareHTTPAuth checked and whose certificates it read, ready to switch to.

func PrepareHTTPAuth added in v0.1.12

func PrepareHTTPAuth(addr string, auth HTTPAuth) (PreparedAuth, error)

PrepareHTTPAuth checks that a listener on addr may authenticate callers as auth does, under the rules ServeHTTP starts with, and reads its certificates: a reload prepares the authentication it would switch to, and fails when it cannot.

func (PreparedAuth) WithUsers added in v0.1.12

func (p PreparedAuth) WithUsers(users UserDirectory) PreparedAuth

WithUsers returns p resolving its users with users, when it has users.

type UserDirectory added in v0.1.11

type UserDirectory interface {
	UserByTokenHash(hash string) (bootstrap.UserIdentity, bool)
	UserByName(name string) (bootstrap.UserIdentity, bool)
}

UserDirectory resolves configured users against the current snapshot. bootstrap.Runtime and bootstrap.App implement it.

Jump to

Keyboard shortcuts

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