/** * Session Manager for stateful Streamable HTTP MCP transport. * * Each client session owns a StreamableHTTPServerTransport + McpServer pair. * Sessions are identified by mcp-session-id header (generated by the SDK). * Notifications are broadcast to all active sessions via their McpServer instances. */ import type { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js'; import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; interface SessionState { sessionId: string; transport: StreamableHTTPServerTransport; server: McpServer; createdAt: number; lastAccessedAt: number; isClosing: boolean; activeSseCount: number; lastPingedAt: number; pingPending: boolean; clientName?: string; clientVersion?: string; protocolVersion?: string; createdByMethod?: string; lastMethod?: string; } export declare class SessionManager { private sessions; private cleanupTimer; private pendingClientMetadata; /** * Registers a newly created session. */ addSession(sessionId: string, transport: StreamableHTTPServerTransport, server: McpServer): void; /** * Looks up an existing session by ID and updates lastAccessedAt. */ getSession(sessionId: string): SessionState | undefined; /** * Marks an SSE stream as opened for the given session. * Prevents the session from being cleaned up while the stream is active. */ markSseOpened(sessionId: string): void; /** * Marks an SSE stream as closed for the given session. */ markSseClosed(sessionId: string): void; /** * Removes a session (called on DELETE or onsessionclosed). */ removeSession(sessionId: string): void; private pendingBroadcasts; private readonly BROADCAST_DEBOUNCE_MS; /** * Broadcasts a notification to all active sessions. * Debounced: same-type notifications within BROADCAST_DEBOUNCE_MS are coalesced. * @param method - 'tools' for sendToolListChanged, 'resources' for sendResourceListChanged */ broadcastNotification(method: 'tools' | 'resources'): void; /** * Removes sessions that have been idle longer than SESSION_TIMEOUT_MS. */ private cleanupStaleSessions; private ensureCleanupTimer; /** * Shuts down all sessions. Called on server shutdown. */ shutdownAll(): void; /** Returns the current session count (for diagnostics). */ get sessionCount(): number; /** * Stores client metadata against a McpServer instance so that * addSession() can consume it when the session is registered later. */ storePendingClientMetadata(server: McpServer, metadata: { clientName: string; clientVersion: string; protocolVersion: string; }): void; /** * Returns metadata for all active sessions. Does not expose internal * transport or server references. */ getAllSessions(): Array<{ sessionId: string; clientName?: string; clientVersion?: string; protocolVersion?: string; createdByMethod?: string; lastMethod?: string; createdAt: string; lastAccessedAt: string; isClosing: boolean; activeSseCount: number; }>; /** Updates the last HTTP method seen for a session. */ updateSessionMethod(sessionId: string, method: string): void; } /** Singleton instance. */ export declare const sessionManager: SessionManager; export {}; //# sourceMappingURL=session-manager.d.ts.map