fix: bound request body size in SSE and Streamable HTTP transports#839
Open
devcrocod wants to merge 2 commits into
Open
fix: bound request body size in SSE and Streamable HTTP transports#839devcrocod wants to merge 2 commits into
devcrocod wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a bounded request-body reader and wires it into the server-side SSE and Streamable HTTP transports to prevent oversized HTTP POST bodies (including chunked requests without Content-Length) from exhausting server memory.
Changes:
- Introduced
receiveTextWithLimit(...)+RequestBodyTooLargeExceptionand used it in SSE and Streamable HTTP request parsing. - Added/updated transport and route configuration to pass a
maxRequestBodySize(default 4 MiB) and return413 Payload Too Largeon oversize bodies. - Added new JVM tests covering oversize bodies (including chunked/no
Content-Length) and UTF-8 multibyte chunk-boundary decoding.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/RequestBody.kt | Adds shared size-limited request-body reader and exception. |
| kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/StreamableHttpServerTransport.kt | Switches POST body parsing to the new size-limited reader. |
| kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/SSEServerTransport.kt | Adds max body size support and enforces it in SSE POST handling. |
| kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/KtorServer.kt | Exposes maxRequestBodySize through public mcp(...) route/application setup. |
| kotlin-sdk-server/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/RequestBodyTest.kt | New tests for receiveTextWithLimit(...) behavior and UTF-8 safety. |
| kotlin-sdk-server/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/SseServerTransportTest.kt | New SSE transport tests for not-started behavior and 413 rejection. |
| kotlin-sdk-server/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/StreamableHttpServerTransportTest.kt | Adds a chunked/no-Content-Length oversize test and updates line-reading utilities. |
| kotlin-sdk-server/api/kotlin-sdk-server.api | Updates API dump for new/changed public signatures. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+25
to
+33
| internal suspend fun ApplicationCall.receiveTextWithLimit(maxBytes: Long): String { | ||
| val buffer = Buffer() | ||
| val read = receiveChannel().readRemaining(maxBytes + 1).transferTo(buffer) | ||
| if (read > maxBytes) { | ||
| buffer.clear() | ||
| throw RequestBodyTooLargeException(maxBytes) | ||
| } | ||
| return buffer.readString() | ||
| } |
Comment on lines
51
to
63
| @KtorDsl | ||
| @Suppress("LongParameterList") | ||
| public fun Route.mcp( | ||
| path: String, | ||
| enableDnsRebindingProtection: Boolean = true, | ||
| allowedHosts: List<String>? = null, | ||
| allowedOrigins: List<String>? = null, | ||
| maxRequestBodySize: Long = DEFAULT_MAX_REQUEST_BODY_SIZE, | ||
| block: ServerSSESession.() -> Server, | ||
| ) { | ||
| route(path) { | ||
| mcp(enableDnsRebindingProtection, allowedHosts, allowedOrigins, block) | ||
| mcp(enableDnsRebindingProtection, allowedHosts, allowedOrigins, maxRequestBodySize, block) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Caps the size of incoming HTTP POST bodies in both server transports so an oversized request can no longer exhaust server memory
How Has This Been Tested?
new tests
Breaking Changes
binary incompatible for SseServerTransport constuctors
Types of changes
Checklist