fix: isolate MCP stdout so tool-handler writes don't kill rmcp transport#133
Merged
Conversation
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).
✅ QualityMax Pipeline
Powered by QualityMax — AI-Powered Test Automation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Codex orch sessions died mid tool-loop with:
line: 0, column: 0means 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) sharedos.Stdoutbetween the JSON-RPC transport and terminal UI code. Tool handlers — and the TUI helpers they call — freelyfmt.Printtoos.Stdout:runTestWithProgresscallsfmt.Println()(writes\n→ empty line) atinternal/agent/tools.go:1043internal/tui/*writes progress bars, browser animations, banners viafmt.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 (serdeline 0, column 0); a progress bar deserialized to garbage. Either kills the transport worker → Codex exits 1.Fix (
internal/mcp/server.go)Stdout isolation —
RunServerpins the real stdout for exclusive use by the JSON-RPC encoder, then repoints the process-levelos.Stdoutat stderr. Every strayfmt.Printfrom a tool handler now lands on stderr (diagnostics), never on the wire the parser reads.Panic recovery —
dispatchgets a deferredrecoverso a panicking tool handler (nil deref, index out of range) yields a-32603JSON-RPC error response instead of crashing the server. A crash would EOF stdout and kill the transport the same way.Extracted
serveMCP(in, out)— the read/respond loop is now testable without swapping globalos.Stdin/os.Stdout.Tests (
internal/mcp/server_test.go)TestDispatchRecoversFromPanic— nil-deref inagent.ExecuteToolis caught by the deferred recover and returns a-32603response.TestServeMCPOutputIsCleanJSON—initialize→notifications/initialized→tools/listhandshake 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 passgo test ./...— full suite greengo vet/gofmtcleanrun_test) and confirm the tool loop completes without the rmcp transport fatal