Skip to content

mcp: add max body size limit for SSE + streamable HTTP#783

Open
TheodorNEngoy wants to merge 1 commit intomodelcontextprotocol:mainfrom
TheodorNEngoy:codex/max-body-bytes
Open

mcp: add max body size limit for SSE + streamable HTTP#783
TheodorNEngoy wants to merge 1 commit intomodelcontextprotocol:mainfrom
TheodorNEngoy:codex/max-body-bytes

Conversation

@TheodorNEngoy
Copy link
Contributor

@TheodorNEngoy TheodorNEngoy commented Feb 7, 2026

This adds a basic DoS guard for the built-in HTTP transports by limiting POST request body size.

  • Adds MaxBodyBytes to SSEOptions and StreamableHTTPOptions (default: DefaultMaxBodyBytes = 1_000_000; negative disables).
  • Adds MaxBodyBytes to SSEServerTransport and StreamableServerTransport for custom handler usage.
  • Returns 413 Request Entity Too Large when the limit is exceeded.
  • Includes tests covering both SSE and streamable handlers/transports.

Rationale: avoid unbounded io.ReadAll(req.Body) buffering for network-exposed servers.

@TheodorNEngoy
Copy link
Contributor Author

Local sanity check: go test ./... passes on this branch.

@maciej-kisiel
Copy link
Contributor

Adding a default for this limit is a backwards incompatible behavior change, which we generally try to avoid. Isn't this already easy enough to achieve by wrapping the MCP HTTP Handler with https://godoc.corp.google.com/pkg/net/http#MaxBytesHandler?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants