feat: Enhance Keep-Alive configuration handling (#455)

This commit is contained in:
samanhappy
2025-11-30 09:59:48 +08:00
committed by GitHub
parent 063b081297
commit 8770b9ccfe
14 changed files with 234 additions and 175 deletions

View File

@@ -29,37 +29,7 @@ import { createOAuthProvider } from './mcpOAuthProvider.js';
const servers: { [sessionId: string]: Server } = {};
// Helper function to set up keep-alive ping for SSE connections
const setupKeepAlive = (serverInfo: ServerInfo, serverConfig: ServerConfig): void => {
// Only set up keep-alive for SSE connections
if (!(serverInfo.transport instanceof SSEClientTransport)) {
return;
}
// Clear any existing interval first
if (serverInfo.keepAliveIntervalId) {
clearInterval(serverInfo.keepAliveIntervalId);
}
// Use configured interval or default to 60 seconds for SSE
const interval = serverConfig.keepAliveInterval || 60000;
serverInfo.keepAliveIntervalId = setInterval(async () => {
try {
if (serverInfo.client && serverInfo.status === 'connected') {
await serverInfo.client.ping();
console.log(`Keep-alive ping successful for server: ${serverInfo.name}`);
}
} catch (error) {
console.warn(`Keep-alive ping failed for server ${serverInfo.name}:`, error);
// TODO Consider handling reconnection logic here if needed
}
}, interval);
console.log(
`Keep-alive ping set up for server ${serverInfo.name} with interval ${interval / 1000} seconds`,
);
};
import { setupClientKeepAlive } from './keepAliveService.js';
export const initUpstreamServers = async (): Promise<void> => {
// Initialize OAuth clients for servers with dynamic registration
@@ -596,9 +566,10 @@ export const initializeClientsFromSettings = async (
if (!dataError) {
serverInfo.status = 'connected';
serverInfo.error = null;
// Set up keep-alive ping for SSE connections
setupKeepAlive(serverInfo, expandedConf);
// Set up keep-alive ping for SSE connections via shared service
setupClientKeepAlive(serverInfo, expandedConf).catch((e) =>
console.warn(`Keepalive setup failed for ${name}:`, e),
);
} else {
serverInfo.status = 'disconnected';
serverInfo.error = `Failed to list data: ${dataError} `;
@@ -812,10 +783,9 @@ export const addOrUpdateServer = async (
return { success: false, message: 'Server name already exists' };
}
// If overriding and this is a DXT server (stdio type with file paths),
// we might want to clean up old files in the future
if (exists && config.type === 'stdio') {
// Close existing server connections
// If overriding an existing server, close connections and clear keep-alive timers
if (exists) {
// Close existing server connections (clears keep-alive intervals as well)
closeServer(name);
// Remove from server infos
serverInfos = serverInfos.filter((serverInfo) => serverInfo.name !== name);