Repository navigation
Description
Describe the bug
The stdio transport ends the entire session on a single syntactically malformed JSON-RPC frame, instead of replying with a JSON-RPC parse error (-32700) and continuing. Any client that sends one malformed frame terminates the session.
Root cause: newIOConn reads from a single streaming json.Decoder over stdin. A malformed frame makes Decode return a *json.SyntaxError; the read goroutine returns on that error, ending the connection. A streaming decoder also cannot resynchronize after a syntax error, since its buffered stream state is poisoned.
This is the transport-level cousin of #976 (empty-method requests), fixed in #1000. mark3labs/mcp-go handles the same input correctly: it reads newline-delimited frames, unmarshals each independently, and on a bad frame replies -32700 and keeps serving.
To Reproduce
- Start any stdio server (e.g.
conformance/everything-server). - Complete the initialize handshake.
- Send a syntactically invalid frame, e.g.
{bad jsonfollowed by a newline. - The session ends: no
-32700is returned, and later valid requests get no response.
Minimal transport-level repro:
tr := newIOConn(rwc{rc: io.NopCloser(strings.NewReader(
"{bad\n" + `{"jsonrpc":"2.0","id":1,"method":"ping"}` + "\n"))})
_, err := tr.Read(context.Background())
// returns the decode error; the following valid "ping" is never read.Expected behavior
Per JSON-RPC 2.0, a malformed frame should be answered with a -32700 parse error (id: null) and the session should continue: the read loop resynchronizes to the next frame and delivers the following valid request. This matches mark3labs/mcp-go and the recoverable-decode direction of #1000.
Additional context
Tested v1.4.1 through v1.7.0 (latest); behavior is unchanged. Surfaced by an MCP conformance study run against a downstream server (blackwell-systems/agent-lsp#14); because the behavior lives in the shared stdio transport, every server built on the SDK's stdio transport exhibits it.
I have a fix ready (reply -32700, resync to the next newline-delimited frame, terminate only on a genuine EOF/I/O error, with tests) and will open a PR referencing this issue.
Activity
fix(mcp): keep the session alive on undecodable frames (#62)
docs(upstream): record the two client-go merges and the page merge, a…
serverplumber commentedon Sep 27, 2026
Confirming this is still present in v1.8.0-pre.2. Two findings below go beyond the issue: answers to requests the server has already received are lost too, and #1210 as it stands (3906b01) leaves part of this open.
Still present after v1.7.0
conformance/everything-serverbuilt from v1.8.0-pre.2 exits on the first malformed line:initializeand ping 2 are answered, thenServer failed: invalid character 'b' looking for beginning of object key stringand exit 1. Ping 3 is never answered and no-32700is sent, as the issue describes.Answers already owed are lost too
Sending the same lines all at once:
{ printf '%s\n' "$INIT" "$NOTE" "$P2" '{bad json' "$P3"; sleep 1; } | ./everything-serverAcross three runs the server wrote 0, 0 and 1 of the two answers it owed (
initializeand ping 2), both for requests that arrived before the malformed line. The read error closes the session while those handlers are still running, and their replies are never written. Which ones survive depends on timing. So a client can't rely on "earlier requests are answered" either. From outside I can't tell whether the connection closing or the process exiting is what drops them.What #1210 fixes, and what it leaves open
I built everything-server from the PR head (3906b01) and ran a set of single bad frames between ping 2 and ping 3, both paced and all at once (three runs each). The script is below.
{bad json, an unbalanced object, a NUL byte, nesting 11000 deep-32700is sent twice for each malformed frame, in every run42,{},[]idand nomethod-32700. One malformed frame should get one error. My guess, from reading the diff rather than tracing it: the resync reads fromdec.Buffered(), and when that starts at the newline ending the previous frame,ReadBytes('\n')consumes only that newline. The malformed frame is then decoded, and answered, a second time.readBatch.Readstill returns that error, and the session ends exactly as before. JSON-RPC 2.0's own examples answer1and[]with-32600 Invalid Requestand"id": null, and the server should keep serving after that just as after a parse error.idwith nomethodis at least not fatal, before or after the PR. It goes unanswered where-32600would be expected. That's closer to Stdio server leaves some malformed JSON-RPC requests unanswered while the session remains alive #976 than to this issue.A test that would cover all of it: for each bad frame above, the all-at-once input yields the
initializeresult, pong 2, exactly one error (-32700for a syntax error,-32600for the rest), and pong 3, on every run.Reproduction script (pass the server binary as the argument)
A downstream server shows the same behaviour
github/github-mcp-serverv1.12.2, built on go-sdk v1.7.0, exits the same way on a malformed stdio frame (error running server: invalid character '{' after object key:value pair).Streamable HTTP is unaffected
The same binary run with
--httpanswers the same malformed body with400(malformed payload: unmarshaling jsonrpc message: …), answers the nextpingwith200, and keeps serving. So the fault is specific to the stdio transport's read path.#1210 is the right direction and would be good to get reviewed. It has been waiting about a month and now conflicts with
main. The duplicate reply and the non-JSON-RPC frames seem worth covering before it lands.Pin what a go-sdk client does with this server's refusals, and record…
mcp: test that an oversized stdio frame is recovered from
serverplumber commentedon Nov 3, 2026
Thanks @jmrplens and @Ahmad-Faraj for your input on #1210
@blackwell-systems all this could go into #1210
I wrote regression tests for this. They cover the cases from my earlier comments and the bad-frame cases @jmrplens provided. His -32601 and -32602 cases are left out and
{"hello":"world"}isn't included.The tests drive a real
Serverover stdio pipes and check 4 things:initialize, ping 2 and ping 3 are all answeredTwo tests check that
ioConnresynchronises after a bad frame and reaches EOF cleanly.A third tests oversized frames
TestIOConnOversizedFrame.The session tests fail for every bad frame on
main(3a46369).Before moving forward I'd need the maintainers to decide on the following behaviours:
id. The tests expect error replies to omitid, since MCP'sJSONRPCErrorResponseallows a string or a number but notnull, unlike JSON-RPC 2.0. This includes{"jsonrpc":"2.0","id":9}whose id names one of the server's own requests. Is that correct?Oversized frames. I believe a frame over
MaxLineLengthshould be discarded without buffering, answered with an error and we keep reading.TestIOConnFrameCapstill holds, sinceReadstill reportserrFrameTooLarge. However "cap does not reset per new line" case would need changing. It assumes values can span lines but the spec says "MUST NOT contain embedded newlines".I can write a test for 2 once I get a reply and I'll update 1 if need be.
Links to the specs:
Oversized frames
AI disclosure: Claude wrote most of the test code and drafted this comment. I directed it, reviewed the results, and decided the expected behaviour.
aibeshkov commentedon Nov 16, 2026
Same root cause, with one more consequence on the input side: the read side is a streaming
json.Decoder, so a frame is not delimited by its newline at all. A line that is incomplete JSON (for example a truncated frame) is not rejected at the newline; the decoder keeps reading and absorbs the following line(s) into the same value. The next, valid request is then never answered, and any resulting decode error ends the session as described here.Observed on v1.8.0 with an
mcp.IOTransportoverio.Pipe(initialize handshake, then a truncated line followed by a validping): thepinggets no response.Reading newline-delimited frames and decoding each line independently would fix both this and the original report: a bad frame gets
-32700and the next line is read normally. The behaviour is trusted-client only (the client has to send the truncated line), so this is robustness and spec conformance rather than a security issue.