Skip to content

HttpContextAccessor has empty HttpContext for HTTP Streaming but not for SSE #633

@ouvreboite

Description

@ouvreboite

Describe the bug
How/when the dependencies are injected is inconsistent between SSE and HttpStreaming.

Mainly, the dependencies for HTTP streaming (non-stateless) are derived from StreamableHttpHandler.ApplicationServices and this handler is registered as a singleton. So the HttpContextAccessor has an empty HttpContext.
On the other hand, the dependencies for SSE are derived from the HttpContext.RequestServices (in SseHandler), so the HttpContextAccessor has a filled HttpContext

To Reproduce

builder.Services.AddHttpContextAccessor();
builder.Services.AddMcpServer()
    .WithHttpTransport()
    .WithTools<EchoTool>()

[McpServerToolType]
public sealed class EchoTool(IHttpContextAccessor accessor)
{
    [McpServerTool]
    public bool HasContext() => accessor.HttpContext != null;
}
  • Connect with SSE: HasContext returns "true" ✅
  • Connect with non-stateless HttpStreaming: HasContext returns "false" 🔴

Naive question: why can't we use HttpContext.RequestServices in both?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions