Skip to content

Fix stdio protocol corruption by redirecting console methods to stderr#28

Open
gkorland with Claude wants to merge 4 commits into
mainfrom
claude/fix-stdio-protocol-corruption
Open

Fix stdio protocol corruption by redirecting console methods to stderr#28
gkorland with Claude wants to merge 4 commits into
mainfrom
claude/fix-stdio-protocol-corruption

Conversation

@Claude

@Claude Claude AI commented Feb 23, 2026

Copy link
Copy Markdown
Contributor

In stdio transport mode, any output to stdout (console.log, console.warn, etc.) corrupts the MCP JSON-RPC protocol stream, causing immediate client disconnection.

Changes

  • src/index.ts: Added redirectConsoleToStderr() function that monkey-patches all console methods to write to stderr when stdio transport is active
  • src/services/logger.service.ts: Changed console.warn to console.error for log directory creation failures
  • src/stdio-console.test.ts: Added test suite verifying console redirection prevents stdout pollution

Implementation

The fix activates automatically when the server starts in stdio mode:

async function startStdioServer(): Promise<void> {
  // Redirect console to stderr to prevent corruption of the MCP protocol stream
  redirectConsoleToStderr();
  
  const transport = new StdioServerTransport();
  await server.connect(transport);
}

function redirectConsoleToStderr(): void {
  console.log = (...args: unknown[]) => {
    process.stderr.write(`[LOG] ${args.map(String).join(' ')}\n`);
  };
  // ... same for warn, info, debug, error
}

HTTP transport is unaffected as it doesn't use stdio for protocol communication.

Original prompt

This section details on the original issue you should resolve

<issue_title>Stdio Protocol Corruption via Standard Output</issue_title>
<issue_description>Description: In stdio transport mode, any dependency writing to stdout (e.g., console.log) will corrupt the JSON-RPC stream used by the MCP protocol.

Impact: Immediate disconnection or protocol errors for MCP clients like Claude Desktop.

Recommended Fix: Monkey-patch console.log to redirect to console.error (stderr) when the stdio transport is active.

Affected Files: src/index.ts, src/services/logger.service.ts.</issue_description>

Comments on the Issue (you are @claude[agent] in this section)

Co-authored-by: gkorland <753206+gkorland@users.noreply.github.com>
@Claude Claude AI changed the title [WIP] Fix stdio protocol corruption via standard output Fix stdio protocol corruption by redirecting console methods to stderr Feb 23, 2026
@Claude
Claude AI requested a review from gkorland February 23, 2026 08:29
@gkorland
gkorland marked this pull request as ready for review February 23, 2026 09:07
@codecov

codecov Bot commented Feb 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 47.07%. Comparing base (2b24b1d) to head (5e33f13).

Files with missing lines Patch % Lines
src/index.ts 0.00% 12 Missing ⚠️
src/services/logger.service.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #28      +/-   ##
==========================================
- Coverage   48.32%   47.07%   -1.26%     
==========================================
  Files          11       11              
  Lines         449      461      +12     
  Branches      103      100       -3     
==========================================
  Hits          217      217              
- Misses        232      244      +12     
Flag Coverage Δ
unittests 47.07% <0.00%> (-1.26%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stdio Protocol Corruption via Standard Output

2 participants