spring-ai
spring-ai copied to clipboard
Incorrect Error handling when calling MCP tools
Codebase: M7 Issue Description: When we execute MCP Tools it's normal that tool can report errors, for example: MCP Tool is handling renaming files, we fire this tool and trying to rename a file which don't exist, Tool will report an error.
Current state: When LLM execute some MCP tool and MCP tool reports error - this will terminate execution flow with following error:
java.lang.IllegalStateException: Error calling tool: [TextContent[audience=null, priority=null, text=file not found]] at org.springframework.ai.mcp.SyncMcpToolCallback.call(SyncMcpToolCallback.java:117) ~[spring-ai-mcp-1.0.0-M7.jar:1.0.0-M7] at org.springframework.ai.mcp.SyncMcpToolCallback.call(SyncMcpToolCallback.java:125) ~[spring-ai-mcp-1.0.0-M7.jar:1.0.0-M7] at org.springframework.ai.model.tool.DefaultToolCallingManager.executeToolCall(DefaultToolCallingManager.java:227) ~[spring-ai-model-1.0.0-M7.jar:1.0.0-M7] at org.springframework.ai.model.tool.DefaultToolCallingManager.executeToolCalls(DefaultToolCallingManager.java:139) ~[spring-ai-model-1.0.0-M7.jar:1.0.0-M7] at com.example.demo.MyController.processRequest(MyController.java:234) ~[main/:na]
Expected Behavior: Error should be processed by ToolExecutionExceptionProcessor and result should be returned to LLM
Issue analysis: DefaultToolCallingManager should catch this kind of errors but in current code state if MCP Tool calls produce an error they will throw IllegalStateException but DefaultToolCallingManager is expecting ToolExecutionException in case of error in Tool execution.
Reference points: https://github.com/spring-projects/spring-ai/blob/81b715b3d2ea06e0216b13bf5c7a79a31b52e4e6/spring-ai-model/src/main/java/org/springframework/ai/model/tool/DefaultToolCallingManager.java#L204-L210
https://github.com/spring-projects/spring-ai/blob/81b715b3d2ea06e0216b13bf5c7a79a31b52e4e6/mcp/common/src/main/java/org/springframework/ai/mcp/SyncMcpToolCallback.java#L111-L120
https://github.com/spring-projects/spring-ai/blob/81b715b3d2ea06e0216b13bf5c7a79a31b52e4e6/mcp/common/src/main/java/org/springframework/ai/mcp/AsyncMcpToolCallback.java#L107-L117