From 76f9b9254bbfc4c8f4ed256d636df290bef056a6 Mon Sep 17 00:00:00 2001 From: disonjer Date: Thu, 28 May 2026 13:30:03 +0200 Subject: [PATCH 1/2] fix(mcp): redirect console.log/warn to stderr in --mcp stdio mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MCP stdio transport uses stdout exclusively for JSON-RPC messages. DB init logs ([DB] ...), credential loader messages ([CREDENTIALS] ...), and env loading output (📋 Loaded env from ...) were written to stdout via console.log, corrupting the protocol and causing JSON parse errors in MCP clients (e.g. Hermes Agent). Redirect console.log/warn to stderr early in the CLI entry point when --mcp flag is present, before loadEnvFile() and any DB initialization. --- bin/omniroute.mjs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bin/omniroute.mjs b/bin/omniroute.mjs index e536ac30e03..4f5bb0bc274 100755 --- a/bin/omniroute.mjs +++ b/bin/omniroute.mjs @@ -28,6 +28,15 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const ROOT = join(__dirname, ".."); +// MCP stdio transport uses stdout exclusively for JSON-RPC messages. +// Redirect console.log/warn to stderr early (before loadEnvFile and DB init) +// so no startup output corrupts the protocol. +if (process.argv.includes("--mcp")) { + const _stderrWrite = (msg) => process.stderr.write(msg + "\n"); + console.log = (...a) => _stderrWrite(a.join(" ")); + console.warn = (...a) => _stderrWrite(a.join(" ")); +} + function loadEnvFile() { const envPaths = []; From fd778ec09c165db9da3c645548c1322beed3b1f6 Mon Sep 17 00:00:00 2001 From: disonjer Date: Thu, 28 May 2026 13:49:31 +0200 Subject: [PATCH 2/2] refactor: use Node.js Console class for stderr redirect Address review feedback from gemini-code-assist: use Node's built-in Console class instead of manual join() to preserve printf-style formatting (%s, %d, %o) and object inspection. --- bin/omniroute.mjs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/omniroute.mjs b/bin/omniroute.mjs index 4f5bb0bc274..b03bb3626ce 100755 --- a/bin/omniroute.mjs +++ b/bin/omniroute.mjs @@ -32,9 +32,10 @@ const ROOT = join(__dirname, ".."); // Redirect console.log/warn to stderr early (before loadEnvFile and DB init) // so no startup output corrupts the protocol. if (process.argv.includes("--mcp")) { - const _stderrWrite = (msg) => process.stderr.write(msg + "\n"); - console.log = (...a) => _stderrWrite(a.join(" ")); - console.warn = (...a) => _stderrWrite(a.join(" ")); + const { Console } = await import("node:console"); + const stderrConsole = new Console({ stdout: process.stderr, stderr: process.stderr }); + console.log = stderrConsole.log.bind(stderrConsole); + console.warn = stderrConsole.warn.bind(stderrConsole); } function loadEnvFile() {