go-sdk icon indicating copy to clipboard operation
go-sdk copied to clipboard

fix: bound stdio JSON-RPC message size

Open he-yufeng opened this issue 6 months ago • 6 comments

Fixes #984.

Summary

Adds an optional MaxMessageBytes limit for newline-delimited JSON-RPC transports:

  • CommandTransport.MaxMessageBytes bounds frames read from child process stdout.
  • StdioTransport.MaxMessageBytes and IOTransport.MaxMessageBytes expose 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 TestCommandTransportTerminateDuration
  • go 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

he-yufeng avatar Jun 01 '26 08:06 he-yufeng

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.

he-yufeng avatar Jun 02 '26 19:06 he-yufeng

Fresh local re-validation on the current checkout:

  • go test ./mcp -> passed
  • gofmt -l mcp/cmd.go mcp/transport.go mcp/transport_test.go -> no output
  • git diff --check origin/main...HEAD -> passed

The GitHub checks are also green as of the latest run.

he-yufeng avatar Jun 04 '26 10:06 he-yufeng

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 ./mcp
  • go test -race ./mcp
  • go test ./...
  • git diff --check origin/main..HEAD

he-yufeng avatar Jun 06 '26 19:06 he-yufeng

Nice refactor in the stdio framing path. A few notes from reading the diff:

  1. Pre-decode bound — Checking len(frame)+len(part) against MaxMessageBytes before validateJSONFrame avoids allocating/decoding oversized payloads. That composes well with other stdio hardening work on malformed envelopes.

  2. ReadSlice + ErrBufferFull — The continue loop on bufio.ErrBufferFull is correct for long lines, but it does grow frame without an early cap when maxMessageBytes == 0. Worth confirming that is intentional (unbounded mode preserves prior behavior) and that the capped path rejects before unbounded growth when maxMessageBytes > 0.

  3. Limit semantics — TestIOConnReadMaxMessageBytes uses int64(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\n would help server authors pick values.

  4. Surface area — Exposing the knob on CommandTransport, StdioTransport, and IOTransport covers 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.

piyushbag avatar Jun 11 '26 02:06 piyushbag

Thanks for the careful read. I pushed 880a8de with a small follow-up for the limit semantics you called out.

What changed:

  • documented MaxMessageBytes as applying to the JSON-RPC payload bytes, not the NDJSON line ending
  • changed readFrame so LF/CRLF are trimmed before the final capped-size check
  • kept the bufio.ErrBufferFull path capped when MaxMessageBytes > 0
  • added regression coverage for payload-at-limit with LF and CRLF
  • added a regression check that MaxMessageBytes == 0 preserves the old unbounded behavior

Validation:

  • gofmt -w mcp\cmd.go mcp\transport.go mcp\transport_test.go
  • go test ./mcp
  • go test -race ./mcp
  • go test ./...
  • git diff --check

he-yufeng avatar Jun 11 '26 22:06 he-yufeng

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.

piyushbag avatar Jun 12 '26 00:06 piyushbag

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 output
  • go test ./mcp
  • go test -race ./mcp
  • go test ./...
  • git diff --check origin/main..HEAD

All passed locally after the rebase.

he-yufeng avatar Jun 15 '26 14:06 he-yufeng