From b63a8f738c9689c29f5a92f09fc7f9131067cd81 Mon Sep 17 00:00:00 2001 From: Lucas Gomide Date: Tue, 17 Jun 2025 18:22:48 -0300 Subject: [PATCH] feat: support to initialize a tool from defined Tool attributes --- src/crewai/utilities/agent_utils.py | 2 +- tests/agent_test.py | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/crewai/utilities/agent_utils.py b/src/crewai/utilities/agent_utils.py index be0abd92c..0c2a28683 100644 --- a/src/crewai/utilities/agent_utils.py +++ b/src/crewai/utilities/agent_utils.py @@ -476,7 +476,7 @@ def load_agent_from_repository(from_repository: str) -> Dict[str, Any]: try: module = importlib.import_module(tool["module"]) tool_class = getattr(module, tool["name"]) - attributes[key].append(tool_class()) + attributes[key].append(tool_class(**tool["init_params"])) except Exception as e: raise AgentRepositoryError( f"Tool {tool['name']} could not be loaded: {e}" diff --git a/tests/agent_test.py b/tests/agent_test.py index 8af97c77f..33361992a 100644 --- a/tests/agent_test.py +++ b/tests/agent_test.py @@ -2099,7 +2099,7 @@ def mock_get_auth_token(): @patch("crewai.cli.plus_api.PlusAPI.get_agent") def test_agent_from_repository(mock_get_agent, mock_get_auth_token): - from crewai_tools import SerperDevTool, XMLSearchTool + from crewai_tools import SerperDevTool, XMLSearchTool, CSVSearchTool mock_get_response = MagicMock() mock_get_response.status_code = 200 @@ -2108,8 +2108,9 @@ def test_agent_from_repository(mock_get_agent, mock_get_auth_token): "goal": "test goal", "backstory": "test backstory", "tools": [ - {"module": "crewai_tools", "name": "SerperDevTool"}, - {"module": "crewai_tools", "name": "XMLSearchTool"}, + {"module": "crewai_tools", "name": "SerperDevTool", "init_params": {"n_results": 30}}, + {"module": "crewai_tools", "name": "XMLSearchTool", "init_params": {"summarize": True}}, + {"module": "crewai_tools", "name": "CSVSearchTool", "init_params": {}}, ], } mock_get_agent.return_value = mock_get_response @@ -2118,9 +2119,14 @@ def test_agent_from_repository(mock_get_agent, mock_get_auth_token): assert agent.role == "test role" assert agent.goal == "test goal" assert agent.backstory == "test backstory" - assert len(agent.tools) == 2 + assert len(agent.tools) == 3 assert isinstance(agent.tools[0], SerperDevTool) + assert agent.tools[0].n_results == 30 assert isinstance(agent.tools[1], XMLSearchTool) + assert agent.tools[1].summarize + + assert isinstance(agent.tools[2], CSVSearchTool) + assert not agent.tools[2].summarize @patch("crewai.cli.plus_api.PlusAPI.get_agent") @@ -2133,7 +2139,7 @@ def test_agent_from_repository_override_attributes(mock_get_agent, mock_get_auth "role": "test role", "goal": "test goal", "backstory": "test backstory", - "tools": [{"name": "SerperDevTool", "module": "crewai_tools"}], + "tools": [{"name": "SerperDevTool", "module": "crewai_tools", "init_params": {}}], } mock_get_agent.return_value = mock_get_response agent = Agent(from_repository="test_agent", role="Custom Role")