-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Support accessing HTTP headers in ToolContext when using WebFlux #2757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Is there any solution available now? I am also facing this issue. |
I have this problem too |
It seems that the current MCP SDKS all have similar problems. I tried to use the python sdk to obtain the headers, but it didn't work either. |
python其实是可以解决的,你在查查gpt吧 |
modelcontextprotocol/java-sdk#172 |
+1 Without getting the authenticated user information from acces token, ID token, or headers, a (remote) MCP server does not make a lot of sense. |
Not only WebFlux, but WebMVC is also not supported. // Get current request using RequestContextHolder
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
String token= null;
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
token= request.getHeader("token");
logger.info("Received token: {}", token);
} else {
logger.warn("No request context available");
} |
@heavenwing There is no problem with webmvc, at least there is no problem here. You can check whether the tool and controller are in the same thread. |
@heavenwing WebMVC also does not provide built-in support and requires external plugins to handle thread-related issues. This problem can be resolved by using Alibaba's thread plugin. |
MVC is supported. There is no place in the tool that uses a new thread. You can get the Header from RequestContextHolder.getRequestAttributes(). My project is already running。Flux is a different thread |
@poo0054 I don't understand this sentence "You can check whether the tool and controller are in the same thread." @Bean
public ToolCallbackProvider materialTools(MaterialService materialService) {
return MethodToolCallbackProvider.builder().toolObjects(materialService).build();
} And MaterialService have a Tool method, I try to get request header in this method, but RequestContextHolder.getRequestAttributes() return null |
Can you provide a minimal example? |
hao can i use RequestContextHolder in @tool |
这个问题可以关了,已经有解决方法了。使用 This issue can be closed as there's already a solution. Just use BiFunction<BiFunctionEvents.Request, ToolContext, String>. @Bean
public FunctionToolCallback biFunctionEvents() {
return FunctionToolCallback.builder("getTime", new BiFunctionEvents())
.description("获取时间")
.inputType(BiFunctionEvents.Request.class)
.build();
}
@Autowired
public void setChatClientBuilder(ChatClient.Builder chatClientBuilder,
FunctionCallback biFunctionEvents) {
this.chatClient = chatClientBuilder
.clone()
.defaultTools(biFunctionEvents)
.defaultAdvisors(new SimpleLoggerAdvisor(Ordered.LOWEST_PRECEDENCE - 1))
.build();
}
@GetMapping("/quality")
public Flux<String> quality(@RequestParam("message") String message) {
UserMessage userMessage = new UserMessage(message);
String format = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
LogMessage logMessage = LogMessage.format("今天是日期为:[%s]。", format);
ChatClient.StreamResponseSpec stream = chatClient.prompt()
.messages(userMessage)
.toolContext(Map.of("format", format))
.system(logMessage.toString())
.stream();
return stream.content();
}
public class BiFunctionEvents implements BiFunction<BiFunctionEvents.Request, ToolContext, String> {
@Override
public String apply(@ToolParam(description = "位置信息") Request location, ToolContext toolContext) {
System.out.println(toolContext);
return toolContext.getContext().get("format").toString();
}
public record Request(@ToolParam(description = "位置信息") String location) {
}
} |
Source code is here
@Override
public String call(String toolInput, @Nullable ToolContext toolContext) {
Assert.hasText(toolInput, "toolInput cannot be null or empty");
logger.debug("Starting execution of tool: {}", toolDefinition.name());
I request = JsonParser.fromJson(toolInput, toolInputType);
O response = toolFunction.apply(request, toolContext);
logger.debug("Successful execution of tool: {}", toolDefinition.name());
return toolCallResultConverter.convert(response, null);
} |
你这个是client端吧,@tools 方法ToolContext 里面就一个exchange的对象 |
我的 my |
是的,我这个是clinet,server端我还没试。 |
Related to #2378 |
In @Override
public String call(String toolArguments, ToolContext toolContext) {
// ToolContext is not supported by the MCP tools
return this.call(toolArguments);
} ToolContext is not supported by the MCP tools |
我需要在mcp server端来获取headers里的信息 |
已放弃使用Java来开发Remote MCP Server,.NET开发的话,这些问题都不是问题。不过我现在是用Python实现了Local MCP Server,但是使用的时候,还是加载为Semantic Kernel的Function。 I have abandoned using Java to develop the Remote MCP Server. With .NET development, these issues are not a problem. However, I am currently using Python to implement the Local MCP Server, but when using it, it is still loaded as a Semantic Kernel Function. |
No matter what language you use to develop MCP, it is the same. Please refer to #2432. java:Unless you change |
No, .NET implementation is working. sample code is here: |
Wait for the community to update, or submit a PR. This implementation is very simple, I don't have time recently。 I'm not sure what the official position is. Some of them seem to think that the current framework or even the protocol (SSE/STDIO transport) does not support ToolContext functionality. |
SSE is a temporary choice, Streamable HTTP will be official HTTP Protocol. |
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!
The text was updated successfully, but these errors were encountered: