Skip to content

Commit 127f700

Browse files
Fj-ivytzolov
andcommitted
fix: Improve error handling in MCP tool callbacks
- Add null check for isError() method in SyncMcpToolCallback - Implement consistent error handling in AsyncMcpToolCallback to match SyncMcpToolCallback behavior - Throw IllegalStateException with error content when tool calls fail Resolves #2447 Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com> Co-authored-by: Christian Tzolov <christian.tzolov@broadcom.com>
1 parent e4853d7 commit 127f700

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

mcp/common/src/main/java/org/springframework/ai/mcp/AsyncMcpToolCallback.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,12 @@ public String call(String functionInput) {
109109
Map<String, Object> arguments = ModelOptionsUtils.jsonToMap(functionInput);
110110
// Note that we use the original tool name here, not the adapted one from
111111
// getToolDefinition
112-
return this.asyncMcpClient.callTool(new CallToolRequest(this.tool.name(), arguments))
113-
.map(response -> ModelOptionsUtils.toJsonString(response.content()))
114-
.block();
112+
return this.asyncMcpClient.callTool(new CallToolRequest(this.tool.name(), arguments)).map(response -> {
113+
if (response.isError() != null && response.isError()) {
114+
throw new IllegalStateException("Error calling tool: " + response.content());
115+
}
116+
return ModelOptionsUtils.toJsonString(response.content());
117+
}).block();
115118
}
116119

117120
@Override

mcp/common/src/main/java/org/springframework/ai/mcp/SyncMcpToolCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public String call(String functionInput) {
113113
// Note that we use the original tool name here, not the adapted one from
114114
// getToolDefinition
115115
CallToolResult response = this.mcpClient.callTool(new CallToolRequest(this.tool.name(), arguments));
116-
if (response.isError()) {
116+
if (response.isError() != null && response.isError()) {
117117
throw new IllegalStateException("Error calling tool: " + response.content());
118118
}
119119
return ModelOptionsUtils.toJsonString(response.content());

0 commit comments

Comments
 (0)