@@ -2562,6 +2562,44 @@ def spend() -> str:
25622562 )
25632563
25642564
2565+
2566+ async def test_tool_error_with_content_returns_content_in_error_result ():
2567+ """ToolError with content returns that content in the is_error result."""
2568+ mcp = MCPServer ()
2569+
2570+ @mcp .tool ()
2571+ def analyze () -> str :
2572+ raise ToolError (
2573+ "analysis failed" ,
2574+ content = [TextContent (type = "text" , text = "partial output" ), ImageContent (type = "image" , data = "YWJj" , mime_type = "image/png" )],
2575+ )
2576+
2577+ async with Client (mcp ) as client :
2578+ result = await client .call_tool ("analyze" , {})
2579+
2580+ assert result .is_error is True
2581+ assert len (result .content ) == 2
2582+ assert result .content [0 ] == TextContent (type = "text" , text = "partial output" )
2583+ assert result .content [1 ] == ImageContent (type = "image" , data = "YWJj" , mime_type = "image/png" )
2584+
2585+
2586+ async def test_tool_error_with_image_helper_content_returns_image_in_error_result ():
2587+ """ToolError with Image helper content converts to ImageContent in is_error result."""
2588+ mcp = MCPServer ()
2589+
2590+ @mcp .tool ()
2591+ def screenshot () -> str :
2592+ raise ToolError ("capture failed" , content = [Image (data = b"abc" , format = "png" )])
2593+
2594+ async with Client (mcp ) as client :
2595+ result = await client .call_tool ("screenshot" , {})
2596+
2597+ assert result .is_error is True
2598+ assert len (result .content ) == 1
2599+ assert result .content [0 ].type == "image"
2600+ assert result .content [0 ].mime_type == "image/png"
2601+
2602+
25652603async def test_tool_argument_validation_failure_is_logged_at_info_without_traceback (
25662604 caplog : pytest .LogCaptureFixture ,
25672605):
0 commit comments