fix(mcp): refactor render engine to use io.Writer to fix EOF error #18
+83
−95
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.
Bug Report / Proposed Fix: Persistent "EOF" in MCP Mode
Problem Description
The
codemap-mcpserver consistently returns anEOFerror (disconnection) upon any tool call that generates output. This makes the tool unusable in environments like Claude Desktop or other MCP clients that rely on a stable JSON-RPC stream over Stdio.Root Cause Analysis
The current implementation of the MCP server in
mcp/main.gouses acaptureOutputfunction that hijacks the globalos.Stdout.While this works for a simple CLI, it is a protocol violation in an MCP server using
mcp.StdioTransport. The MCP transport relies onos.Stdoutto send JSON-RPC messages. By redirectingos.Stdoutglobally, the server inadvertently corrupts or interrupts the communication stream, leading to an immediateEOFon the client side.Proposed Solution: Refactor to
io.WriterInstead of global output hijacking, the rendering engine should be refactored to support dependency injection of the output stream.
1. Refactor Render Signatures
Update all rendering functions in
render/(Tree,Depgraph,Skyline) to accept anio.Writer.2. Replace direct
os.StdoutwritesReplace all instances of
fmt.Printfandfmt.Printlnwithfmt.Fprintf(w, ...)andfmt.Fprintln(w, ...).3. Update MCP Handlers
In
mcp/main.go, replace thecaptureOutputlogic with a localbytes.Buffer. This ensures the rendering happens in memory and the resulting string is safely returned as part of the JSON-RPC response without ever touching the actualos.Stdoutstream.Benefits
Verification
The fix was verified by manually sending JSON-RPC
initializeandtools/callrequests to the patched binary. The server now remains connected and returns correctly formatted output across multiple calls.