Skip to content

fix: isolate MCP stdout so tool-handler writes don't kill rmcp transport#133

Merged
Desperado merged 1 commit into
mainfrom
Desperado/fix-codex-orch-rmcp
Jun 30, 2026
Merged

fix: isolate MCP stdout so tool-handler writes don't kill rmcp transport#133
Desperado merged 1 commit into
mainfrom
Desperado/fix-codex-orch-rmcp

Conversation

@Desperado

Copy link
Copy Markdown
Contributor

Problem

Codex orch sessions died mid tool-loop with:

ERROR rmcp::transport::worker: worker quit with fatal: Transport channel closed,
when Deserialize(Error("data did not match any variant of untagged enum JsonRpcMessage", line: 0, column: 0))
✗ codex exited with error: exit status 1

line: 0, column: 0 means rmcp tried to deserialize empty input — an empty line on the MCP server's stdout.

Root cause

The qmax MCP server (qmax-code serve --mcp) shared os.Stdout between the JSON-RPC transport and terminal UI code. Tool handlers — and the TUI helpers they call — freely fmt.Print to os.Stdout:

  • runTestWithProgress calls fmt.Println() (writes \n → empty line) at internal/agent/tools.go:1043
  • internal/tui/* writes progress bars, browser animations, banners via fmt.Print*

These non-JSON bytes landed on the same newline-delimited stream Codex's rmcp client reads. An empty line from fmt.Println() deserialized to nothing (serde line 0, column 0); a progress bar deserialized to garbage. Either kills the transport worker → Codex exits 1.

Fix (internal/mcp/server.go)

  1. Stdout isolationRunServer pins the real stdout for exclusive use by the JSON-RPC encoder, then repoints the process-level os.Stdout at stderr. Every stray fmt.Print from a tool handler now lands on stderr (diagnostics), never on the wire the parser reads.

  2. Panic recoverydispatch gets a deferred recover so a panicking tool handler (nil deref, index out of range) yields a -32603 JSON-RPC error response instead of crashing the server. A crash would EOF stdout and kill the transport the same way.

  3. Extracted serveMCP(in, out) — the read/respond loop is now testable without swapping global os.Stdin/os.Stdout.

Tests (internal/mcp/server_test.go)

  • TestDispatchRecoversFromPanic — nil-deref in agent.ExecuteTool is caught by the deferred recover and returns a -32603 response.
  • TestServeMCPOutputIsCleanJSONinitializenotifications/initializedtools/list handshake produces only valid JSON-RPC lines (no empty or corrupt lines), and the notification is not echoed.

Test plan

  • go test ./internal/mcp/ -v — all 5 tests pass
  • go test ./... — full suite green
  • go vet / gofmt clean
  • Manual: run a Codex orch session that triggers qmax tools (e.g. run_test) and confirm the tool loop completes without the rmcp transport fatal

The qmax MCP server (qmax-code serve --mcp) shared os.Stdout between the
JSON-RPC transport and terminal UI code. Tool handlers (and the TUI
helpers they call — progress bars, browser animations, fmt.Println in
runTestWithProgress) freely wrote to os.Stdout, corrupting the
newline-delimited JSON-RPC stream that Codex/CC rmcp reads.

An empty line from fmt.Println() deserialized to nothing (serde
line:0,column:0) and a progress bar deserialized to garbage, either
killing the transport worker with: "data did not match any variant of
untagged enum JsonRpcMessage" — then Codex exited with status 1.

Fix (internal/mcp/server.go):
- Pin the real stdout for exclusive use by the JSON-RPC encoder and
  repoint process-level os.Stdout at stderr, so every stray write lands
  on diagnostics instead of the wire the parser reads.
- Add deferred panic recovery in dispatch so a panicking tool handler
  returns a -32603 error response instead of crashing the server (a
  crash would EOF stdout and kill the transport the same way).
- Extract serveMCP(in, out) so the output contract is unit-testable.

Tests (internal/mcp/server_test.go):
- TestDispatchRecoversFromPanic: nil-deref in ExecuteTool is recovered.
- TestServeMCPOutputIsCleanJSON: initialize+tools/list handshake yields
  only valid JSON-RPC lines (no empty/corrupt lines).
@qualitymaxapp

qualitymaxapp Bot commented Jun 29, 2026

Copy link
Copy Markdown

✅ QualityMax Pipeline

Gate Result
🔍 AI Review ✅ Clean
🧪 Repo Tests ✅ 406/406 passed (go)
🤖 AI Tests ✅ 28/32 passed

Powered by QualityMax — AI-Powered Test Automation

@Desperado Desperado merged commit d5226f9 into main Jun 30, 2026
6 checks passed
@Desperado Desperado deleted the Desperado/fix-codex-orch-rmcp branch June 30, 2026 06:21
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.

1 participant