fix: bound stdio JSON-RPC message size
Fixes #984.
Summary
Adds an optional MaxMessageBytes limit for newline-delimited JSON-RPC transports:
CommandTransport.MaxMessageBytesbounds frames read from child process stdout.StdioTransport.MaxMessageBytesandIOTransport.MaxMessageBytesexpose the same behavior for server-side stdio and custom IO transports.- The IO reader now frames input by NDJSON line, preserves the existing trailing-data rejection, and rejects oversized frames before JSON-RPC decoding.
Validation
go test ./mcp -run "TestIOConnRead|TestIOConnReadMaxMessageBytes|TestBatchFraming"go test ./mcp -run TestCommandTransportTerminateDurationgo test ./...go vet ./...gofmt -l .go run honnef.co/go/tools/cmd/[email protected] ./...git diff --check -- mcp\transport.go mcp\cmd.go mcp\transport_test.go
Rebased on current origin/main and reran validation locally.
Validated:
gofmt -w mcp\cmd.go mcp\transport.go mcp\transport_test.go
go test ./mcp
go test -race ./mcp
go test ./...
git diff --check origin/main...HEAD
All passed after the rebase.
Fresh local re-validation on the current checkout:
go test ./mcp-> passedgofmt -l mcp/cmd.go mcp/transport.go mcp/transport_test.go-> no outputgit diff --check origin/main...HEAD-> passed
The GitHub checks are also green as of the latest run.
Rebased this branch onto the latest upstream main and force-pushed the cleaned head.
Validated locally:
gofmt -l mcp/cmd.go mcp/transport.go mcp/transport_test.go(no output)go test ./mcpgo test -race ./mcpgo test ./...git diff --check origin/main..HEAD
Nice refactor in the stdio framing path. A few notes from reading the diff:
-
Pre-decode bound — Checking
len(frame)+len(part)againstMaxMessageBytesbeforevalidateJSONFrameavoids allocating/decoding oversized payloads. That composes well with other stdio hardening work on malformed envelopes. -
ReadSlice+ErrBufferFull— The continue loop onbufio.ErrBufferFullis correct for long lines, but it does growframewithout an early cap whenmaxMessageBytes == 0. Worth confirming that is intentional (unbounded mode preserves prior behavior) and that the capped path rejects before unbounded growth whenmaxMessageBytes > 0. -
Limit semantics —
TestIOConnReadMaxMessageBytesusesint64(len(input))without the trailing newline, which matches rejecting only the JSON payload bytes. A one-line doc comment on whether the limit includes\n/\r\nwould help server authors pick values. -
Surface area — Exposing the knob on
CommandTransport,StdioTransport, andIOTransportcovers the main server entry points I have been exercising in go-sdk transport tests.
I ran a quick read-only review against current main; I have not tested this branch locally. Looks like a solid fix for #984.
Thanks for the careful read. I pushed 880a8de with a small follow-up for the limit semantics you called out.
What changed:
- documented
MaxMessageBytesas applying to the JSON-RPC payload bytes, not the NDJSON line ending - changed
readFrameso LF/CRLF are trimmed before the final capped-size check - kept the
bufio.ErrBufferFullpath capped whenMaxMessageBytes > 0 - added regression coverage for payload-at-limit with LF and CRLF
- added a regression check that
MaxMessageBytes == 0preserves the old unbounded behavior
Validation:
gofmt -w mcp\cmd.go mcp\transport.go mcp\transport_test.gogo test ./mcpgo test -race ./mcpgo test ./...git diff --check
Thanks for the follow-up. The payload-vs-line-ending semantics and the CRLF/LF regression tests match what I had in mind. Looks good from my read.
Rebased this onto current main and re-ran the local Go checks.
Current head: 6b03643
Validation:
gofmt -l mcp\cmd.go mcp\transport.go mcp\transport_test.go-> no outputgo test ./mcpgo test -race ./mcpgo test ./...git diff --check origin/main..HEAD
All passed locally after the rebase.