UNPKG

n8n-mcp

Version:

Integration between n8n workflow automation and Model Context Protocol (MCP)

162 lines 4.94 kB
#!/usr/bin/env node "use strict"; const fs = require('fs'); const path = require('path'); const logFile = path.join(process.cwd(), 'fixed-protocol.log'); function log(message) { const timestamp = new Date().toISOString(); fs.appendFileSync(logFile, `[${timestamp}] ${message}\n`); } log('fixed-protocol.ts started'); log(`Node version: ${process.version}`); log(`Process argv: ${JSON.stringify(process.argv)}`); class MCPServer { constructor() { this.buffer = ''; this.setupStdio(); } setupStdio() { log('Setting up stdio handlers'); process.stdin.setEncoding('utf8'); process.stdin.on('data', (chunk) => { this.buffer += chunk; this.processBuffer(); }); process.stdin.on('end', () => { log('stdin ended'); process.exit(0); }); process.stdin.on('error', (err) => { log(`stdin error: ${err}`); process.exit(1); }); process.stdout.on('error', (err) => { log(`stdout error: ${err}`); if (err.code !== 'EPIPE') { process.exit(1); } }); } processBuffer() { const lines = this.buffer.split('\n'); this.buffer = lines.pop() || ''; for (const line of lines) { if (line.trim()) { this.handleLine(line); } } } handleLine(line) { try { log(`Received line: ${line}`); const message = JSON.parse(line); this.handleMessage(message); } catch (err) { log(`Error parsing JSON: ${err}`); } } handleMessage(message) { log(`Handling message: ${JSON.stringify(message)}`); if (message.method === 'initialize') { const response = { jsonrpc: '2.0', id: message.id, result: { protocolVersion: message.params.protocolVersion || '2024-11-05', capabilities: { tools: {}, prompts: {} }, serverInfo: { name: 'n8n-mcp', version: '2.7.5' } } }; this.sendResponse(response); setImmediate(() => { this.sendNotification('initialized', {}); }); } else if (message.method === 'tools/list') { const response = { jsonrpc: '2.0', id: message.id, result: { tools: [ { name: 'test_tool', description: 'A test tool', inputSchema: { type: 'object', properties: {} } } ] } }; this.sendResponse(response); } else if (message.method === 'prompts/list') { const response = { jsonrpc: '2.0', id: message.id, result: { prompts: [] } }; this.sendResponse(response); } else { log(`Unknown method: ${message.method}`); const error = { jsonrpc: '2.0', id: message.id, error: { code: -32601, message: 'Method not found' } }; this.sendResponse(error); } } sendResponse(response) { const responseStr = JSON.stringify(response); log(`Sending response: ${responseStr}`); try { process.stdout.write(responseStr + '\n'); } catch (err) { log(`Error writing response: ${err}`); } } sendNotification(method, params) { const notification = { jsonrpc: '2.0', method, params }; const notificationStr = JSON.stringify(notification); log(`Sending notification: ${notificationStr}`); try { process.stdout.write(notificationStr + '\n'); } catch (err) { log(`Error writing notification: ${err}`); } } } process.on('uncaughtException', (error) => { log(`Uncaught exception: ${error}`); log(`Stack: ${error.stack}`); }); process.on('unhandledRejection', (reason, promise) => { log(`Unhandled rejection at: ${promise}, reason: ${reason}`); }); setInterval(() => { }, 60000); log('Starting MCP server'); const server = new MCPServer(); log('MCP server started and waiting for messages'); //# sourceMappingURL=fixed-protocol.js.map