From 3b9881c2dd2ea73a8b5c02bc558046121fa2dc61 Mon Sep 17 00:00:00 2001 From: haosenwang1018 Date: Sat, 18 Apr 2026 06:11:36 +0000 Subject: [PATCH] fix: only show server stderr in debug mode Signed-off-by: haosenwang1018 --- src/client.ts | 19 +++++++------------ src/index.ts | 6 ++++++ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/client.ts b/src/client.ts index 50731cf..26f635c 100644 --- a/src/client.ts +++ b/src/client.ts @@ -244,14 +244,16 @@ export async function connectToServer( transport = createStdioTransport(config); // Capture stderr for debugging - attach BEFORE connect - // Always stream stderr immediately so auth prompts are visible const stderrStream = transport.stderr; if (stderrStream) { stderrStream.on('data', (chunk: Buffer) => { const text = chunk.toString(); stderrChunks.push(text); - // Always stream stderr immediately so users can see auth prompts - process.stderr.write(`[${serverName}] ${text}`); + + // Only forward live stderr in debug mode to avoid noisy token burn. + if (process.env.MCP_DEBUG) { + process.stderr.write(`[${serverName}] ${text}`); + } }); } } @@ -268,15 +270,8 @@ export async function connectToServer( throw error; } - // For successful connections, forward stderr to console - if (!isHttpServer(config)) { - const stderrStream = (transport as StdioClientTransport).stderr; - if (stderrStream) { - stderrStream.on('data', (chunk: Buffer) => { - process.stderr.write(chunk); - }); - } - } + // For successful connections, stderr has already been captured above. + // It is only forwarded live when MCP_DEBUG is enabled. return { client, diff --git a/src/index.ts b/src/index.ts index 088deed..0414003 100755 --- a/src/index.ts +++ b/src/index.ts @@ -129,6 +129,10 @@ function parseArgs(args: string[]): ParsedArgs { result.command = 'version'; return result; + case '--debug': + process.env.MCP_DEBUG = '1'; + break; + case '-d': case '--with-descriptions': result.withDescriptions = true; @@ -367,6 +371,7 @@ Options: -v, --version Show version number -d, --with-descriptions Include tool descriptions -c, --config Path to mcp_servers.json config file + --debug Show MCP server stderr/debug output Output: mcp-cli/info/grep Human-readable text to stdout @@ -385,6 +390,7 @@ Examples: Environment Variables: MCP_NO_DAEMON=1 Disable connection caching (force fresh connections) MCP_DAEMON_TIMEOUT=N Set daemon idle timeout in seconds (default: 60) + MCP_DEBUG=1 Show MCP server stderr/debug output Config File: The CLI looks for mcp_servers.json in: