mcpsession

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 6 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	DefaultMax   = 8
	DefaultIdle  = time.Hour
	DefaultEvery = time.Minute
)

The defaults of Options.

View Source
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 New

func New(next http.Handler, opts Options) *Sessions

New returns Sessions over next, and starts its sweeps; Close stops them.

func (*Sessions) Close

func (s *Sessions) Close() error

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.

func (*Sessions) Len

func (s *Sessions) Len() int

Len returns the number of live sessions, for tests.

func (*Sessions) ServeHTTP

func (s *Sessions) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

Jump to

Keyboard shortcuts

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