-
Notifications
You must be signed in to change notification settings - Fork 1.5k
SyncMcpToolCallback call fails with UnsupportedOperationException if ToolContext is set #2378
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
Thanks @henningSaulCM , I believe this is a know issue and should have been resolved by |
Likely the same as #2337 |
Hi @tzolov, thanks for chiming in. I just tried with 1.0.0-SNAPSHOT (spring-ai-core-1.0.0-20250307.123602-1325.jar) and it seems I'm still getting the UnsupportedOperationException. Stacktrace (excerpt):
Any thoughts? |
@henningSaulCM I overlooked that AsyncMcpToolCallback and SyncMcpToolCallback don't implement String call(String toolInput, ToolContext tooContext). |
Hi @tzolov,
Yes, this is happening because I was trying to integrate MCP tools with an existing Spring AI application that uses custom Spring AI tools and custom (non-empty) ToolContext. If MCP does not support a ToolContext (yet), ignoring the ToolContext for MCP Tool calls would be just fine IMHO to avoid the Exception. #2432 looks good to me. Cheers |
- Implement the new ToolCallback.call(String, ToolContext) method in both Sync and Async MCP tool callbacks. - Since MCP tools don't support tool context, the implementation ignores the context parameter and delegates to the existing call(String) method. Added test to verify the behavior. Resolves spring-projects#2378 Signed-off-by: Christian Tzolov <[email protected]>
- Implement the new ToolCallback.call(String, ToolContext) method in both Sync and Async MCP tool callbacks. - Since MCP tools don't support tool context, the implementation ignores the context parameter and delegates to the existing call(String) method. Added test to verify the behavior. Resolves spring-projects#2378 Signed-off-by: Christian Tzolov <[email protected]>
- Implement the new ToolCallback.call(String, ToolContext) method in both Sync and Async MCP tool callbacks. - Since MCP tools don't support tool context, the implementation ignores the context parameter and delegates to the existing call(String) method. Added test to verify the behavior. Resolves spring-projects#2378 Signed-off-by: Christian Tzolov <[email protected]>
- Implement the new ToolCallback.call(String, ToolContext) method in both Sync and Async MCP tool callbacks. - Since MCP tools don't support tool context, the implementation ignores the context parameter and delegates to the existing call(String) method. Added test to verify the behavior. Resolves spring-projects#2378 Signed-off-by: Christian Tzolov <[email protected]>
- Implement the new ToolCallback.call(String, ToolContext) method in both Sync and Async MCP tool callbacks. - Since MCP tools don't support tool context, the implementation ignores the context parameter and delegates to the existing call(String) method. Added test to verify the behavior. Resolves spring-projects#2378 Signed-off-by: Christian Tzolov <[email protected]>
- Implement the new ToolCallback.call(String, ToolContext) method in both Sync and Async MCP tool callbacks. - Since MCP tools don't support tool context, the implementation ignores the context parameter and delegates to the existing call(String) method. Added test to verify the behavior. Resolves spring-projects#2378 Signed-off-by: Christian Tzolov <[email protected]>
@tzolov when you said " as the context only makes sense within Spring AI Tool Calling." can you please elaborate. Do you mean this is supported only when the tool is not remotely hosted over sse? I am attempting to pass a bearer token back to the MCP server using some mechanism - I thought ToolContext would be the right way, but if it is not supported, I am at a loss on how to pass a special parameter back that the AI agent is not allowed to touch. |
I am also interested in this topic. |
I think I got to the bottom of this, currently the framework and maybe even the protocol (SSE/STDIO transport) does not support the ToolContext feature, I got it working with great success by pulling the tool into the App server's memory and process space by using org.springframework.ai.tool.method.MethodToolCallback In order to get a reference to the MethodToolCallback objects you annotated and scanned in your App server, just autowire it:
This allows you to pass in the HttpServletRequest object and do all sorts of fancy things:
For example, you can have the tool return the time in the visitor's own timezone:
|
Bug description
Integrating and calling an MCP Tool with an existing Spring AI application that uses a ToolContext will result in an UnsupportedOperationException.
Environment
Spring AI 1.0.0-M6
Steps to reproduce
Exception is raised here:
spring-ai/spring-ai-core/src/main/java/org/springframework/ai/tool/ToolCallback.java
Line 57 in 2394ac8
Expected behavior
The MCP printEnv tool is called, ignoring the ToolContext (assuming this concept is not supported in MCP).
The text was updated successfully, but these errors were encountered: