|
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,6 +213,69 @@ void testMissingHandlerReturnsMethodNotFoundError() { |
212 | 213 | } |
213 | 214 | } |
214 | 215 |
|
| 216 | + /** |
| 217 | + * https://github.com/modelcontextprotocol/java-sdk/issues/293 - without this header, |
| 218 | + * proxies like Nginx buffer the SSE response, breaking real-time streaming. |
| 219 | + */ |
| 220 | + @Test |
| 221 | + void listeningStreamIncludesXAccelBufferingHeader() throws Exception { |
| 222 | + prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build(); |
| 223 | + var sessionId = initializeSession(httpClient); |
| 224 | + |
| 225 | + var get = HttpRequest.newBuilder() |
| 226 | + .uri(URI.create("http://localhost:" + PORT + MESSAGE_ENDPOINT)) |
| 227 | + .header("Accept", "text/event-stream") |
| 228 | + .header(HttpHeaders.MCP_SESSION_ID, sessionId) |
| 229 | + .GET() |
| 230 | + .build(); |
| 231 | + |
| 232 | + var response = httpClient.send(get, HttpResponse.BodyHandlers.ofInputStream()); |
| 233 | + try { |
| 234 | + assertThat(response.headers().firstValue("X-Accel-Buffering")).contains("no"); |
| 235 | + } |
| 236 | + finally { |
| 237 | + response.body().close(); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * https://github.com/modelcontextprotocol/java-sdk/issues/293 - same header is also |
| 243 | + * required on the SSE response a POST request opens for a streamed tool call. |
| 244 | + */ |
| 245 | + @Test |
| 246 | + void toolCallSseResponseIncludesXAccelBufferingHeader() throws Exception { |
| 247 | + prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0") |
| 248 | + .capabilities(McpSchema.ServerCapabilities.builder().tools(true).build()) |
| 249 | + .tools(McpServerFeatures.AsyncToolSpecification.builder() |
| 250 | + .tool(McpSchema.Tool.builder("echo", EMPTY_JSON_SCHEMA).description("returns immediately").build()) |
| 251 | + .callHandler((exchange, |
| 252 | + request) -> Mono.just(McpSchema.CallToolResult.builder() |
| 253 | + .content(List.of(McpSchema.TextContent.builder("ok").build())) |
| 254 | + .isError(false) |
| 255 | + .build())) |
| 256 | + .build()) |
| 257 | + .build(); |
| 258 | + var sessionId = initializeSession(httpClient); |
| 259 | + |
| 260 | + var post = HttpRequest.newBuilder() |
| 261 | + .uri(URI.create("http://localhost:" + PORT + MESSAGE_ENDPOINT)) |
| 262 | + .header("Content-Type", "application/json") |
| 263 | + .header("Accept", "text/event-stream, application/json") |
| 264 | + .header(HttpHeaders.MCP_SESSION_ID, sessionId) |
| 265 | + .POST(HttpRequest.BodyPublishers |
| 266 | + .ofString("{\"jsonrpc\":\"2.0\",\"id\":\"call-1\",\"method\":\"tools/call\"," |
| 267 | + + "\"params\":{\"name\":\"echo\",\"arguments\":{}}}")) |
| 268 | + .build(); |
| 269 | + |
| 270 | + var response = httpClient.send(post, HttpResponse.BodyHandlers.ofInputStream()); |
| 271 | + try { |
| 272 | + assertThat(response.headers().firstValue("X-Accel-Buffering")).contains("no"); |
| 273 | + } |
| 274 | + finally { |
| 275 | + response.body().close(); |
| 276 | + } |
| 277 | + } |
| 278 | + |
215 | 279 | @Override |
216 | 280 | protected void prepareClients(int port, String mcpEndpoint) { |
217 | 281 | } |
|
0 commit comments