Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
fix(agent): warn when non-empty `apps` resolve to zero platform tools (#6469)
João Mouraba3674616912
diff --git a/lib/crewai/src/crewai/agent/core.py b/lib/crewai/src/crewai/agent/core.pyindex 1ce5f6392..e45eed2b1 100644--- a/lib/crewai/src/crewai/agent/core.py+++ b/lib/crewai/src/crewai/agent/core.py@@ -1360,15 +1360,40 @@ class Agent(BaseAgent):return agent_tools.tools()def get_platform_tools(self, apps: list[PlatformAppOrAction]) -> list[BaseTool]:+ """Resolve platform app selectors into tools.++ Args:+ apps: App or ``app/action`` selectors to resolve.++ Returns:+ The resolved tools, or an empty list when none resolve or resolution+ fails. A non-empty ``apps`` that resolves to no tools emits a+ ``UserWarning`` naming the agent and its apps.+ """+ platform_tools: list[BaseTool] = []try:from crewai_tools import (CrewaiPlatformTools,)- return CrewaiPlatformTools(apps=apps)+ platform_tools = CrewaiPlatformTools(apps=apps)except Exception as e:self._logger.log("error", f"Error getting platform tools: {e!s}")- return []++ if apps and not platform_tools:+ # Logger.log above is verbose-gated; without a warning a bad `apps`+ # entry only shows up later as a task the agent couldn't do.+ app_names = ", ".join(str(app) for app in apps)+ warnings.warn(+ f"Agent '{self.role}' declares apps [{app_names}] but none "+ "resolved to any tools; the agent will run without them and may "+ "be unable to complete its goal. Check that these apps exist and "+ "are connected, and that CREWAI_PLATFORM_INTEGRATION_TOKEN is set.",+ UserWarning,+ stacklevel=2,+ )++ return platform_toolsdef get_mcp_tools(self, mcps: list[str | MCPServerConfig]) -> list[BaseTool]:"""Convert MCP server references/configs to CrewAI tools.diff --git a/lib/crewai/tests/agents/test_agent.py b/lib/crewai/tests/agents/test_agent.pyindex 73408ef11..082646c0a 100644--- a/lib/crewai/tests/agents/test_agent.py+++ b/lib/crewai/tests/agents/test_agent.py@@ -2662,6 +2662,96 @@ def test_agent_without_apps_no_platform_tools():assert tools == []+def test_get_platform_tools_warns_when_apps_resolve_to_zero_tools(monkeypatch):+ monkeypatch.setattr("crewai_tools.CrewaiPlatformTools", lambda apps: [])+ agent = Agent(+ role="Empty Apps Agent",+ goal="Use apps",+ backstory="b",+ apps=["gmail", "slack"],+ )++ with pytest.warns(+ UserWarning,+ # The apps validator dedupes through a set, so either order is possible.+ match=r"Agent 'Empty Apps Agent' declares apps \[(gmail, slack|slack, gmail)\] "+ r"but none resolved to any tools",+ ):+ result = agent.get_platform_tools(agent.apps)++ assert result == []+++def test_get_platform_tools_warns_when_no_action_matches_a_selector(monkeypatch):+ # Runs the real CrewaiPlatformTools factory (it returns a plain list) against an+ # integrations API that knows no action by this name.+ monkeypatch.setenv("CREWAI_PLATFORM_INTEGRATION_TOKEN", "test-token")+ response = MagicMock()+ response.json.return_value = {"actions": {}}+ agent = Agent(+ role="Slack Agent", goal="g", backstory="b", apps=["slack/send-message"]+ )++ with (+ patch(+ "crewai_tools.tools.crewai_platform_tools.integrations_client.requests.get",+ return_value=response,+ ) as get,+ pytest.warns(+ UserWarning,+ match=r"Agent 'Slack Agent' declares apps \[slack/send-message\]",+ ),+ ):+ result = agent.get_platform_tools(agent.apps)++ assert result == []+ assert get.call_args.kwargs["params"] == {"apps": "slack/send-message"}+++def test_get_platform_tools_warns_when_token_is_missing(monkeypatch, capsys):+ monkeypatch.delenv("CREWAI_PLATFORM_INTEGRATION_TOKEN", raising=False)+ agent = Agent(+ role="No Token Agent", goal="g", backstory="b", apps=["gmail"], verbose=True+ )++ with pytest.warns(+ UserWarning, match="CREWAI_PLATFORM_INTEGRATION_TOKEN is set"+ ):+ result = agent.get_platform_tools(agent.apps)++ assert result == []+ assert "No Platform integration token found" in capsys.readouterr().out+++def test_get_platform_tools_quiet_when_tools_resolve(monkeypatch):+ @tool+ def platform_tool() -> str:+ """A resolved platform tool."""+ return "ok"++ monkeypatch.setattr("crewai_tools.CrewaiPlatformTools", lambda apps: [platform_tool])+ agent = Agent(role="A", goal="g", backstory="b", apps=["gmail"])++ with warnings.catch_warnings(record=True) as caught:+ warnings.simplefilter("always")+ result = agent.get_platform_tools(agent.apps)++ assert result == [platform_tool]+ assert not any("resolved to any tools" in str(w.message) for w in caught)+++def test_crew_prepare_tools_warns_on_empty_apps(monkeypatch):+ monkeypatch.setattr("crewai_tools.CrewaiPlatformTools", lambda apps: [])+ agent = Agent(role="Crew Agent", goal="g", backstory="b", apps=["gmail"])+ task = Task(description="d", expected_output="o", agent=agent)+ crew = Crew(agents=[agent], tasks=[task])++ with pytest.warns(+ UserWarning, match=r"Agent 'Crew Agent' declares apps \[gmail\]"+ ):+ crew._prepare_tools(agent, task, [])++def test_agent_mcps_accepts_slug_with_specific_tool():"""Agent(mcps=["notion#get_page"]) must pass validation (_SLUG_RE)."""agent = Agent(