Skip to content

Commit 2e17550

Browse files
Copilotstephentoub
andauthored
Add X-Accel-Buffering header to SSE responses (#1294)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 43a302f commit 2e17550

3 files changed

Lines changed: 13 additions & 0 deletions

File tree

src/ModelContextProtocol.AspNetCore/StreamableHttpHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ internal static void InitializeSseResponse(HttpContext context)
339339

340340
// Make sure we disable all response buffering for SSE.
341341
context.Response.Headers.ContentEncoding = "identity";
342+
context.Response.Headers["X-Accel-Buffering"] = "no";
342343
context.Features.GetRequiredFeature<IHttpResponseBodyFeature>().DisableBuffering();
343344
}
344345

tests/ModelContextProtocol.AspNetCore.Tests/SseServerIntegrationTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public async Task EventSourceResponse_Includes_ExpectedHeaders()
2626
Assert.NotNull(sseResponse.Headers.CacheControl);
2727
Assert.True(sseResponse.Headers.CacheControl.NoStore);
2828
Assert.True(sseResponse.Headers.CacheControl.NoCache);
29+
Assert.Equal("no", Assert.Single(sseResponse.Headers.GetValues("X-Accel-Buffering")));
2930
}
3031

3132
[Fact]

tests/ModelContextProtocol.AspNetCore.Tests/StreamableHttpServerConformanceTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ public async Task InitialPostResponse_Includes_McpSessionIdHeader()
9292
Assert.Equal("text/event-stream", Assert.Single(response.Content.Headers.GetValues("content-type")));
9393
}
9494

95+
[Fact]
96+
public async Task SseResponse_Includes_XAccelBufferingHeader()
97+
{
98+
await StartAsync();
99+
100+
using var response = await HttpClient.PostAsync("", JsonContent(InitializeRequest), TestContext.Current.CancellationToken);
101+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
102+
Assert.Equal("text/event-stream", response.Content.Headers.ContentType?.MediaType);
103+
Assert.Equal("no", Assert.Single(response.Headers.GetValues("X-Accel-Buffering")));
104+
}
105+
95106
[Fact]
96107
public async Task PostRequest_IsUnsupportedMediaType_WithoutJsonContentType()
97108
{

0 commit comments

Comments
 (0)