Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/claude_agent_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,16 @@ async def call_tool(name: str, arguments: dict[str, Any]) -> Any:
)
)

# Return just the content list - the decorator wraps it
# Propagate is_error flag by raising, which the MCP decorator
# catches and wraps in CallToolResult(isError=True)
if result.get("is_error", False):
error_text = (
content[0].text
if content and hasattr(content[0], "text")
else "Tool error"
)
raise ValueError(error_text)

return content

# Return SDK server configuration
Expand Down
38 changes: 38 additions & 0 deletions tests/test_sdk_mcp_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,41 @@ async def plain_tool(args: dict[str, Any]) -> dict[str, Any]:

# Tool without annotations should not have the key
assert "annotations" not in tools_by_name["plain_tool"]


@pytest.mark.asyncio
async def test_is_error_flag():
"""Test that is_error flag in tool results is properly propagated."""

@tool("divide", "Divide two numbers", {"a": float, "b": float})
async def divide(args: dict[str, Any]) -> dict[str, Any]:
if args["b"] == 0:
return {
"content": [{"type": "text", "text": "Error: Division by zero"}],
"is_error": True,
}
return {
"content": [{"type": "text", "text": f"Result: {args['a'] / args['b']}"}]
}

server_config = create_sdk_mcp_server(name="error-flag-test", tools=[divide])
server = server_config["instance"]
call_handler = server.request_handlers[CallToolRequest]

# Test error case - is_error flag should be propagated
error_request = CallToolRequest(
method="tools/call",
params=CallToolRequestParams(name="divide", arguments={"a": 1, "b": 0}),
)
result = await call_handler(error_request)
assert result.root.isError is True
assert "Division by zero" in result.root.content[0].text

# Test success case - should not be an error
success_request = CallToolRequest(
method="tools/call",
params=CallToolRequestParams(name="divide", arguments={"a": 10, "b": 2}),
)
result = await call_handler(success_request)
assert result.root.isError is not True
assert "5.0" in result.root.content[0].text