Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(mcp): protect stdout from stray console.log in stdio MCP mode (#1910) (#1998)

rUv0873f8146354
diff --git a/v3/@claude-flow/cli/src/mcp-server.ts b/v3/@claude-flow/cli/src/mcp-server.ts
index 661a05429b..024c52cebe 100644
--- a/v3/@claude-flow/cli/src/mcp-server.ts
+++ b/v3/@claude-flow/cli/src/mcp-server.ts
@@ -309,6 +309,42 @@ export class MCPServerManager extends EventEmitter {
* Handles stdin/stdout directly like V2 implementation
*/
private async startStdioServer(): Promise<void> {
+ // ruflo#1910 — protect the JSON-RPC stdout from any stray
+ // console.log/info/debug emitted by lazily-loaded modules
+ // (@ruvector/router, @claude-flow/neural, transformers.js, ONNX,
+ // semantic-router init, etc.). Codex closes the MCP transport
+ // the moment it sees a non-JSON line on stdout, and one such
+ // line during a tool batch bricked the whole session.
+ //
+ // Strategy: replace console.log/info/debug with stderr writers
+ // for the rest of the process. JSON-RPC frames go out via the
+ // dedicated `writeFrame()` helper below (process.stdout.write
+ // with the original native binding, NOT console.log), so the
+ // hijack can't accidentally redirect protocol frames too.
+ process.env.MCP_STDIO_MODE = '1';
+ const originalLog = console.log; // eslint-disable-line no-console
+ console.log = (...args: unknown[]) => process.stderr.write('[stdout→stderr] ' + args.map(a => typeof a === 'string' ? a : JSON.stringify(a)).join(' ') + '\n');
+ console.info = (...args: unknown[]) => process.stderr.write('[stdout→stderr] ' + args.map(a => typeof a === 'string' ? a : JSON.stringify(a)).join(' ') + '\n');
+ console.debug = (...args: unknown[]) => process.stderr.write('[stdout→stderr] ' + args.map(a => typeof a === 'string' ? a : JSON.stringify(a)).join(' ') + '\n');
+
+ /** Send a single JSON-RPC frame to the real stdout. Use this instead
+ * of `console.log` so the hijack above can't redirect protocol frames. */
+ const writeFrame = (msg: unknown): void => {
+ process.stdout.write(JSON.stringify(msg) + '\n');
+ };
+ // Reference originalLog to keep the eslint-disable meaningful — also
+ // gives us an escape hatch if a test wants to verify it was replaced.
+ void originalLog;
+
+ // Catch fatal errors that would otherwise close the transport
+ // mid-batch with no JSON-RPC error returned to the client.
+ process.on('uncaughtException', (err) => {
+ process.stderr.write(`[mcp-stdio] uncaughtException: ${err.stack || err.message}\n`);
+ });
+ process.on('unhandledRejection', (reason) => {
+ process.stderr.write(`[mcp-stdio] unhandledRejection: ${reason instanceof Error ? reason.stack || reason.message : String(reason)}\n`);
+ });
+
// Import the tool registry
const { listMCPTools, callMCPTool, hasTool } = await import('./mcp-client.js');
@@ -364,7 +400,7 @@ export class MCPServerManager extends EventEmitter {
}));
// Send server initialization notification
- console.log(JSON.stringify({
+ writeFrame({
jsonrpc: '2.0',
method: 'server.initialized',
params: {
@@ -377,7 +413,7 @@ export class MCPServerManager extends EventEmitter {
},
},
},
- }));
+ });
// Handle stdin messages (S-5: bounded buffer to prevent OOM)
const MAX_BUFFER_SIZE = 10 * 1024 * 1024; // 10MB
@@ -391,10 +427,10 @@ export class MCPServerManager extends EventEmitter {
`[${new Date().toISOString()}] ERROR [claude-flow-mcp] Buffer exceeded ${MAX_BUFFER_SIZE} bytes, rejecting`
);
buffer = '';
- console.log(JSON.stringify({
+ writeFrame({
jsonrpc: '2.0',
error: { code: -32600, message: 'Request too large' },
- }));
+ });
return;
}
@@ -408,7 +444,7 @@ export class MCPServerManager extends EventEmitter {
const message = JSON.parse(line);
const response = await this.handleMCPMessage(message, sessionId);
if (response) {
- console.log(JSON.stringify(response));
+ writeFrame(response);
}
} catch (error) {
console.error(