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:
@@ -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
52
src/mcp/stdio-wrapper.ts
Normal 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();
|
||||
Reference in New Issue
Block a user