MCP: `--mcp --verbose` writes logger output to stdout, corrupting the JSON-RPC channel
What happens
repomix --mcp --verbose puts the logger's normal output on stdout — the same file descriptor
the MCP stdio transport uses for JSON-RPC. Measured on main @ 9f01703a / Node v24.18.0 / Windows
11, driving the real server:
$ node probe.mjs .
stdout lines: 2 non-JSON-RPC: 0
$ node probe.mjs . --verbose
stdout lines: 19 non-JSON-RPC: 17Those 17 lines are the logger, e.g.:
directories: [ '.' ]
cwd: D:\Desktop\pr\rpx
options: {
enableFileProcessors: true,
fileSummary: true,
...and they are interleaved with the two legitimate JSON-RPC responses, so the stream reads:
garbage · garbage · {"jsonrpc":"2.0","id":1,...} · garbage · ….
Why
The logger writes everything except error through console.log, i.e. to stdout:
src/shared/logger.ts:36—error()->console.errorsrc/shared/logger.ts:42/48/54/60/66/72/78—warn/success/info/log/note/debug/trace->console.log
and --verbose sets DEBUG, which enables the trace()/debug() calls that the MCP path makes
about itself:
src/cli/actions/mcpAction.ts:6—logger.trace('Starting Repomix MCP server…')src/mcp/mcpServer.ts:120—logger.trace('Repomix MCP Server running on stdio')src/mcp/mcpServer.ts:107—logger.trace('Repomix MCP Server shutdown complete')src/cli/cliRun.ts:361-363—logger.trace('directories:' / 'cwd:' / 'options:'), which is what produced the multi-lineutil.inspectdumps above (runCliis what--mcpgoes through).
At the default INFO level the MCP server happens to log nothing to stdout, which is why this has stayed invisible: the very flag an operator would reach for to diagnose the server is the flag that corrupts it.
Impact, stated carefully
MCP over stdio is defined as newline-delimited JSON-RPC on stdout, with the process's stderr free for
diagnostics; stdout is reserved for protocol messages, so anything else on it is out of contract.
What a client actually does with the stray lines depends on how strict its reader is: lenient
readers skip unparseable lines and the session survives (the two responses above did arrive),
whereas a client that treats a parse failure as fatal will tear down the connection at the first
directories: [ '.' ]. I measured the server's output, not a specific client's behaviour, so I am
not claiming any particular client breaks — just that the server's stdout stops being a valid
JSON-RPC stream the moment --verbose is added.
Practical consequence either way: --verbose cannot be used to debug an MCP session, because the
diagnostics land in the channel the client is parsing instead of where the operator can read them.
Repro
Needs a built checkout (npm install && npm run build). The probe takes the repo directory,
spawns node bin/repomix.cjs --mcp [flags], performs initialize + tools/list, and counts
stdout lines that are not JSON-RPC messages.
// probe.mjs -> node probe.mjs <repoDir> [--verbose]
import { spawn } from 'node:child_process';
import path from 'node:path';
const repo = path.resolve(process.argv[2]);
const child = spawn(process.execPath, [path.join(repo, 'bin', 'repomix.cjs'), '--mcp', ...process.argv.slice(3)],
{ cwd: repo, stdio: ['pipe', 'pipe', 'pipe'] });
let buf = '';
const lines = [];
child.stdout.on('data', (d) => {
buf += d;
let i;
while ((i = buf.indexOf('\n')) >= 0) {
const line = buf.slice(0, i).trim();
buf = buf.slice(i + 1);
if (line) lines.push(line);
}
});
child.stderr.on('data', () => {});
const wait = (ms) => new Promise((r) => setTimeout(r, ms));
const send = (o) => child.stdin.write(JSON.stringify(o) + '\n');
await wait(1500);
send({ jsonrpc: '2.0', id: 1, method: 'initialize', params: { protocolVersion: '2024-11-05',
capabilities: {}, clientInfo: { name: 'probe', version: '1' } } });
await wait(1200);
send({ jsonrpc: '2.0', method: 'notifications/initialized' });
send({ jsonrpc: '2.0', id: 2, method: 'tools/list', params: {} });
await wait(2500);
const bad = lines.filter((l) => {
try { return typeof JSON.parse(l).jsonrpc !== 'string'; } catch { return true; }
});
console.log(`stdout lines: ${lines.length} non-JSON-RPC: ${bad.length}`);
for (const l of bad.slice(0, 6)) console.log(' >>', l.slice(0, 100));
child.kill();Direction I would suggest (not filed as a PR yet)
Give the logger a "diagnostics go to stderr" mode that the MCP action turns on, so --verbose keeps
working for its only real audience — the operator reading the terminal. Roughly: a
logger.setOutputStream(process.stderr)-style switch called from runMcpAction
(src/cli/actions/mcpAction.ts:4), which leaves the CLI's stdout behaviour untouched, including the
deliberate --stdout pack output path where the level is already forced to SILENT
(src/cli/cliRun.ts:357-358).
I would rather not touch the log levels here: --mcp packing tools already pass quiet: true, and
the log-level state they leave behind is being handled separately in #1865. The two problems are
independent — one is where the bytes go, the other is whether they are emitted at all — so this
should stay its own change.
Happy to send the PR if this direction looks right, or to reframe it if the intended answer is "--verbose is not supported with --mcp" (in which case the CLI should probably reject or warn on the combination rather than silently corrupt its own transport — note that a warning written via the logger would itself land on stdout today).
Found by measuring the running server, not by reading the code alone; the numbers above come from the script in this issue. Written by sxh313 with help from an AI coding agent (Qoder).
Source: yamadashy/repomix