mcp: add a max request body size of the old transport (#1224)

added for consistency with the streamable transport because sse is not
officially deprecated in the SDK

Co-authored-by: Guglielmo Colombo <guglielmoc@google.com>
This commit is contained in:
Yaroslav 2026-09-01 17:28:35 +02:00 • committed by GitHub
commit cb0de64136
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -383,3 +383,52 @@ func TestSSELocalhostProtection(t *testing.T) {
})
}
}
// TestSSEServerTransportMaxRequestBodyBytes exercises the limit on the exported
// [SSEServerTransport] directly, since it is documented as usable on its own.
func TestSSEServerTransportMaxRequestBodyBytes(t *testing.T) {
requestOfSize := func(n int) []byte {
b := bytes.Repeat([]byte(" "), n)
b[0], b[n-1] = '{', '}'
return b
}
testCases := []struct {
name string
limit int64
request []byte
wantCode int
}{
{
name: "over limit returns",
limit: 1024,
request: requestOfSize(2048),
wantCode: http.StatusRequestEntityTooLarge,
},
{
name: "under limit accepted",
limit: 1024,
request: []byte(`{"jsonrpc":"2.0","id":1,"method":"ping"}`),
wantCode: http.StatusAccepted,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tr := &SSEServerTransport{
Endpoint: "/msg",
Response: httptest.NewRecorder(),
MaxRequestBodyBytes: tc.limit,
}
if _, err := tr.Connect(context.Background()); err != nil {
t.Fatalf("Connect: %v", err)
}
req := httptest.NewRequest(http.MethodPost, "/msg", bytes.NewReader(tc.request))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
tr.ServeHTTP(rec, req)
if rec.Code != tc.wantCode {
t.Errorf("status = %d, want %d", rec.Code, tc.wantCode)
}
})
}
}