Skip to content

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

Closed
TheodorNEngoy wants to merge 1 commit into
modelcontextprotocol:mainfrom
TheodorNEngoy:codex/max-body-bytes
Closed

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

Conversation

@TheodorNEngoy
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

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

@maciej-kisiel
Copy link
Copy Markdown
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?

@maciej-kisiel
Copy link
Copy Markdown
Contributor

I will close this PR for house-keeping, I believe this goal is achievable easily with the standard library and doesn't require special support in the SDK. Feel free to re-open if you'd like to continue the discussion.

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