|
4 | 4 |
|
5 | 5 | package io.modelcontextprotocol.server; |
6 | 6 |
|
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.io.InputStreamReader; |
7 | 10 | import java.net.URI; |
8 | 11 | import java.net.http.HttpClient; |
9 | 12 | import java.net.http.HttpRequest; |
|
12 | 15 | import java.nio.charset.StandardCharsets; |
13 | 16 | import java.time.Duration; |
14 | 17 | import java.util.Map; |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import java.util.concurrent.atomic.AtomicBoolean; |
15 | 21 | import java.util.stream.Stream; |
16 | 22 |
|
17 | 23 | import io.modelcontextprotocol.AbstractMcpClientServerIntegrationTests; |
|
35 | 41 | import org.junit.jupiter.params.ParameterizedTest; |
36 | 42 | import org.junit.jupiter.params.provider.Arguments; |
37 | 43 | import org.junit.jupiter.params.provider.MethodSource; |
| 44 | +import org.junit.jupiter.params.provider.ValueSource; |
| 45 | +import reactor.core.publisher.Mono; |
38 | 46 |
|
39 | 47 | import static io.modelcontextprotocol.util.ToolsUtils.EMPTY_JSON_SCHEMA; |
40 | 48 | import static org.assertj.core.api.Assertions.assertThat; |
@@ -224,6 +232,55 @@ public void cancel() { |
224 | 232 | assertThat(response.statusCode()).isEqualTo(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE); |
225 | 233 | } |
226 | 234 |
|
| 235 | + @ParameterizedTest |
| 236 | + @ValueSource(strings = { "text/plain;charset=UTF-8", "application/x-www-form-urlencoded", "multipart/form-data" }) |
| 237 | + void rejectsNonJsonContentType(String contentType) throws Exception { |
| 238 | + var httpClient = HttpClient.newHttpClient(); |
| 239 | + var toolCalled = new AtomicBoolean(); |
| 240 | + prepareAsyncServerBuilder().capabilities(McpSchema.ServerCapabilities.builder().tools(true).build()) |
| 241 | + .tools(McpServerFeatures.AsyncToolSpecification.builder() |
| 242 | + .tool(McpSchema.Tool.builder().name("tool1").inputSchema(EMPTY_JSON_SCHEMA).build()) |
| 243 | + .callHandler((exchange, request) -> { |
| 244 | + toolCalled.set(true); |
| 245 | + return Mono.just(McpSchema.CallToolResult.builder().build()); |
| 246 | + }) |
| 247 | + .build()) |
| 248 | + .build(); |
| 249 | + |
| 250 | + // Establish an SSE session to obtain a valid session ID |
| 251 | + var sseRequest = HttpRequest.newBuilder() |
| 252 | + .uri(URI.create("http://localhost:" + PORT + CUSTOM_SSE_ENDPOINT)) |
| 253 | + .header("Accept", "text/event-stream") |
| 254 | + .GET() |
| 255 | + .build(); |
| 256 | + HttpResponse<InputStream> sseResponse = httpClient.send(sseRequest, HttpResponse.BodyHandlers.ofInputStream()); |
| 257 | + try (var reader = new BufferedReader(new InputStreamReader(sseResponse.body(), StandardCharsets.UTF_8))) { |
| 258 | + var sessionIdFuture = CompletableFuture.supplyAsync(() -> reader.lines() |
| 259 | + .filter(line -> line.startsWith("data:") && line.contains("sessionId=")) |
| 260 | + .map(line -> line.substring(line.indexOf("sessionId=") + "sessionId=".length()).strip()) |
| 261 | + .findFirst() |
| 262 | + .orElseThrow(() -> new IllegalStateException("sessionId not found in SSE stream"))); |
| 263 | + String sessionId = sessionIdFuture.get(5, TimeUnit.SECONDS); |
| 264 | + |
| 265 | + // CORS-safelisted content types can be sent cross-origin by a browser without |
| 266 | + // a |
| 267 | + // preflight, so they must be rejected before the message is handled |
| 268 | + var request = HttpRequest.newBuilder() |
| 269 | + .uri(URI.create("http://localhost:" + PORT + CUSTOM_MESSAGE_ENDPOINT + "?sessionId=" + sessionId)) |
| 270 | + .header("Content-Type", contentType) |
| 271 | + .POST(HttpRequest.BodyPublishers.ofString( |
| 272 | + """ |
| 273 | + {"jsonrpc":"2.0","id":"call-1","method":"tools/call","params":{"name":"tool1","arguments":{}}}""")) |
| 274 | + .build(); |
| 275 | + |
| 276 | + var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); |
| 277 | + |
| 278 | + assertThat(response.statusCode()).isEqualTo(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); |
| 279 | + assertThat(response.body()).contains("Unsupported Media Type: Content-Type must be application/json"); |
| 280 | + assertThat(toolCalled).isFalse(); |
| 281 | + } |
| 282 | + } |
| 283 | + |
227 | 284 | static McpTransportContextExtractor<HttpServletRequest> TEST_CONTEXT_EXTRACTOR = (r) -> McpTransportContext |
228 | 285 | .create(Map.of("important", "value")); |
229 | 286 |
|
|
0 commit comments