Documentation
¶
Overview ¶
Package mcpguard is the access check in front of an MCP endpoint (Family Hub docs/PLAN.md §5.6), taken from Family Hub's internal/gateway:
- Guard.Middleware finds the resource of the request path, checks the bearer access token, the person behind it and the resource switch, answers 401 the way the MCP authorization spec wants it, and puts the caller's core.AccessInfo and core.Person in the request context (AccessFrom, PersonFrom);
- the helpers an application's MCP handler or proxy uses: the header policy (RequestHeaders, ResponseHeaders), the body cap (ReadBody), the in-flight cap (Guard.Admit), the failure limit for other doors (Guard.Refuse) and the relay of an answer that flushes server-sent events and pings an idle stream (Guard.Relay).
The application keeps what is its own: resolving the resource to something that runs (its 404s go in front of the middleware), MCP session ids, the identity it hands its servers, and its call log.
Index ¶
- Constants
- func AccessFrom(ctx context.Context) (core.AccessInfo, bool)
- func PersonFrom(ctx context.Context) (core.Person, bool)
- func ReadBody(w http.ResponseWriter, r *http.Request) ([]byte, bool)
- func RequestHeaders(dst, src http.Header)
- func ResponseHeaders(dst, src http.Header)
- func SecureHeaders(h http.Header)
- type Guard
- func (g *Guard) Admit(w http.ResponseWriter, a core.AccountID, id core.ResourceID) (release func(), ok bool)
- func (g *Guard) Middleware(next http.Handler) http.Handler
- func (g *Guard) Refuse(w http.ResponseWriter, r *http.Request, refusal func())
- func (g *Guard) Relay(ctx context.Context, w http.ResponseWriter, status int, body io.Reader) error
- type Options
- type People
Constants ¶
const MaxBodyBytes = 1 << 20
MaxBodyBytes is the largest request body ReadBody takes.
const Scope = "mcp"
Scope is the OAuth scope of MCP traffic, which a 401 tells the assistant to ask for.
const SessionHeader = "Mcp-Session-Id"
SessionHeader is the MCP session id (Streamable HTTP, session management). It passes both ways; an application that binds session ids to a grant (Family Hub) unwraps it in the request before RequestHeaders and wraps it in the answer after ResponseHeaders.
Variables ¶
This section is empty.
Functions ¶
func AccessFrom ¶
func AccessFrom(ctx context.Context) (core.AccessInfo, bool)
AccessFrom returns the access of the token that Guard.Middleware let through.
func PersonFrom ¶
PersonFrom returns the person behind the token that Guard.Middleware let through, as the store had them then.
func ReadBody ¶
ReadBody buffers the request body: 413 above MaxBodyBytes, whether Content-Length says so or the body runs over, and 400 when it cannot be read. ok is false once an answer is written.
func RequestHeaders ¶
RequestHeaders copies to dst the assistant's request headers in src that an MCP server may see: Content-Type, Accept, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, Mcp-Method, Mcp-Name and every Mcp-Param-<name>.
func ResponseHeaders ¶
ResponseHeaders copies to dst the MCP server's answer headers in src that the assistant may see: Content-Type and Mcp-Session-Id.
func SecureHeaders ¶
SecureHeaders marks an answer as data that no browser should sniff, render or frame, and that no cache may keep: whatever an MCP server sends is served from the application's own origin, and is one family's alone. Middleware sets them on every answer; Relay replaces Cache-Control with "no-cache, no-store" on an event stream. An application calls it itself on the answers it writes in front of the middleware.
Types ¶
type Guard ¶
type Guard struct {
// contains filtered or unexported fields
}
Guard is the access check of the MCP endpoints and the limits they share.
func (*Guard) Admit ¶
func (g *Guard) Admit(w http.ResponseWriter, a core.AccountID, id core.ResourceID) (release func(), ok bool)
Admit takes a place for a call of account a in resource id among the 16 calls that may be inside it at once, or answers 429 with Retry-After. The caller gives the place back with release when the call ends (a second release does nothing). Every door into the same resource admits its calls here (Family Hub: its legacy REST door too), so that they share the places.
func (*Guard) Middleware ¶
Middleware lets a request through to next only with a live access token for the resource of its path. In order:
- every answer gets SecureHeaders;
- a path outside Paths is 404;
- no bearer token is 401 unauthorized;
- a token that is unknown, dead or for another resource, or whose person is gone, disabled or in another account, is 401 invalid_token (429 when the client's address has presented too many such tokens, see Refuse);
- an account that may not use the resource now is 403;
- a failure of the services behind it is 500, logged.
The 401s carry WWW-Authenticate with the resource's protected-resource metadata and Scope. The middleware does not check that the resource exists: an application with several resources answers its own 404 in front of it. next gets a request whose context holds the caller (AccessFrom, PersonFrom); in a framework that keeps its own request (Gin), read it from the *http.Request next is called with.
func (*Guard) Refuse ¶
func (g *Guard) Refuse(w http.ResponseWriter, r *http.Request, refusal func())
Refuse answers a credential the application does not know with refusal, unless the client's address has presented 30 such credentials in the last minute: 429 with Retry-After then. Every door of an application that takes credentials calls it on a refusal (Family Hub: its legacy REST door too), so that the failures of all doors count together; a request without a credential is no failure. A valid credential is never limited: the assistants call from shared egress addresses, and junk sent from one of them must not stop every family's connector there.
func (*Guard) Relay ¶
Relay sends an MCP server's answer to the assistant: status, then body as it comes. The caller has put the headers that pass on w already (ResponseHeaders). An event stream (Content-Type text/event-stream) gets Cache-Control "no-cache, no-store" and X-Accel-Buffering "no", its status and headers go out at once, every chunk is flushed as it arrives, and a stream that stays silent for the ping interval between two events gets ": ping" (Cloudflare cuts an answer that sends nothing for 100 s). Relay returns at the end of body (nil), on a read or write error, or, for an event stream, when ctx ends; ctx is the assistant's request's, so the caller tells a client that went away by ctx.Err(). The caller closes body.
type Options ¶
type Options struct {
Site core.Site // PublicURL for resource_metadata, ClientIP for the failure limit
Paths core.ResourcePaths // the resource of a request path (Family Hub: core.PerResource{Prefix: "/mcp/"})
Tokens core.TokenValidator // the access tokens (package oauth)
People People // a person who is gone, disabled or moved makes their tokens worthless
Resources core.ResourceCatalog // Available: whether the token's account may use the resource now (403)
// PingInterval is how long an event stream may stay silent before Relay writes ": ping" into it; 0 means 25 s,
// under Cloudflare's 100 s cut.
PingInterval time.Duration
Log *slog.Logger // nil discards
Clock core.Clock // nil is time.Now
}
Options configure a Guard.