From 44e8ea6f9b8dd80e273ea3a8b6437ce06089f1f2 Mon Sep 17 00:00:00 2001 From: Oliver Kieselbach Date: Thu, 2 Apr 2026 22:33:33 +0200 Subject: [PATCH] added sigterm handler --- .../autopilot-monitor-mcp/src/index.ts | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/McpServer/autopilot-monitor-mcp/src/index.ts b/src/McpServer/autopilot-monitor-mcp/src/index.ts index 2d957779..94bcd30e 100644 --- a/src/McpServer/autopilot-monitor-mcp/src/index.ts +++ b/src/McpServer/autopilot-monitor-mcp/src/index.ts @@ -128,10 +128,32 @@ app.all('/mcp', async (req, res) => { } }); -app.listen(PORT, '0.0.0.0', () => { +const server = app.listen(PORT, '0.0.0.0', () => { console.error(`Autopilot-Monitor MCP Server running (Streamable HTTP on port ${PORT})`); console.error(`API URL: ${process.env.AUTOPILOT_API_URL ?? 'https://autopilotmonitor-api.azurewebsites.net'}`); console.error(`Search backend: ${knowledgeBase.name} | Documents: ${knowledgeBase.size} | Rules dir: ${RULES_DIR}`); console.error(`Health: http://localhost:${PORT}/health`); console.error(`MCP endpoint: http://localhost:${PORT}/mcp`); }); + +// --- Graceful shutdown --- + +async function gracefulShutdown(signal: string) { + console.error(`[mcp] Received ${signal}, shutting down gracefully…`); + for (const [id, entry] of transports) { + try { + await entry.transport.close(); + console.error(`[mcp] Closed session ${id}`); + } catch (e) { + console.error(`[mcp] Error closing session ${id}:`, e); + } + } + transports.clear(); + server.close(() => { + console.error('[mcp] HTTP server closed'); + process.exit(0); + }); +} + +process.on('SIGTERM', () => gracefulShutdown('SIGTERM')); +process.on('SIGINT', () => gracefulShutdown('SIGINT'));