Skip to content

Commit 5c9fee1

Browse files
committed
add tests for the X-Accel-Buffering SSE response header
Covers all three response paths that open an SSE stream: the legacy SSE transport's GET endpoint, the streamable transport's GET listening stream, and the streamable transport's POST tool-call response. Verified each test fails without the header and passes with it.
1 parent 78483c8 commit 5c9fee1

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

‎mcp-test/src/test/java/io/modelcontextprotocol/server/HttpServletSseIntegrationTests.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,29 @@ public void after() {
121121
protected void prepareClients(int port, String mcpEndpoint) {
122122
}
123123

124+
@Test
125+
void sseResponseIncludesXAccelBufferingHeader() throws Exception {
126+
// https://github.com/modelcontextprotocol/java-sdk/issues/293 - without this
127+
// header, proxies like Nginx buffer the SSE response, breaking real-time
128+
// streaming.
129+
prepareAsyncServerBuilder().build();
130+
131+
var httpClient = HttpClient.newHttpClient();
132+
var sseRequest = HttpRequest.newBuilder()
133+
.uri(URI.create("http://localhost:" + PORT + CUSTOM_SSE_ENDPOINT))
134+
.header("Accept", "text/event-stream")
135+
.GET()
136+
.build();
137+
138+
var response = httpClient.send(sseRequest, HttpResponse.BodyHandlers.ofInputStream());
139+
try {
140+
assertThat(response.headers().firstValue("X-Accel-Buffering")).contains("no");
141+
}
142+
finally {
143+
response.body().close();
144+
}
145+
}
146+
124147
@Test
125148
void rejectsWhenBodyBytesExceedLimitWithoutContentLengthHeader() throws Exception {
126149
var httpClient = HttpClient.newHttpClient();

‎mcp-test/src/test/java/io/modelcontextprotocol/server/HttpServletStreamableIntegrationTests.java‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.net.http.HttpResponse;
1515
import java.nio.ByteBuffer;
1616
import java.time.Duration;
17+
import java.util.List;
1718
import java.util.Map;
1819
import java.util.Queue;
1920
import java.util.concurrent.CompletableFuture;
@@ -204,6 +205,69 @@ void testMissingHandlerReturnsMethodNotFoundError() {
204205
}
205206
}
206207

208+
/**
209+
* https://github.com/modelcontextprotocol/java-sdk/issues/293 - without this header,
210+
* proxies like Nginx buffer the SSE response, breaking real-time streaming.
211+
*/
212+
@Test
213+
void listeningStreamIncludesXAccelBufferingHeader() throws Exception {
214+
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build();
215+
var sessionId = initializeSession(httpClient);
216+
217+
var get = HttpRequest.newBuilder()
218+
.uri(URI.create("http://localhost:" + PORT + MESSAGE_ENDPOINT))
219+
.header("Accept", "text/event-stream")
220+
.header(HttpHeaders.MCP_SESSION_ID, sessionId)
221+
.GET()
222+
.build();
223+
224+
var response = httpClient.send(get, HttpResponse.BodyHandlers.ofInputStream());
225+
try {
226+
assertThat(response.headers().firstValue("X-Accel-Buffering")).contains("no");
227+
}
228+
finally {
229+
response.body().close();
230+
}
231+
}
232+
233+
/**
234+
* https://github.com/modelcontextprotocol/java-sdk/issues/293 - same header is also
235+
* required on the SSE response a POST request opens for a streamed tool call.
236+
*/
237+
@Test
238+
void toolCallSseResponseIncludesXAccelBufferingHeader() 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+
assertThat(response.headers().firstValue("X-Accel-Buffering")).contains("no");
265+
}
266+
finally {
267+
response.body().close();
268+
}
269+
}
270+
207271
@Override
208272
protected void prepareClients(int port, String mcpEndpoint) {
209273
}

0 commit comments

Comments
 (0)