fix: extract instance context from HTTP headers for multi-tenant support

- Add header extraction logic in http-server-single-session.ts
- Extract X-N8n-Url, X-N8n-Key, X-Instance-Id, X-Session-Id headers
- Pass extracted context to handleRequest method
- Maintain full backward compatibility (falls back to env vars)
- Add comprehensive tests for header extraction scenarios
- Update documentation with HTTP header specifications

This fixes the bug where instance-specific configuration headers were not
being extracted and passed to the MCP server, preventing the multi-tenant
feature from working as designed in PR #209.

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
czlonkowski
2025-09-20 00:25:40 +02:00
parent f0338ea5ce
commit 424f8ae1ff
3 changed files with 209 additions and 2 deletions

View File

@@ -999,8 +999,31 @@ export class SingleSessionHTTPServer {
sessionType: this.session?.isSSE ? 'SSE' : 'StreamableHTTP',
sessionInitialized: this.session?.initialized
});
await this.handleRequest(req, res);
// Extract instance context from headers if present (for multi-tenant support)
const instanceContext: InstanceContext | undefined =
(req.headers['x-n8n-url'] || req.headers['x-n8n-key']) ? {
n8nApiUrl: req.headers['x-n8n-url'] as string,
n8nApiKey: req.headers['x-n8n-key'] as string,
instanceId: req.headers['x-instance-id'] as string,
sessionId: req.headers['x-session-id'] as string,
metadata: {
userAgent: req.headers['user-agent'],
ip: req.ip
}
} : undefined;
// Log context extraction for debugging (only if context exists)
if (instanceContext) {
logger.debug('Instance context extracted from headers', {
hasUrl: !!instanceContext.n8nApiUrl,
hasKey: !!instanceContext.n8nApiKey,
instanceId: instanceContext.instanceId,
sessionId: instanceContext.sessionId
});
}
await this.handleRequest(req, res, instanceContext);
logger.info('POST /mcp request completed - checking response status', {
responseHeadersSent: res.headersSent,