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>
This commit is contained in:
@@ -109,9 +109,12 @@ public class AsyncMcpToolCallback implements ToolCallback {
|
||||
Map<String, Object> arguments = ModelOptionsUtils.jsonToMap(functionInput);
|
||||
// Note that we use the original tool name here, not the adapted one from
|
||||
// getToolDefinition
|
||||
return this.asyncMcpClient.callTool(new CallToolRequest(this.tool.name(), arguments))
|
||||
.map(response -> ModelOptionsUtils.toJsonString(response.content()))
|
||||
.block();
|
||||
return this.asyncMcpClient.callTool(new CallToolRequest(this.tool.name(), arguments)).map(response -> {
|
||||
if (response.isError() != null && response.isError()) {
|
||||
throw new IllegalStateException("Error calling tool: " + response.content());
|
||||
}
|
||||
return ModelOptionsUtils.toJsonString(response.content());
|
||||
}).block();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -113,7 +113,7 @@ public class SyncMcpToolCallback implements ToolCallback {
|
||||
// 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()) {
|
||||
if (response.isError() != null && response.isError()) {
|
||||
throw new IllegalStateException("Error calling tool: " + response.content());
|
||||
}
|
||||
return ModelOptionsUtils.toJsonString(response.content());
|
||||
|
||||
Reference in New Issue
Block a user