|
| 1 | +package agents_engine.mcp |
| 2 | + |
| 3 | +import agents_engine.core.agent |
| 4 | +import java.net.URI |
| 5 | +import java.net.http.HttpClient |
| 6 | +import java.net.http.HttpRequest |
| 7 | +import java.net.http.HttpResponse |
| 8 | +import kotlin.test.AfterTest |
| 9 | +import kotlin.test.Test |
| 10 | +import kotlin.test.assertEquals |
| 11 | +import kotlin.test.assertTrue |
| 12 | + |
| 13 | +// Tests for #851 — McpServer caps request body size and returns 413 before |
| 14 | +// loading the entire body into memory. |
| 15 | +class McpServerBodySizeLimitTest { |
| 16 | + |
| 17 | + private val toStop = mutableListOf<() -> Unit>() |
| 18 | + |
| 19 | + @AfterTest fun cleanup() { toStop.forEach { runCatching { it() } } } |
| 20 | + |
| 21 | + private fun trivialAgent() = agent<String, String>("greeter") { |
| 22 | + skills { skill<String, String>("greet", "Greets") { implementedBy { "hi $it" } } } |
| 23 | + } |
| 24 | + |
| 25 | + private fun startServer(maxBytes: Long = McpServer.DEFAULT_MAX_REQUEST_BYTES): McpServer { |
| 26 | + val server = McpServer.from(trivialAgent()) { |
| 27 | + expose("greet") |
| 28 | + port = 0 |
| 29 | + maxRequestBytes = maxBytes |
| 30 | + }.start() |
| 31 | + toStop.add { server.stop() } |
| 32 | + return server |
| 33 | + } |
| 34 | + |
| 35 | + private fun postRaw(url: String, body: String, contentLength: Long? = null): HttpResponse<String> { |
| 36 | + val builder = HttpRequest.newBuilder() |
| 37 | + .uri(URI.create(url)) |
| 38 | + .header("Content-Type", "application/json") |
| 39 | + .POST(HttpRequest.BodyPublishers.ofString(body)) |
| 40 | + if (contentLength != null) { |
| 41 | + // The JDK HttpClient sets Content-Length automatically; this branch is here |
| 42 | + // for explicit-override tests if the framework ever needs them. |
| 43 | + builder.setHeader("Content-Length", contentLength.toString()) |
| 44 | + } |
| 45 | + return HttpClient.newHttpClient().send(builder.build(), HttpResponse.BodyHandlers.ofString()) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun `request larger than the cap is rejected with 413`() { |
| 50 | + val server = startServer(maxBytes = 1024) // 1 KiB cap for the test |
| 51 | + val tooBig = "x".repeat(2048) // 2 KiB |
| 52 | + val response = postRaw(server.url, """{"jsonrpc":"2.0","id":1,"method":"ping","params":{"pad":"$tooBig"}}""") |
| 53 | + assertEquals(413, response.statusCode()) |
| 54 | + assertTrue( |
| 55 | + response.body().contains("Payload Too Large", ignoreCase = true), |
| 56 | + "expected error body to mention size limit; got: ${response.body()}", |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun `request exactly at the cap is processed normally`() { |
| 62 | + val server = startServer(maxBytes = 16 * 1024) // 16 KiB cap |
| 63 | + // Build a request that's well under the cap but uses real JSON-RPC shape. |
| 64 | + val response = postRaw(server.url, """{"jsonrpc":"2.0","id":1,"method":"ping"}""") |
| 65 | + assertEquals(200, response.statusCode()) |
| 66 | + assertTrue(response.body().contains("\"result\""), "expected success: ${response.body()}") |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun `default cap accepts a normal-sized tools-list request`() { |
| 71 | + // Sanity: the default cap doesn't break ordinary traffic. |
| 72 | + val server = startServer() // default |
| 73 | + val response = postRaw(server.url, """{"jsonrpc":"2.0","id":1,"method":"tools/list"}""") |
| 74 | + assertEquals(200, response.statusCode()) |
| 75 | + } |
| 76 | +} |
0 commit comments