diff --git a/python/src/agent_squad/utils/tool.py b/python/src/agent_squad/utils/tool.py index 46e9e670..c66df508 100644 --- a/python/src/agent_squad/utils/tool.py +++ b/python/src/agent_squad/utils/tool.py @@ -304,11 +304,13 @@ def _get_tool_use_block( return None async def _process_tool(self, tool_name, input_data): + tool = next((tool for tool in self.tools if tool.name == tool_name), None) + if tool is None: + return f"Tool '{tool_name}' not found" try: - tool = next(tool for tool in self.tools if tool.name == tool_name) return await tool.func(**input_data) - except StopIteration: - return f"Tool '{tool_name}' not found" + except Exception as e: + return f"Error processing tool '{tool_name}': {e}" def to_claude_format(self) -> list[dict[str, Any]]: """Convert all tools to Claude format""" diff --git a/python/src/tests/utils/test_tool.py b/python/src/tests/utils/test_tool.py index c0f57be6..0ba1e5c1 100644 --- a/python/src/tests/utils/test_tool.py +++ b/python/src/tests/utils/test_tool.py @@ -473,14 +473,25 @@ def test_tool_with_properties(): @pytest.mark.asyncio async def test_tool_not_found(): - try: - tools = AgentTools([AgentTool( - name="weather", - func=fetch_weather_data - )]) - await tools._process_tool("test", {'test':'value'}) - except Exception as e: - assert str(e) == f"Tool weather not found" + tools = AgentTools([AgentTool( + name="weather", + func=fetch_weather_data + )]) + result = await tools._process_tool("test", {'test': 'value'}) + assert result == "Tool 'test' not found" + + +@pytest.mark.asyncio +async def test_tool_processing_error(): + async def failing_tool(value: str): + raise ValueError("boom") + + tools = AgentTools([AgentTool( + name="failing", + func=failing_tool + )]) + result = await tools._process_tool("failing", {'value': 'x'}) + assert result == "Error processing tool 'failing': boom" def test_get_tool_use_block():