feat(server): support configurable max request payload size (#646)

## Summary

Add `maxRequestBodySize` parameter to
`StreamableHttpServerTransport.Configuration`, allowing consumers to set
a custom request body size limit lower than the default 4 MB.

- Add `maxRequestBodySize: Long` property to `Configuration` with a
default of 4 MB (`4,194,304` bytes)
- Replace hardcoded `MAXIMUM_MESSAGE_SIZE` constant with the
configurable value in `parseBody()`
- Use `Long` for content-length comparison to avoid `Int` overflow on
large values

## Motivation and Context

Closes #521

The `StreamableHttpServerTransport` has 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 via `Configuration`.

## How Has This Been Tested?

- Existing unit tests continue to pass (default 4 MB behavior unchanged)
- Added `POST with custom max request body size rejects oversized
payload` — verifies `PayloadTooLarge` is returned for payloads exceeding
the custom limit
- Added `POST 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

```bash
./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

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update

## Checklist

- [x] I have read the [MCP
Documentation](https://modelcontextprotocol.io)
- [x] My code follows the repository's style guidelines
- [x] New and existing tests pass locally
- [x] I have added appropriate error handling
- [x] I have added or updated documentation as needed
This commit is contained in:
johny 2026-04-01 19:57:17 +09:00 • committed by GitHub
commit 6ce9ff81e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -43,10 +43,12 @@ import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation as ClientContentNegotiation
@ -63,6 +65,15 @@ class StreamableHttpServerTransportTest {
null,
"lolol",
)
private val sizeTestPayload = "x".repeat(64)
@JvmStatic
fun maxBodySizeTestCases(): List<Arguments> = listOf(
Arguments.of(sizeTestPayload.length.toLong() - 1, HttpStatusCode.PayloadTooLarge),
Arguments.of(sizeTestPayload.length.toLong(), HttpStatusCode.BadRequest),
Arguments.of(sizeTestPayload.length.toLong() + 1, HttpStatusCode.BadRequest),
)
}
private val path = "/transport"
@ -383,6 +394,45 @@ class StreamableHttpServerTransportTest {
response.status shouldBe HttpStatusCode.PayloadTooLarge
}
@ParameterizedTest
@MethodSource("maxBodySizeTestCases")
fun `POST with custom max request body size validates payload size`(
maxSize: Long,
expectedStatus: HttpStatusCode,
) = testApplication {
configTestServer()
val client = createTestClient()
val transport = StreamableHttpServerTransport(
StreamableHttpServerTransport.Configuration(
enableJsonResponse = true,
maxRequestBodySize = maxSize,
),
)
transport.onMessage { message ->
if (message is JSONRPCRequest) {
transport.send(JSONRPCResponse(message.id, EmptyResult()))
}
}
configureTransportEndpoint(transport)
val response = client.post(path) {
addStreamableHeaders()
setBody(sizeTestPayload)
}
response.status shouldBe expectedStatus
}
@Test
fun `Configuration with negative maxRequestBodySize throws IllegalArgumentException`() {
assertFailsWith<IllegalArgumentException> {
StreamableHttpServerTransport.Configuration(maxRequestBodySize = -1)
}
}
private fun ApplicationTestBuilder.configureTransportEndpoint(transport: StreamableHttpServerTransport) {
application {
routing {