Skip to content

fix(cli): redirect stderr before MCP loading to prevent terminal pollution#1581

Merged
RealKai42 merged 1 commit intoMoonshotAI:mainfrom
n-WN:n-WN/fix-mcp-stderr-pollution
Mar 25, 2026
Merged

fix(cli): redirect stderr before MCP loading to prevent terminal pollution#1581
RealKai42 merged 1 commit intoMoonshotAI:mainfrom
n-WN:n-WN/fix-mcp-stderr-pollution

Conversation

@n-WN
Copy link
Copy Markdown
Collaborator

@n-WN n-WN commented Mar 25, 2026

Related Issue

Resolves #1214 — Verbose MCP debug messages printed to console when starting with -C flag and OAuth MCP servers.

Also related: #1215 (attempted fix, still open), #1501 (closed without merge).

Description

MCP server subprocesses (e.g. mcp-remote) write debug and OAuth logs to stderr. Previously, redirect_stderr_to_logger() was called after KimiCLI.create(), which meant MCP server stderr output during startup went directly to the user's terminal, polluting the TUI.

Root Cause

enable_logging(debug, redirect_stderr=False)   ← line 343: stderr NOT redirected yet
    ...
instance = await KimiCLI.create(...)           ← line 530: MCP servers start here
    ...                                           stderr noise goes to terminal ❌
redirect_stderr_to_logger()                    ← line 549: too late

Fix

Move redirect_stderr_to_logger() to just before KimiCLI.create(). At this point CLI argument parsing has already completed, so Typer/Click startup errors are no longer a concern. Fatal errors from create() remain visible because _emit_fatal_error() writes to the saved original stderr fd (not affected by the redirect).

enable_logging(debug, redirect_stderr=False)   ← line 343: still deferred here
    ...
redirect_stderr_to_logger()                    ← line 535: NOW redirected ✅
instance = await KimiCLI.create(...)           ← line 537: MCP stderr → log file

Before / After

Tested with a noisy MCP server that writes 5 debug lines to stderr during startup:

# Before (upstream/main):
$ kimi --mcp-config-file noisy.json --yolo -p 'say hi' 2>&1 | cat
[mcp-remote DEBUG] Connecting to OAuth provider... attempt 1
[mcp-remote DEBUG] Connecting to OAuth provider... attempt 2
[mcp-remote DEBUG] Connecting to OAuth provider... attempt 3
[mcp-remote DEBUG] Connecting to OAuth provider... attempt 4
[mcp-remote DEBUG] Connecting to OAuth provider... attempt 5
• Hello! 👋 How can I help you today?

# After (this branch):
$ kimi --mcp-config-file noisy.json --yolo -p 'say hi' 2>&1 | cat
• Hello! 👋 I'm Kimi Code CLI, your software engineering assistant. How can I help you today?

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked the related issue, if any.
  • I have run make gen-changelog to update the changelog.

Open with Devin

…ution

MCP server subprocesses (e.g. mcp-remote) write debug/OAuth logs to stderr.
Previously, `redirect_stderr_to_logger()` was called *after*
`KimiCLI.create()`, which meant MCP server stderr output during startup
went directly to the user's terminal, polluting the TUI.

Move the redirect call to just before `create()` — CLI argument parsing
has already completed at this point, so Typer/Click startup errors are
no longer a concern.  Fatal errors from `create()` remain visible
because `_emit_fatal_error()` writes to the saved original stderr fd.
@n-WN n-WN requested a review from RealKai42 March 25, 2026 11:24
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

@RealKai42 RealKai42 merged commit 8c33182 into MoonshotAI:main Mar 25, 2026
18 checks passed
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.

Verbose MCP debug messages printed to console when starting with -C flag and OAuth MCP servers

2 participants