feat(server): support configurable max request payload size#646
feat(server): support configurable max request payload size#646jskjw157 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
7a50a35 to
1a92a5e
Compare
…textprotocol#521) Add maxRequestBodySize parameter to StreamableHttpServerTransport.Configuration, defaulting to 4 MB. This allows consumers to set a lower request body size limit without manual payload counting. - Add maxRequestBodySize: Long to Configuration with require(> 0) validation - Replace hardcoded MAXIMUM_MESSAGE_SIZE with configurable value in parseBody() - Use Long for content-length comparison to avoid Int overflow - Error message displays exact byte limit for clarity at any configured size - Note: body.length check after receiveText() uses character count (pre-existing behavior); byte-accurate fallback is tracked separately
1a92a5e to
ddf9bc9
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
@kpavlov Hey! I’ve addressed the feedback on this PR and all checks are passing now. Would you mind taking a look when you get a chance? Thanks! |
|
|
||
| configureTransportEndpoint(transport) | ||
|
|
||
| val oversizedPayload = "x".repeat(customMaxSize.toInt() + 1) |
There was a problem hiding this comment.
nit: Maybe let's use small valid payload and set the max size to payload length + 1. It ensures that rejection reason is size.
| } | ||
|
|
||
| @Test | ||
| fun `POST at exactly custom max request body size is not rejected as payload too large`() = testApplication { |
There was a problem hiding this comment.
nit: This and previous tests could be combined into one parametrized test
| return null | ||
| } | ||
|
|
||
| val body = call.receiveText() |
There was a problem hiding this comment.
nit: If there is a content length header and it's bigger than max body size, then receiving text is a waste of time and memory. Request can be immediately rejected.
| val transport = StreamableHttpServerTransport( | ||
| StreamableHttpServerTransport.Configuration( | ||
| enableJsonResponse = true, | ||
| maxRequestBodySize = customMaxSize, |
There was a problem hiding this comment.
- test for negative value throwong
IllegalArgumentException
Summary
Add
maxRequestBodySizeparameter toStreamableHttpServerTransport.Configuration, allowing consumers to set a custom request body size limit lower than the default 4 MB.maxRequestBodySize: Longproperty toConfigurationwith a default of 4 MB (4,194,304bytes)MAXIMUM_MESSAGE_SIZEconstant with the configurable value inparseBody()Longfor content-length comparison to avoidIntoverflow on large valuesMotivation and Context
Closes #521
The
StreamableHttpServerTransporthas a hardcoded 4 MB request body limit. Consumers who need a lower limit currently have to manually count the request payload size before passing it to the SDK. This change makes the limit configurable viaConfiguration.How Has This Been Tested?
POST with custom max request body size rejects oversized payload— verifiesPayloadTooLargeis returned for payloads exceeding the custom limitPOST at exactly custom max request body size is not rejected as payload too large— verifies payloads at exactly the limit are not rejected by the size check./gradlew :kotlin-sdk-server:jvmTest --tests "*.StreamableHttpServerTransportTest"Breaking Changes
None. The new parameter has a default value matching the previous hardcoded limit.
Types of changes
Checklist