Documentation
¶
Overview ¶
Package mcpsession binds an app's MCP sessions to the token that opened them (app-contract.md, MCP tokens and scope). It wraps any MCP Streamable HTTP handler that issues Mcp-Session-Id at initialize and ends a session on DELETE, such as the MCP SDK's StreamableHTTPHandler:
- a session is bound at initialize to the caller's Lodge-Token and Lodge-Scope, and a request for it under another token name or scope answers 404, as does one for a session it does not know;
- each token name keeps at most Max live sessions, and opening one more ends the least recently used;
- a session idle for Idle ends: Sessions sweeps on every request and every Every until Close, so an idle session ends, and frees what the app holds for it, even when no other request comes.
The binding keeps sessions apart; the scope itself is enforced by the app's engine, on the connection its tools use.
Index ¶
Constants ¶
const ( DefaultMax = 8 DefaultIdle = time.Hour DefaultEvery = time.Minute )
The defaults of Options.
const Header = "Mcp-Session-Id"
Header is MCP Streamable HTTP's session header.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Options ¶
type Options struct {
// Max bounds the live sessions per token name: DefaultMax when 0.
Max int
// Idle ends a session that has seen no request for this long:
// DefaultIdle when 0.
Idle time.Duration
// CheckScope parses a scope at initialize. An error answers 403, as
// the contract requires for a scope the app cannot parse. Nil admits
// every scope.
CheckScope func(scope string) error
// Every is how often idle sessions are swept between requests:
// DefaultEvery when 0.
Every time.Duration
// Now is the clock, for tests: time.Now when nil.
Now func() time.Time
// Logger receives ended sessions. Nil discards them.
Logger *slog.Logger
// contains filtered or unexported fields
}
Options configures Sessions. The zero value is the contract's defaults.
type Sessions ¶
type Sessions struct {
// contains filtered or unexported fields
}
Sessions is an http.Handler that binds next's MCP sessions.
func (*Sessions) Close ¶
Close stops the sweeps between requests, and waits for one under way. Live sessions stay, and requests still pass; an app closes Sessions as it stops serving.