|
14 | 14 | import java.net.http.HttpResponse; |
15 | 15 | import java.nio.ByteBuffer; |
16 | 16 | import java.time.Duration; |
| 17 | +import java.util.List; |
17 | 18 | import java.util.Map; |
18 | 19 | import java.util.Queue; |
19 | 20 | import java.util.concurrent.CompletableFuture; |
@@ -212,10 +213,72 @@ void testMissingHandlerReturnsMethodNotFoundError() { |
212 | 213 | } |
213 | 214 | } |
214 | 215 |
|
| 216 | + @Test |
| 217 | + void listeningStreamHasStreamingHeaders() throws Exception { |
| 218 | + prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build(); |
| 219 | + var sessionId = initializeSession(httpClient); |
| 220 | + |
| 221 | + var get = HttpRequest.newBuilder() |
| 222 | + .uri(URI.create("http://localhost:" + PORT + MESSAGE_ENDPOINT)) |
| 223 | + .header("Accept", "text/event-stream") |
| 224 | + .header(HttpHeaders.MCP_SESSION_ID, sessionId) |
| 225 | + .GET() |
| 226 | + .build(); |
| 227 | + |
| 228 | + var response = httpClient.send(get, HttpResponse.BodyHandlers.ofInputStream()); |
| 229 | + try { |
| 230 | + assertSseHeaders(response); |
| 231 | + } |
| 232 | + finally { |
| 233 | + response.body().close(); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + @Test |
| 238 | + void toolCallSseResponseHasStreamingHeaders() throws Exception { |
| 239 | + prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0") |
| 240 | + .capabilities(McpSchema.ServerCapabilities.builder().tools(true).build()) |
| 241 | + .tools(McpServerFeatures.AsyncToolSpecification.builder() |
| 242 | + .tool(McpSchema.Tool.builder("echo", EMPTY_JSON_SCHEMA).description("returns immediately").build()) |
| 243 | + .callHandler((exchange, |
| 244 | + request) -> Mono.just(McpSchema.CallToolResult.builder() |
| 245 | + .content(List.of(McpSchema.TextContent.builder("ok").build())) |
| 246 | + .isError(false) |
| 247 | + .build())) |
| 248 | + .build()) |
| 249 | + .build(); |
| 250 | + var sessionId = initializeSession(httpClient); |
| 251 | + |
| 252 | + var post = HttpRequest.newBuilder() |
| 253 | + .uri(URI.create("http://localhost:" + PORT + MESSAGE_ENDPOINT)) |
| 254 | + .header("Content-Type", "application/json") |
| 255 | + .header("Accept", "text/event-stream, application/json") |
| 256 | + .header(HttpHeaders.MCP_SESSION_ID, sessionId) |
| 257 | + .POST(HttpRequest.BodyPublishers |
| 258 | + .ofString("{\"jsonrpc\":\"2.0\",\"id\":\"call-1\",\"method\":\"tools/call\"," |
| 259 | + + "\"params\":{\"name\":\"echo\",\"arguments\":{}}}")) |
| 260 | + .build(); |
| 261 | + |
| 262 | + var response = httpClient.send(post, HttpResponse.BodyHandlers.ofInputStream()); |
| 263 | + try { |
| 264 | + assertSseHeaders(response); |
| 265 | + } |
| 266 | + finally { |
| 267 | + response.body().close(); |
| 268 | + } |
| 269 | + } |
| 270 | + |
215 | 271 | @Override |
216 | 272 | protected void prepareClients(int port, String mcpEndpoint) { |
217 | 273 | } |
218 | 274 |
|
| 275 | + private static void assertSseHeaders(HttpResponse<?> response) { |
| 276 | + assertThat(response.headers().firstValue("Content-Type")).contains("text/event-stream;charset=UTF-8"); |
| 277 | + assertThat(response.headers().firstValue("Cache-Control")).contains("no-cache, no-transform"); |
| 278 | + assertThat(response.headers().firstValue("X-Accel-Buffering")).contains("no"); |
| 279 | + assertThat(response.headers().firstValue("Connection")).isEmpty(); |
| 280 | + } |
| 281 | + |
219 | 282 | static McpTransportContextExtractor<HttpServletRequest> TEST_CONTEXT_EXTRACTOR = (r) -> McpTransportContext |
220 | 283 | .create(Map.of("important", "value")); |
221 | 284 |
|
|
0 commit comments