Skip to content

IHttpContextAccessor stopped working since 0.1.0-preview.10 #365

Open
@xiaomi7732

Description

@xiaomi7732

Describe the bug
Refer to #185, there's an example fetching request info from IHttpContextAccessor.

It worked in 0.1.0-preview.9 but not working in preview 10 and 11.

To Reproduce
Steps to reproduce the behavior:

  1. Add service:

    builder.Services.AddHttpContextAccessor();
  2. Inject and use:

    [McpServerToolType]
    public class EchoTool(IHttpContextAccessor contextAccessor)
    {
        private readonly IHttpContextAccessor _contextAccessor = contextAccessor ?? throw new ArgumentNullException(nameof(contextAccessor));
    
        [McpServerTool, Description("Echoes the message back to the client.")]
        public string Echo(string message)
        {
            return $"hello {message}, {_contextAccessor.HttpContext?.Request.Path ?? "(no HttpContext)"}";
        }
    }

Expected behavior
The _contextAccessor.HttpContext is not null, and provides current httpContext.

It is in preview 10 and preview 11. (Same code works in preview.9).

Logs
Having IHttpAccessor allows checking authorization header until the official authorization middleware is in place.
Looking forward to #349 .

Additional context

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