|
30 | 30 | import io.modelcontextprotocol.client.McpClient; |
31 | 31 | import io.modelcontextprotocol.client.transport.HttpClientStreamableHttpTransport; |
32 | 32 | import io.modelcontextprotocol.common.McpTransportContext; |
| 33 | +import io.modelcontextprotocol.json.McpJsonDefaults; |
33 | 34 | import io.modelcontextprotocol.server.McpServer.AsyncSpecification; |
34 | 35 | import io.modelcontextprotocol.server.McpServer.SyncSpecification; |
35 | 36 | import io.modelcontextprotocol.server.transport.HttpServletStreamableServerTransportProvider; |
|
50 | 51 | import org.junit.jupiter.api.Timeout; |
51 | 52 | import org.junit.jupiter.params.ParameterizedTest; |
52 | 53 | import org.junit.jupiter.params.provider.Arguments; |
| 54 | +import org.junit.jupiter.params.provider.MethodSource; |
53 | 55 | import org.junit.jupiter.params.provider.ValueSource; |
54 | 56 | import org.slf4j.LoggerFactory; |
55 | 57 | import reactor.core.publisher.Flux; |
|
61 | 63 | import static io.modelcontextprotocol.util.ToolsUtils.EMPTY_JSON_SCHEMA; |
62 | 64 | import static java.nio.charset.StandardCharsets.UTF_8; |
63 | 65 | import static org.assertj.core.api.Assertions.assertThat; |
| 66 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
64 | 67 | import static org.awaitility.Awaitility.await; |
65 | 68 |
|
66 | 69 | /** |
@@ -90,6 +93,8 @@ class HttpServletStreamableIntegrationTests extends AbstractMcpClientServerInteg |
90 | 93 | // result in a session sweep |
91 | 94 | private final Duration SESSION_SWEEP_INTERVAL = KEEP_ALIVE_INTERVAL.multipliedBy(2); |
92 | 95 |
|
| 96 | + protected static final int MAX_REQUEST_SIZE = 2048; |
| 97 | + |
93 | 98 | private final HttpClient httpClient = HttpClient.newHttpClient(); |
94 | 99 |
|
95 | 100 | @Override |
@@ -253,6 +258,51 @@ public void cancel() { |
253 | 258 | assertThat(response.statusCode()).isEqualTo(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE); |
254 | 259 | } |
255 | 260 |
|
| 261 | + @ParameterizedTest(name = "{0} : {displayName} ") |
| 262 | + @MethodSource("clientsForTesting") |
| 263 | + void testRejectsWhenContentLengthHeaderExceedsLimit(String clientType) { |
| 264 | + var clientBuilder = clientBuilders.get(clientType); |
| 265 | + |
| 266 | + String inputSchema = """ |
| 267 | + { |
| 268 | + "type": "object", |
| 269 | + "properties": { |
| 270 | + "message": { "type": "string" } |
| 271 | + }, |
| 272 | + "required": ["message"] |
| 273 | + } |
| 274 | + """; |
| 275 | + |
| 276 | + McpServerFeatures.SyncToolSpecification tool1 = McpServerFeatures.SyncToolSpecification.builder() |
| 277 | + .tool(McpSchema.Tool.builder("tool1", McpJsonDefaults.getMapper(), inputSchema) |
| 278 | + .description("tool1 description") |
| 279 | + .build()) |
| 280 | + .callHandler((exchange, request) -> McpSchema.CallToolResult.builder() |
| 281 | + .addContent(McpSchema.TextContent.builder(request.arguments().get("message").toString()).build()) |
| 282 | + .build()) |
| 283 | + .build(); |
| 284 | + |
| 285 | + var mcpServer = prepareSyncServerBuilder() |
| 286 | + .capabilities(McpSchema.ServerCapabilities.builder().tools(false).build()) |
| 287 | + .tools(tool1) |
| 288 | + .build(); |
| 289 | + |
| 290 | + try (var mcpClient = clientBuilder.build()) { |
| 291 | + String oversizedBody = "a".repeat(MAX_REQUEST_SIZE + 1); |
| 292 | + |
| 293 | + mcpClient.initialize(); |
| 294 | + assertThat(mcpClient.listTools().tools()).contains(tool1.tool()); |
| 295 | + |
| 296 | + assertThatThrownBy(() -> mcpClient.callTool( |
| 297 | + McpSchema.CallToolRequest.builder("tool1").arguments(Map.of("message", oversizedBody)).build())) |
| 298 | + .isInstanceOf(RuntimeException.class) |
| 299 | + .hasMessageContaining("413"); |
| 300 | + } |
| 301 | + finally { |
| 302 | + mcpServer.closeGracefully(); |
| 303 | + } |
| 304 | + } |
| 305 | + |
256 | 306 | @ParameterizedTest |
257 | 307 | @ValueSource(strings = { "text/plain;charset=UTF-8", "application/x-www-form-urlencoded", "multipart/form-data" }) |
258 | 308 | void rejectsInitializeWithNonJsonContentType(String contentType) throws Exception { |
|
0 commit comments