@@ -383,18 +383,34 @@ async def async_tool(x: int, ctx: Context) -> str:
383383 assert result == "42"
384384
385385 @pytest .mark .anyio
386- async def test_context_error_handling (self ):
387- """Test error handling when context injection fails ."""
386+ async def test_unexpected_error_hides_internal_details (self ):
387+ """Test error handling does not expose unexpected exception details ."""
388388
389389 def tool_with_context (x : int , ctx : Context ) -> str :
390- raise ValueError ("Test error " )
390+ raise ValueError ("secret token leaked " )
391391
392392 manager = ToolManager ()
393393 manager .add_tool (tool_with_context )
394394
395- with pytest .raises (ToolError , match = "Error executing tool tool_with_context" ) :
395+ with pytest .raises (ToolError ) as exc_info :
396396 await manager .call_tool ("tool_with_context" , {"x" : 42 }, context = Context ())
397397
398+ assert str (exc_info .value ) == "Error executing tool tool_with_context: unexpected internal error"
399+ assert "secret token leaked" not in str (exc_info .value )
400+
401+ @pytest .mark .anyio
402+ async def test_tool_error_preserves_message (self ):
403+ """Test explicit ToolError messages remain visible to clients."""
404+
405+ def tool_with_expected_error () -> str :
406+ raise ToolError ("safe client-facing error" )
407+
408+ manager = ToolManager ()
409+ manager .add_tool (tool_with_expected_error )
410+
411+ with pytest .raises (ToolError , match = "safe client-facing error" ):
412+ await manager .call_tool ("tool_with_expected_error" , {}, context = Context ())
413+
398414
399415class TestToolAnnotations :
400416 def test_tool_annotations (self ):
0 commit comments