UNPKG

mcp-web-ui

Version:

Ultra-lightweight vanilla JavaScript framework for MCP servers - Zero dependencies, perfect security, 2-3KB bundle size

137 lines 4.19 kB
import { WebUISession } from '../types/index.js'; /** * Session Manager that handles both direct and proxy modes * * Direct Mode: Uses local memory for session management * Proxy Mode: Uses TokenRegistry (MongoDB) for distributed session management */ export declare class SessionManager { private readonly VERSION; private localSessions; private usedPorts; private blockedPorts; private portRange; private baseUrl; private protocol; private userSessionLimits; private tokenRegistry?; private mongoClient?; private proxyMode; private serverName; constructor(sessionTimeout?: number, portRange?: [number, number], baseUrl?: string, protocol?: string, blockedPorts?: number[], options?: { proxyMode?: boolean; mongoUrl?: string; mongoDbName?: string; jwtSecret?: string; serverName?: string; logger?: (level: string, message: string, data?: any) => void; }); /** * Initialize proxy mode with MongoDB connection */ private initializeProxyMode; /** * Check if proxy mode is enabled */ isProxyMode(): boolean; /** * Generate composite session key for proper session isolation * Format: userId:serverName:serverType */ private generateSessionKey; /** * Find existing session by composite key */ private findSessionByCompositeKey; /** * Create a new session for a user * Automatically terminates any existing session for the same user */ createSession(userId: string): Promise<WebUISession>; /** * Create a session through the gateway */ private createGatewaySession; /** * Determine the backend host that the gateway can use to reach this UI server * Priority: * 1) MCP_WEB_UI_BACKEND_HOST env var (explicit override) * 2) First non-internal IPv4 address (container/host primary interface) * 3) Fallback to 127.0.0.1 */ private resolveBackendHost; /** * Validate gateway health before attempting operations */ private validateGatewayHealth; /** * Clear stale sessions and ports when gateway is unavailable * This prevents reusing ports from previous gateway sessions that may be outside the configured range */ private clearStaleSessions; /** * Discover a registered server from the gateway */ private discoverRegisteredServer; /** * Create a direct session (original logic) */ private createDirectSession; /** * Get session by token (for authentication) */ getSessionByToken(token: string, updateActivity?: boolean): Promise<WebUISession | null>; /** * Get session by ID */ getSession(sessionId: string): Promise<WebUISession | null>; /** * Get session by user ID (returns first active session for user) * Note: This method is deprecated in favor of composite key lookups * Use findSessionByCompositeKey for proper server isolation */ getSessionByUserId(userId: string): Promise<WebUISession | null>; /** * Get all active sessions */ getActiveSessions(): Promise<WebUISession[]>; /** * Terminate a session */ terminateSession(sessionId: string): Promise<boolean>; /** * Extend session by token */ extendSessionByToken(token: string, extensionMinutes: number): Promise<boolean>; /** * Get session statistics */ getStats(): Promise<{ mode: 'direct' | 'proxy'; totalActiveSessions: number; usedPorts: number[]; sessionsByUser: Record<string, number>; sessionsByServer?: Record<string, number>; }>; /** * Allocate a random port from the available range */ private allocatePort; /** * Free a port when session is terminated */ private freePort; /** * Clean up expired sessions */ cleanupExpiredSessions(): Promise<void>; /** * Shutdown the session manager */ shutdown(): Promise<void>; /** * Log messages with optional data */ private log; } //# sourceMappingURL=SessionManager.d.ts.map