Skip to content

When the MCP calls a tool and returns an error message, it causes the entire process to be interrupted. #3023

Open
@zhonghua1016

Description

@zhonghua1016

Bug description
When the MCP calls a tool and returns an error message, it causes the entire process to be interrupted.

Environment
spring ai v1.0.0-M8

Steps to reproduce
When calling the mcp-server tool, if the tool returns a result with an error indicator.

Expected behavior
When encountering error messages returned by the mcp-server tool, the system can continue executing the operation, and the large language model will automatically attempt to fix the issue.

Code that caused the bug
org.springframework.ai
spring-ai-model
In this module, the class DefaultToolCallingManager has a corresponding method called executeToolCall, with the following code:
String toolResult; try { toolResult = toolCallback.call(toolInputArguments, toolContext); } catch (ToolExecutionException ex) { toolResult = toolExecutionExceptionProcessor.process(ex); }
The exception being caught here is ToolExecutionException. However, in another module
org.springframework.ai
spring-ai-mcp
The corresponding class SyncMcpToolCallback contains the following code:
public String call(String functionInput) { Map<String, Object> arguments = ModelOptionsUtils.jsonToMap(functionInput); // Note that we use the original tool name here, not the adapted one from // getToolDefinition CallToolResult response = this.mcpClient.callTool(new CallToolRequest(this.tool.name(), arguments)); if (response.isError() != null && response.isError()) { throw new IllegalStateException("Error calling tool: " + response.content()); } return ModelOptionsUtils.toJsonString(response.content()); }
When an error flag is returned, the exception thrown is IllegalStateException, which cannot be caught by the upper layer that only catches ToolExecutionException, leading to an unexpected interruption.

The suggestion
The lower-level module throws an IllegalStateException, but the upper layer only handles ToolExecutionException. As a result, the exception is unhandled and causes the process to break. Possible solutions include catching IllegalStateException at the upper layer or modifying the lower layer to throw a ToolExecutionException instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions