Documentation
¶
Overview ¶
Package session provides per-session subprocess management for MCP servers.
Each upstream MCP session gets its own isolated subprocess connected via the go-sdk CommandTransport. The Manager tracks the mapping from session IDs to downstream ClientSessions and handles lifecycle (spawn, teardown, cleanup).
Index ¶
- Variables
- type Manager
- func (m *Manager) AddOnSessionRemoved(fn func(sessionID string))
- func (m *Manager) AdmissionStatus() (atCapacity bool, current int, max int)
- func (m *Manager) CloseAll()
- func (m *Manager) GetSession(sessionID string) *TrackedSession
- func (m *Manager) RemoveSession(sessionID string) error
- func (m *Manager) SessionCount() int
- func (m *Manager) Sessions() []string
- func (m *Manager) SetOnSessionRemoved(fn func(sessionID string))
- func (m *Manager) SpawnSession(ctx context.Context, sessionID string, clientOpts ...*mcp.ClientOptions) (*mcp.ClientSession, error)
- func (m *Manager) StartReaper(ctx context.Context, checkInterval time.Duration)
- func (m *Manager) TouchSession(sessionID string)
- type SharedSessionManager
- func (sm *SharedSessionManager) AdmissionStatus() (atCapacity bool, current int, max int)
- func (sm *SharedSessionManager) BeginRequest(ctx context.Context, sessionID string) (func(), error)
- func (sm *SharedSessionManager) CloseAll()
- func (sm *SharedSessionManager) Downstream() *mcp.ClientSession
- func (sm *SharedSessionManager) EnsureReady(ctx context.Context) error
- func (sm *SharedSessionManager) GetOrCreateSession(ctx context.Context, sessionID string) (*mcp.ClientSession, error)
- func (sm *SharedSessionManager) HasDownstream() bool
- func (sm *SharedSessionManager) ProcessStatus() (int, time.Time)
- func (sm *SharedSessionManager) RefCount() int
- func (sm *SharedSessionManager) RemoveSession(sessionID string) error
- func (sm *SharedSessionManager) SessionCount() int
- func (sm *SharedSessionManager) SetOnSessionExpired(fn func(sessionID string))
- func (sm *SharedSessionManager) SetReachabilityStore(store *reachability.Store)
- func (sm *SharedSessionManager) StartHealthProbe(ctx context.Context)
- func (sm *SharedSessionManager) StartReaper(ctx context.Context, checkInterval time.Duration)
- func (sm *SharedSessionManager) Subscribe(sessionID string, sub SharedSubscriber)
- func (sm *SharedSessionManager) TouchSession(sessionID string)
- func (sm *SharedSessionManager) Unsubscribe(sessionID string)
- type SharedStartingError
- type SharedStartupError
- type SharedSubscriber
- type TrackedSession
Constants ¶
This section is empty.
Variables ¶
var ( ErrSessionExists = errors.New("session: session already exists") ErrSessionNotFound = errors.New("session: session not found") ErrMaxSessions = errors.New("session: maximum concurrent sessions reached") )
Errors returned by session Manager operations.
Functions ¶
This section is empty.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager manages per-session subprocess lifecycle for a single MCP server. Each upstream session gets its own subprocess, connected via CommandTransport.
func NewManager ¶
NewManager creates a new session Manager for the given server.
func (*Manager) AddOnSessionRemoved ¶ added in v1.5.11
AddOnSessionRemoved registers an additional removal observer that fires after the primary callback, before session IO teardown. Shared surfaces — a slot-group multiplexer routed over a member endpoint's manager — use this to observe removals without displacing the member endpoint's own callback.
func (*Manager) AdmissionStatus ¶
AdmissionStatus returns current admission-control state. atCapacity is true only when MaxSessions is configured (>0) and reached.
func (*Manager) CloseAll ¶
func (m *Manager) CloseAll()
CloseAll terminates all sessions and their subprocesses.
func (*Manager) GetSession ¶
func (m *Manager) GetSession(sessionID string) *TrackedSession
GetSession returns the tracked session for the given session ID, or nil.
func (*Manager) RemoveSession ¶
RemoveSession terminates the downstream subprocess and removes the session. It gracefully closes the ClientSession (which closes stdin, waits, then SIGTERM/SIGKILL).
func (*Manager) SessionCount ¶
SessionCount returns the number of active sessions.
func (*Manager) SetOnSessionRemoved ¶
SetOnSessionRemoved registers the primary callback that fires before any session's downstream connection is closed. This is called from all removal paths: RemoveSession, reaper, and CloseAll. The callback receives the session ID and runs outside any Manager lock, allowing the proxy layer to gate further dispatches before the SDK connection is torn down. A second registration replaces the first: each proxy surface owns one primary callback for its endpoint.
func (*Manager) SpawnSession ¶
func (m *Manager) SpawnSession(ctx context.Context, sessionID string, clientOpts ...*mcp.ClientOptions) (*mcp.ClientSession, error)
SpawnSession creates a new downstream subprocess for the given session ID. It connects via CommandTransport, performs the initialize handshake, and returns the ready-to-use ClientSession. If clientOpts is non-nil, it is used to configure the downstream Client (e.g., for notification handlers).
func (*Manager) StartReaper ¶
StartReaper starts a background goroutine that periodically checks for idle and TTL-expired sessions and removes them. The reaper stops when the context is cancelled.
func (*Manager) TouchSession ¶
TouchSession updates the LastActivity timestamp for valid application activity on the given session.
type SharedSessionManager ¶
type SharedSessionManager struct {
// contains filtered or unexported fields
}
SharedSessionManager maintains a single downstream subprocess shared across all upstream sessions for stateless MCP servers.
func NewSharedSessionManager ¶
func NewSharedSessionManager(serverName string, cfg *config.ServerConfig, logger *slog.Logger, idleTimeout time.Duration, m metrics.ServerMetricsReporter) *SharedSessionManager
NewSharedSessionManager creates a new shared session manager. idleTimeout controls the idle reap behavior: when refCount drops to 0, the downstream subprocess is torn down after this duration. 0 disables idle reaping.
func (*SharedSessionManager) AdmissionStatus ¶
func (sm *SharedSessionManager) AdmissionStatus() (atCapacity bool, current int, max int)
AdmissionStatus returns current admission-control state.
func (*SharedSessionManager) BeginRequest ¶ added in v1.5.5
func (sm *SharedSessionManager) BeginRequest(ctx context.Context, sessionID string) (func(), error)
BeginRequest marks an application request as in flight until the returned release function runs, including its readiness wait. Only dispatched calls contribute to activeRequests, so queued calls cannot starve a reserved drain.
func (*SharedSessionManager) CloseAll ¶
func (sm *SharedSessionManager) CloseAll()
CloseAll terminates the shared downstream subprocess and clears all state.
func (*SharedSessionManager) Downstream ¶
func (sm *SharedSessionManager) Downstream() *mcp.ClientSession
Downstream returns the current downstream session (may be nil).
func (*SharedSessionManager) EnsureReady ¶ added in v1.5.23
func (sm *SharedSessionManager) EnsureReady(ctx context.Context) error
EnsureReady starts the same single-flight startup used by admission without reserving an upstream session. Concurrent callers share its result, bounded by their own contexts and the manager's startup deadline.
func (*SharedSessionManager) GetOrCreateSession ¶
func (sm *SharedSessionManager) GetOrCreateSession(ctx context.Context, sessionID string) (*mcp.ClientSession, error)
GetOrCreateSession returns the shared downstream session, lazily spawning it on first use. The sessionID is tracked for admission control and refcounting.
func (*SharedSessionManager) HasDownstream ¶
func (sm *SharedSessionManager) HasDownstream() bool
HasDownstream reports whether a downstream session is currently active.
func (*SharedSessionManager) ProcessStatus ¶ added in v1.5.23
func (sm *SharedSessionManager) ProcessStatus() (int, time.Time)
ProcessStatus returns only live process identity, never arguments or env.
func (*SharedSessionManager) RefCount ¶
func (sm *SharedSessionManager) RefCount() int
RefCount returns the current upstream refcount.
func (*SharedSessionManager) RemoveSession ¶
func (sm *SharedSessionManager) RemoveSession(sessionID string) error
RemoveSession removes the upstream session from tracking and decrements refcount. If refCount reaches 0 and idle reap is enabled, starts the idle reap timer.
func (*SharedSessionManager) SessionCount ¶
func (sm *SharedSessionManager) SessionCount() int
SessionCount returns the number of tracked upstream sessions.
func (*SharedSessionManager) SetOnSessionExpired ¶ added in v1.5.5
func (sm *SharedSessionManager) SetOnSessionExpired(fn func(sessionID string))
SetOnSessionExpired registers the callback used when a shared upstream session exceeds session_timeout. The callback owns proxy-session cleanup.
func (*SharedSessionManager) SetReachabilityStore ¶ added in v1.5.0
func (sm *SharedSessionManager) SetReachabilityStore(store *reachability.Store)
SetReachabilityStore configures the optional store used by health probes. A nil store disables reporting while leaving the probe behavior unchanged.
func (*SharedSessionManager) StartHealthProbe ¶
func (sm *SharedSessionManager) StartHealthProbe(ctx context.Context)
StartHealthProbe starts a background goroutine that periodically probes the shared downstream for liveness. If the probe fails, the downstream is respawned.
func (*SharedSessionManager) StartReaper ¶ added in v1.5.5
func (sm *SharedSessionManager) StartReaper(ctx context.Context, checkInterval time.Duration)
StartReaper starts the shared upstream-session idle reaper.
func (*SharedSessionManager) Subscribe ¶
func (sm *SharedSessionManager) Subscribe(sessionID string, sub SharedSubscriber)
Subscribe registers callbacks for downstream replacement and tool, resource, and prompt list changes.
func (*SharedSessionManager) TouchSession ¶ added in v1.5.5
func (sm *SharedSessionManager) TouchSession(sessionID string)
TouchSession records valid application activity for one upstream session. Protocol pings, notifications, responses, and GET/SSE requests do not call this method.
func (*SharedSessionManager) Unsubscribe ¶
func (sm *SharedSessionManager) Unsubscribe(sessionID string)
Unsubscribe removes the downstream callbacks for the given session ID.
type SharedStartingError ¶ added in v1.5.23
SharedStartingError is a pre-dispatch refusal. Retrying admission is safe; it says nothing about the outcome of a previously dispatched tool call.
func (*SharedStartingError) Error ¶ added in v1.5.23
func (e *SharedStartingError) Error() string
func (*SharedStartingError) Unwrap ¶ added in v1.5.23
func (e *SharedStartingError) Unwrap() error
type SharedStartupError ¶ added in v1.5.23
SharedStartupError preserves the startup cause without exposing command, environment, or peer-provided error text at the HTTP admission boundary.
func (*SharedStartupError) Error ¶ added in v1.5.23
func (e *SharedStartupError) Error() string
func (*SharedStartupError) Unwrap ¶ added in v1.5.23
func (e *SharedStartupError) Unwrap() error
type SharedSubscriber ¶ added in v1.6.0
type SharedSubscriber struct {
}
SharedSubscriber receives notifications for one upstream session.
type TrackedSession ¶
type TrackedSession struct {
SessionID string
Downstream *mcp.ClientSession
Client *mcp.Client
CreatedAt time.Time // When the session was spawned
LastActivity time.Time // Last time the session was actively used
}
TrackedSession holds the downstream client session and metadata for a single upstream MCP session.