fix: resolve Docker stdio initialization timeout issue

- Add InitializeRequestSchema handler to MCP server
- Implement stdout flushing for Docker environments
- Create stdio-wrapper for clean JSON-RPC communication
- Update docker-entrypoint.sh to prevent stdout pollution
- Fix logger to check MCP_MODE before level check

These changes ensure the MCP server responds to initialization requests
within Claude Desktop's 60-second timeout when running in Docker.
This commit is contained in:
czlonkowski
2025-06-17 09:12:01 +02:00
parent 75952f94ca
commit a688ad3d14
7 changed files with 211 additions and 82 deletions

View File

@@ -3,6 +3,7 @@ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
import {
CallToolRequestSchema,
ListToolsRequestSchema,
InitializeRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import { existsSync } from 'fs';
import path from 'path';
@@ -102,6 +103,27 @@ export class N8NDocumentationMCPServer {
}
private setupHandlers(): void {
// Handle initialization
this.server.setRequestHandler(InitializeRequestSchema, async () => {
const response = {
protocolVersion: '2024-11-05',
capabilities: {
tools: {},
},
serverInfo: {
name: 'n8n-documentation-mcp',
version: '2.4.1',
},
};
// Debug: Log to stderr to see if handler is called
if (process.env.DEBUG_MCP === 'true') {
console.error('Initialize handler called, returning:', JSON.stringify(response));
}
return response;
});
// Handle tool listing
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: n8nDocumentationToolsFinal,
@@ -915,9 +937,15 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
// Force flush stdout for Docker environments
// Docker uses block buffering which can delay MCP responses
if (!process.stdout.isTTY) {
// Write empty string to force flush
process.stdout.write('', () => {});
if (!process.stdout.isTTY || process.env.IS_DOCKER) {
// Override write to auto-flush
const originalWrite = process.stdout.write.bind(process.stdout);
process.stdout.write = function(chunk: any, encoding?: any, callback?: any) {
const result = originalWrite(chunk, encoding, callback);
// Force immediate flush
process.stdout.emit('drain');
return result;
};
}
logger.info('n8n Documentation MCP Server running on stdio transport');

52
src/mcp/stdio-wrapper.ts Normal file
View File

@@ -0,0 +1,52 @@
#!/usr/bin/env node
/**
* Stdio wrapper for MCP server
* Ensures clean JSON-RPC communication by suppressing all non-JSON output
*/
// Suppress all console output before anything else
const originalConsoleLog = console.log;
const originalConsoleError = console.error;
const originalConsoleWarn = console.warn;
const originalConsoleInfo = console.info;
const originalConsoleDebug = console.debug;
// Override all console methods
console.log = () => {};
console.error = () => {};
console.warn = () => {};
console.info = () => {};
console.debug = () => {};
// Set environment to ensure logger suppression
process.env.MCP_MODE = 'stdio';
process.env.DISABLE_CONSOLE_OUTPUT = 'true';
process.env.LOG_LEVEL = 'error';
// Import and run the server
import { N8NDocumentationMCPServer } from './server-update';
async function main() {
try {
const server = new N8NDocumentationMCPServer();
await server.run();
} catch (error) {
// In case of fatal error, output to stderr only
originalConsoleError('Fatal error:', error);
process.exit(1);
}
}
// Handle uncaught errors silently
process.on('uncaughtException', (error) => {
originalConsoleError('Uncaught exception:', error);
process.exit(1);
});
process.on('unhandledRejection', (reason) => {
originalConsoleError('Unhandled rejection:', reason);
process.exit(1);
});
main();