fix: memory leak in session removal - close MCP server properly (#471) (#472)

- Add close() method to N8NDocumentationMCPServer that:
  - Calls server.close() (MCP SDK cleanup)
  - Clears internal cache
  - Nullifies service references to help GC
- Update removeSession() to call server.close() before releasing references

Root cause: removeSession() deleted server from map but didn't call cleanup
Evidence: Production server memory grew ~1GB in 43 minutes (10% to 35%)

Conceived by Romuald Członkowski - https://www.aiadvisors.pl/en

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Romuald Członkowski <romualdczlonkowski@MacBook-Pro-Romuald.local>
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Romuald Członkowski
2025-12-05 18:30:51 +01:00
committed by GitHub
parent 60479e0eb4
commit cdaa29e7a2
12 changed files with 104 additions and 10 deletions

13
dist/mcp/server.js vendored
View File

@@ -147,6 +147,19 @@ class N8NDocumentationMCPServer {
});
this.setupHandlers();
}
async close() {
try {
await this.server.close();
this.cache.clear();
this.db = null;
this.repository = null;
this.templateService = null;
this.earlyLogger = null;
}
catch (error) {
logger_1.logger.warn('Error closing MCP server', { error: error instanceof Error ? error.message : String(error) });
}
}
async initializeDatabase(dbPath) {
try {
if (this.earlyLogger) {