Skip to content

Support accessing HTTP headers in ToolContext when using WebFlux #2757

Open
@18801151992

Description

@18801151992

Please do a quick search on GitHub issues first, the feature you are about to request might have already been requested.

Expected Behavior

When using Spring AI (1.0.0-M7) with WebFlux, the ToolContext should include HTTP headers from the incoming request, so that they can be accessed within a @tool method.
@tool
public String example(String input, ToolContext toolContext) {
String token = toolContext.get("X-Token", String.class);
return "Token: " + token;
}
This would allow developers to use authentication tokens or other metadata from headers during tool execution, without requiring custom logic.

Current Behavior

In the current version (M7), when running on WebFlux, the ToolContext does not include HTTP headers, unlike what some might expect based on MVC or previous versions.

Because WebFlux is reactive and does not use RequestContextHolder, there is no easy built-in way to access headers from within a @tool method.

Attempts to extract headers using ToolContext.get(...) return null.

Context

This affects any project using Spring AI with spring-ai-starter-mcp-server-webflux.

In my use case, I need to extract an authentication token from headers like X-Token or Authorization to perform access control logic within tools.

I considered overriding the tool execution pipeline, but SyncMcpToolCallbackProvider does not expose a customizable invokeTool() method.

A possible workaround is to write a wrapper controller that extracts headers and manually adds them to ToolContext, but this breaks the built-in tool invocation pattern.

Please consider adding support for automatically injecting headers into ToolContext for WebFlux-based deployments, or documenting the official workaround if available.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions