Skip to content

Add X-Accel-Buffering header to SSE responses#1294

Merged
stephentoub merged 2 commits intomainfrom
copilot/add-sse-header-support
Feb 17, 2026
Merged

Add X-Accel-Buffering header to SSE responses#1294
stephentoub merged 2 commits intomainfrom
copilot/add-sse-header-support

Conversation

Copy link
Contributor

Copilot AI commented Feb 17, 2026

Per MCP spec update (435a718), SSE responses should include X-Accel-Buffering: no to prevent reverse proxy buffering.

Changes

  • Added header to StreamableHttpHandler.InitializeSseResponse - the single initialization point for all SSE response paths (Streamable HTTP POST/GET, resumed streams, legacy SSE transport)
  • Added test coverage in StreamableHttpServerConformanceTests and updated SseServerIntegrationTests
internal static void InitializeSseResponse(HttpContext context)
{
    context.Response.Headers.ContentType = "text/event-stream";
    context.Response.Headers.CacheControl = "no-cache,no-store";
    context.Response.Headers.ContentEncoding = "identity";
    context.Response.Headers["X-Accel-Buffering"] = "no";  // Added
    context.Features.GetRequiredFeature<IHttpResponseBodyFeature>().DisableBuffering();
}
Original prompt

Context

The MCP specification was updated in modelcontextprotocol/modelcontextprotocol@435a718 to add a new "SSE Stream Configuration" section recommending that servers SHOULD include the X-Accel-Buffering: no header in HTTP responses that return Content-Type: text/event-stream. This header instructs reverse proxies (such as nginx) to disable response buffering, ensuring that SSE events are delivered to clients immediately rather than being held in a buffer. Without this header, proxies may accumulate messages before sending them to the client, introducing unwanted latency and potentially breaking the real-time nature of SSE communication.

Problem

The C# SDK does not currently set the X-Accel-Buffering: no header on any SSE responses. A search for X-Accel-Buffering across the entire repository returns zero results.

Required Change

All SSE responses in the C# SDK flow through a single centralized method: StreamableHttpHandler.InitializeSseResponse in src/ModelContextProtocol.AspNetCore/StreamableHttpHandler.cs. This method currently sets Content-Type, Cache-Control, Content-Encoding, and disables buffering, but does not set X-Accel-Buffering.

Add the X-Accel-Buffering: no response header in InitializeSseResponse. The current method looks like:

internal static void InitializeSseResponse(HttpContext context)
{
    context.Response.Headers.ContentType = "text/event-stream";
    context.Response.Headers.CacheControl = "no-cache,no-store";

    // Make sure we disable all response buffering for SSE.
    context.Response.Headers.ContentEncoding = "identity";
    context.Features.GetRequiredFeature<IHttpResponseBodyFeature>().DisableBuffering();
}

Add one line to set the header:

context.Response.Headers["X-Accel-Buffering"] = "no";

This single change covers all SSE response paths since InitializeSseResponse is called from:

  • HandlePostRequestAsync (Streamable HTTP POST → SSE response)
  • HandleGetRequestAsyncHandleUnsolicitedMessageStreamAsync (Streamable HTTP GET → SSE stream)
  • HandleResumedStreamAsync (Streamable HTTP GET with Last-Event-ID)
  • HandleResumePostResponseStreamAsync (resuming a POST response)
  • SseHandler.HandleSseRequestAsync (legacy SSE transport)

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Add X-Accel-Buffering header to SSE responses Add X-Accel-Buffering header to SSE responses Feb 17, 2026
Copilot AI requested a review from stephentoub February 17, 2026 09:15
@stephentoub stephentoub marked this pull request as ready for review February 17, 2026 09:20
@stephentoub stephentoub requested a review from halter73 February 17, 2026 09:20
Copy link
Contributor

@halter73 halter73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already set Content-Encoding: identity to prevent similar buffering/compression in IIS. I'm surprised that this pretty NGINX-specific header made its way into the spec, but I don't see the harm in including it by default in the SDK regardless.

@stephentoub stephentoub merged commit 2e17550 into main Feb 17, 2026
9 of 10 checks passed
@stephentoub stephentoub deleted the copilot/add-sse-header-support branch February 17, 2026 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants