|
6 | 6 |
|
7 | 7 | import java.time.Duration; |
8 | 8 | import java.util.Map; |
| 9 | +import java.util.concurrent.atomic.AtomicBoolean; |
9 | 10 | import java.util.stream.Stream; |
10 | 11 |
|
11 | 12 | import org.junit.jupiter.api.AfterEach; |
12 | 13 | import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Test; |
13 | 15 | import org.junit.jupiter.api.Timeout; |
| 16 | +import org.junit.jupiter.params.ParameterizedTest; |
14 | 17 | import org.junit.jupiter.params.provider.Arguments; |
| 18 | +import org.junit.jupiter.params.provider.ValueSource; |
15 | 19 |
|
| 20 | +import org.springframework.http.HttpStatus; |
| 21 | +import org.springframework.http.MediaType; |
16 | 22 | import org.springframework.http.server.reactive.HttpHandler; |
17 | 23 | import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter; |
18 | 24 | import org.springframework.web.reactive.function.client.WebClient; |
|
26 | 32 | import io.modelcontextprotocol.server.McpServer; |
27 | 33 | import io.modelcontextprotocol.server.McpServer.AsyncSpecification; |
28 | 34 | import io.modelcontextprotocol.server.McpServer.SyncSpecification; |
| 35 | +import io.modelcontextprotocol.server.McpServerFeatures; |
29 | 36 | import io.modelcontextprotocol.server.McpTransportContextExtractor; |
30 | 37 | import io.modelcontextprotocol.server.TestUtil; |
31 | 38 | import io.modelcontextprotocol.server.transport.WebFluxStreamableServerTransportProvider; |
| 39 | +import io.modelcontextprotocol.spec.HttpHeaders; |
| 40 | +import io.modelcontextprotocol.spec.McpSchema; |
| 41 | +import reactor.core.publisher.Mono; |
32 | 42 | import reactor.netty.DisposableServer; |
33 | 43 | import reactor.netty.http.server.HttpServer; |
34 | 44 |
|
| 45 | +import static io.modelcontextprotocol.util.ToolsUtils.EMPTY_JSON_SCHEMA; |
| 46 | +import static org.assertj.core.api.Assertions.assertThat; |
| 47 | + |
35 | 48 | @Timeout(15) |
36 | 49 | class WebFluxStreamableIntegrationTests extends AbstractMcpClientServerIntegrationTests { |
37 | 50 |
|
@@ -100,4 +113,76 @@ public void after() { |
100 | 113 | } |
101 | 114 | } |
102 | 115 |
|
| 116 | + @ParameterizedTest |
| 117 | + @ValueSource(strings = { "text/plain;charset=UTF-8", "application/x-www-form-urlencoded", "multipart/form-data" }) |
| 118 | + void rejectsInitializeWithNonJsonContentType(String contentType) { |
| 119 | + var webClient = WebClient.create("http://localhost:" + PORT); |
| 120 | + prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build(); |
| 121 | + |
| 122 | + // CORS-safelisted content types can be sent cross-origin by a browser without a |
| 123 | + // preflight, so they must be rejected before a session is created |
| 124 | + var response = webClient.post() |
| 125 | + .uri(CUSTOM_MESSAGE_ENDPOINT) |
| 126 | + .contentType(MediaType.parseMediaType(contentType)) |
| 127 | + .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON) |
| 128 | + .bodyValue(""" |
| 129 | + {"jsonrpc":"2.0","id":"init","method":"initialize","params":{ |
| 130 | + "protocolVersion":"2025-06-18","capabilities":{}, |
| 131 | + "clientInfo":{"name":"test-client","version":"1.0.0"}}}""") |
| 132 | + .exchangeToMono(clientResponse -> clientResponse.toEntity(String.class)) |
| 133 | + .block(); |
| 134 | + |
| 135 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE); |
| 136 | + assertThat(response.getBody()).contains("Unsupported Media Type: Content-Type must be application/json"); |
| 137 | + assertThat(response.getHeaders().containsKey(HttpHeaders.MCP_SESSION_ID)).isFalse(); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void rejectsToolCallWithNonJsonContentType() { |
| 142 | + var webClient = WebClient.create("http://localhost:" + PORT); |
| 143 | + var toolCalled = new AtomicBoolean(); |
| 144 | + prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0") |
| 145 | + .capabilities(McpSchema.ServerCapabilities.builder().tools(true).build()) |
| 146 | + .tools(McpServerFeatures.AsyncToolSpecification.builder() |
| 147 | + .tool(McpSchema.Tool.builder().name("tool1").inputSchema(EMPTY_JSON_SCHEMA).build()) |
| 148 | + .callHandler((exchange, request) -> { |
| 149 | + toolCalled.set(true); |
| 150 | + return Mono.just(McpSchema.CallToolResult.builder().build()); |
| 151 | + }) |
| 152 | + .build()) |
| 153 | + .build(); |
| 154 | + var sessionId = initializeSession(webClient); |
| 155 | + |
| 156 | + var response = webClient.post() |
| 157 | + .uri(CUSTOM_MESSAGE_ENDPOINT) |
| 158 | + .contentType(MediaType.parseMediaType("text/plain;charset=UTF-8")) |
| 159 | + .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON) |
| 160 | + .header(HttpHeaders.MCP_SESSION_ID, sessionId) |
| 161 | + .bodyValue(""" |
| 162 | + {"jsonrpc":"2.0","id":"call-1","method":"tools/call","params":{"name":"tool1","arguments":{}}}""") |
| 163 | + .exchangeToMono(clientResponse -> clientResponse.toEntity(String.class)) |
| 164 | + .block(); |
| 165 | + |
| 166 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE); |
| 167 | + assertThat(response.getBody()).contains("Unsupported Media Type: Content-Type must be application/json"); |
| 168 | + assertThat(toolCalled).isFalse(); |
| 169 | + } |
| 170 | + |
| 171 | + private String initializeSession(WebClient webClient) { |
| 172 | + var response = webClient.post() |
| 173 | + .uri(CUSTOM_MESSAGE_ENDPOINT) |
| 174 | + .contentType(MediaType.APPLICATION_JSON) |
| 175 | + .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON) |
| 176 | + .bodyValue(""" |
| 177 | + {"jsonrpc":"2.0","id":"init","method":"initialize","params":{ |
| 178 | + "protocolVersion":"2025-06-18","capabilities":{}, |
| 179 | + "clientInfo":{"name":"test-client","version":"1.0.0"}}}""") |
| 180 | + .exchangeToMono(clientResponse -> clientResponse.toEntity(String.class)) |
| 181 | + .block(); |
| 182 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 183 | + var sessionId = response.getHeaders().getFirst(HttpHeaders.MCP_SESSION_ID); |
| 184 | + assertThat(sessionId).isNotNull(); |
| 185 | + return sessionId; |
| 186 | + } |
| 187 | + |
103 | 188 | } |
0 commit comments