@pluggedin/pluggedin-mcp-proxy
Version:
Unified MCP proxy that aggregates all your MCP servers (STDIO, SSE, Streamable HTTP) into one powerful interface. Access any tool through a single connection, search across unified documents with built-in RAG, and receive notifications from any model. Tes
110 lines (109 loc) • 2.96 kB
JavaScript
import { debugLog } from './debug-log.js';
/**
* Manages MCP server sessions in a centralized, testable way
*/
export class SessionManager {
sessions = new Map();
/**
* Get a session by server UUID
*/
getSessionByServerUuid(serverUuid) {
// Direct lookup by serverUuid
const directSession = this.sessions.get(serverUuid);
if (directSession) {
return directSession;
}
// Fallback to prefix matching for backward compatibility
// TODO: Remove this once all session creation uses serverUuid directly
for (const [key, session] of this.sessions) {
if (key.startsWith(serverUuid + '_')) {
debugLog(`[SessionManager] Using prefix match for ${serverUuid}`);
return session;
}
}
return null;
}
/**
* Get a session by exact key
*/
getSession(key) {
return this.sessions.get(key) || null;
}
/**
* Set a session
*/
setSession(key, session) {
debugLog(`[SessionManager] Setting session for ${key}`);
this.sessions.set(key, session);
}
/**
* Delete a session
*/
deleteSession(key) {
debugLog(`[SessionManager] Deleting session for ${key}`);
return this.sessions.delete(key);
}
/**
* Get all sessions
*/
getAllSessions() {
return new Map(this.sessions);
}
/**
* Clear all sessions
*/
clearSessions() {
debugLog(`[SessionManager] Clearing all sessions`);
this.sessions.clear();
}
/**
* Get session count
*/
getSessionCount() {
return this.sessions.size;
}
/**
* Check if a session exists
*/
hasSession(key) {
return this.sessions.has(key);
}
/**
* Find sessions by predicate
*/
findSessions(predicate) {
const results = [];
for (const [key, session] of this.sessions) {
if (predicate(key, session)) {
results.push([key, session]);
}
}
return results;
}
/**
* Migrate from global.sessions to SessionManager
*/
static fromGlobalSessions() {
const manager = new SessionManager();
const globalSessions = global.sessions;
if (globalSessions && typeof globalSessions === 'object') {
debugLog('[SessionManager] Migrating from global.sessions');
Object.entries(globalSessions).forEach(([key, session]) => {
manager.setSession(key, session);
});
}
return manager;
}
/**
* Export to object for backward compatibility
*/
toObject() {
const obj = {};
this.sessions.forEach((session, key) => {
obj[key] = session;
});
return obj;
}
}
// Create a singleton instance
export const sessionManager = new SessionManager();