Skip to content

Commit 37b1ab3

Browse files
AkshayShah03copybara-github
authored andcommitted
fix: mark failed MCP tool results as errors for Claude
Merge #7665 AnthropicLlm now sends a tool_result with is_error set when the function response carries a truthy isError or is_error key, as a failed MCP CallToolResult does; it previously always sent it as false. Fixes #7664 PiperOrigin-RevId: 1005107381
1 parent 9833835 commit 37b1ab3

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎src/google/adk/models/anthropic_llm.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,12 @@ def _part_to_message_block(
621621
else:
622622
tool_result_content = content
623623

624+
# An MCP CallToolResult reports a failed call with `isError`, spelled
625+
# `is_error` in dumps made with MCP SDK 2.x field names.
626+
is_error = bool(
627+
response_data.get("isError") or response_data.get("is_error")
628+
)
629+
624630
return anthropic_types.ToolResultBlockParam(
625631
tool_use_id=sanitizer.sanitize(
626632
function_response.id,
@@ -629,7 +635,7 @@ def _part_to_message_block(
629635
),
630636
type="tool_result",
631637
content=tool_result_content,
632-
is_error=False,
638+
is_error=is_error,
633639
)
634640
elif _is_image_part(part):
635641
inline_data = part.inline_data

‎tests/unittests/models/test_anthropic_llm.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,24 @@ def test_part_to_message_block_with_multiple_content_items():
13791379
assert result["content"] == "First part\nSecond part"
13801380

13811381

1382+
@pytest.mark.parametrize("error_key", ["isError", "is_error"])
1383+
def test_part_to_message_block_marks_mcp_error_result_as_error(error_key):
1384+
"""An MCP CallToolResult with isError reaches Claude as an error result."""
1385+
response_part = types.Part.from_function_response(
1386+
name="mcp_tool",
1387+
response={
1388+
"content": [{"type": "text", "text": "file not found"}],
1389+
error_key: True,
1390+
},
1391+
)
1392+
response_part.function_response.id = "test_id_mcp_error"
1393+
1394+
result = part_to_message_block(response_part)
1395+
1396+
assert result["content"] == "file not found"
1397+
assert result["is_error"] is True
1398+
1399+
13821400
def test_part_to_message_block_tool_result_with_image():
13831401
"""Media a tool attached to its response reaches Claude as an image block."""
13841402
image_data = b"chart-bytes"

0 commit comments

Comments
 (0)