FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat: add X-Accel-Buffering: no header to SSE responses by Debasishhh · Pull Request #1139 · modelcontextprotocol/java-sdk · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
response.setCharacterEncoding(UTF_8);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Connection", "keep-alive");
response.setHeader("X-Accel-Buffering", "no");

String sessionId = UUID.randomUUID().toString();
AsyncContext asyncContext = request.startAsync();
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
response.setCharacterEncoding(UTF_8);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Connection", "keep-alive");
response.setHeader("X-Accel-Buffering", "no");

AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
Expand Down Expand Up @@ -597,6 +598,7 @@ else if (message instanceof McpSchema.JSONRPCRequest jsonrpcRequest) {
response.setCharacterEncoding(UTF_8);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Connection", "keep-alive");
response.setHeader("X-Accel-Buffering", "no");

AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,29 @@ public void after() {
protected void prepareClients(int port, String mcpEndpoint) {
}

@Test
void sseResponseIncludesXAccelBufferingHeader() throws Exception {
// https://github.com/modelcontextprotocol/java-sdk/issues/293 - without this
// header, proxies like Nginx buffer the SSE response, breaking real-time
// streaming.
prepareAsyncServerBuilder().build();

var httpClient = HttpClient.newHttpClient();
var sseRequest = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + PORT + CUSTOM_SSE_ENDPOINT))
.header("Accept", "text/event-stream")
.GET()
.build();

var response = httpClient.send(sseRequest, HttpResponse.BodyHandlers.ofInputStream());
try {
assertThat(response.headers().firstValue("X-Accel-Buffering")).contains("no");
}
finally {
response.body().close();
}
}

@Test
void rejectsWhenBodyBytesExceedLimitWithoutContentLengthHeader() throws Exception {
var httpClient = HttpClient.newHttpClient();
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.net.http.HttpResponse;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -204,6 +205,69 @@ void testMissingHandlerReturnsMethodNotFoundError() {
}
}

/**
* https://github.com/modelcontextprotocol/java-sdk/issues/293 - without this header,
* proxies like Nginx buffer the SSE response, breaking real-time streaming.
*/
@Test
void listeningStreamIncludesXAccelBufferingHeader() throws Exception {
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build();
var sessionId = initializeSession(httpClient);

var get = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + PORT + MESSAGE_ENDPOINT))
.header("Accept", "text/event-stream")
.header(HttpHeaders.MCP_SESSION_ID, sessionId)
.GET()
.build();

var response = httpClient.send(get, HttpResponse.BodyHandlers.ofInputStream());
try {
assertThat(response.headers().firstValue("X-Accel-Buffering")).contains("no");
}
finally {
response.body().close();
}
}

/**
* https://github.com/modelcontextprotocol/java-sdk/issues/293 - same header is also
* required on the SSE response a POST request opens for a streamed tool call.
*/
@Test
void toolCallSseResponseIncludesXAccelBufferingHeader() throws Exception {
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0")
.capabilities(McpSchema.ServerCapabilities.builder().tools(true).build())
.tools(McpServerFeatures.AsyncToolSpecification.builder()
.tool(McpSchema.Tool.builder("echo", EMPTY_JSON_SCHEMA).description("returns immediately").build())
.callHandler((exchange,
request) -> Mono.just(McpSchema.CallToolResult.builder()
.content(List.of(McpSchema.TextContent.builder("ok").build()))
.isError(false)
.build()))
.build())
.build();
var sessionId = initializeSession(httpClient);

var post = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + PORT + MESSAGE_ENDPOINT))
.header("Content-Type", "application/json")
.header("Accept", "text/event-stream, application/json")
.header(HttpHeaders.MCP_SESSION_ID, sessionId)
.POST(HttpRequest.BodyPublishers
.ofString("{\"jsonrpc\":\"2.0\",\"id\":\"call-1\",\"method\":\"tools/call\","
+ "\"params\":{\"name\":\"echo\",\"arguments\":{}}}"))
.build();

var response = httpClient.send(post, HttpResponse.BodyHandlers.ofInputStream());
try {
assertThat(response.headers().firstValue("X-Accel-Buffering")).contains("no");
}
finally {
response.body().close();
}
}

@Override
protected void prepareClients(int port, String mcpEndpoint) {
}
Expand Down

Back | FazBrowse Home | New Git URL