@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
57 lines (56 loc) • 2.23 kB
JavaScript
import { getMcpServers } from "./fetch-pluggedinmcp.js";
import { createPluggedinMCPClient, connectPluggedinMCPClient, } from "./client.js";
import { getSessionKey } from "./utils.js";
const _sessions = {};
// Removed logger
export const getSession = async (sessionKey, uuid, params) => {
if (sessionKey in _sessions) {
return _sessions[sessionKey];
}
else {
// Close existing session for this UUID if it exists with a different hash
const old_session_keys = Object.keys(_sessions).filter((k) => k.startsWith(`${uuid}_`));
await Promise.allSettled(old_session_keys.map(async (old_session_key) => {
await _sessions[old_session_key].cleanup();
delete _sessions[old_session_key];
}));
const { client, transport } = createPluggedinMCPClient(params);
if (!client || !transport) {
return;
}
const newClient = await connectPluggedinMCPClient(client, transport);
if (!newClient) {
return;
}
_sessions[sessionKey] = newClient;
// Maintain global.sessions
if (!global.sessions) {
global.sessions = {};
}
global.sessions[sessionKey] = newClient;
return newClient;
}
};
export const initSessions = async () => {
const serverParams = await getMcpServers(true);
await Promise.allSettled(Object.entries(serverParams).map(async ([uuid, params]) => {
const sessionKey = getSessionKey(uuid, params);
try {
await getSession(sessionKey, uuid, params);
}
catch (error) {
// Log errors during initial session establishment attempt
// logger.error(`Failed to initialize session for ${params.name || uuid} during initSessions:`, error); // Removed logging
}
}));
};
export const cleanupAllSessions = async () => {
await Promise.allSettled(Object.entries(_sessions).map(async ([sessionKey, session]) => {
await session.cleanup();
delete _sessions[sessionKey];
// Clean up from global.sessions
if (global.sessions && global.sessions[sessionKey]) {
delete global.sessions[sessionKey];
}
}));
};