fix: prepare MCP tools during execute_task for delegated sub-agents#4616
fix: prepare MCP tools during execute_task for delegated sub-agents#4616Anandesh-Sharma wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
…rewAIInc#4571) MCP tools were only prepared during kickoff() and the crew's _prepare_tools() path. When a main agent delegates to a sub-agent via DelegateWorkTool, execute_task() is called directly on the sub-agent, bypassing both preparation paths. This caused MCP tools to never be loaded for sub-agents. Add _ensure_mcp_tools_loaded() which idempotently loads MCP tools into the agent's tools list, gated by a _mcps_prepared flag. Call it at the start of both execute_task() and aexecute_task() so that delegation works correctly. Also set the flag in _prepare_kickoff() to prevent double-loading when kickoff is the entry point. Closes crewAIInc#4571 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Free Tier Details
Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.
To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| for tool in mcp_tools: | ||
| if getattr(tool, "name", None) not in existing_names: | ||
| self.tools.append(tool) | ||
| self._mcps_prepared = True |
There was a problem hiding this comment.
Crew path causes duplicate MCP server connections
Medium Severity
In the standard crew execution path, crew._prepare_tools() → _inject_mcp_tools() already calls agent.get_mcp_tools(), but it never sets _mcps_prepared on the agent. So when execute_task subsequently runs _ensure_mcp_tools_loaded(), it sees _mcps_prepared is still False and calls get_mcp_tools() a second time. For native MCP configs (MCPServerStdio, MCPServerHTTP, MCPServerSSE), this creates duplicate server connections and appends duplicate clients to _mcp_clients. The redundant tools added to self.tools aren't even used because prepare_tools() prioritizes the passed tools parameter.


Summary
kickoff()and the crew's_prepare_tools()path, but NOT duringexecute_task()which is the code path used when a main agent delegates work to a sub-agent viaDelegateWorkTool_ensure_mcp_tools_loaded()method toAgentthat idempotently loads MCP tools into the agent's tools list, gated by a_mcps_preparedflagexecute_task()andaexecute_task()so sub-agents with MCP servers receive their tools when delegated toRoot Cause
When
DelegateWorkTool._execute()delegates to a sub-agent, it callsselected_agent.execute_task(task, context)directly. Theexecute_taskmethod never prepared MCP tools -- it relied on either:kickoff()->_prepare_kickoff()(standalone agent path)Crew._prepare_tools()->_inject_mcp_tools()(crew task execution path)Neither of these paths run when a sub-agent receives delegated work, so
self.mcpswas configured but the tools were never resolved and added toself.tools.Fix Details
_ensure_mcp_tools_loaded()method checksself.mcpsand the_mcps_preparedflagget_mcp_tools()and appends toself.tools(with duplicate-name protection)_mcps_prepared = Trueafter loading, making subsequent calls no-ops_prepare_kickoff()to prevent double-loading when kickoff is the entry pointTest plan
test_execute_task_loads_mcp_tools_for_sub_agent-- verifies MCP tools are loaded whenexecute_taskis called directly (the delegation path)test_ensure_mcp_tools_loaded_is_idempotent-- verifies no duplicate tools on repeat callstest_ensure_mcp_tools_loaded_no_mcps-- verifies no-op when agent has no MCP configCloses #4571
🤖 Generated with Claude Code
Note
Medium Risk
Changes the agent execution path to dynamically mutate
Agent.toolsbased on MCP configuration, which can affect tool availability and ordering during runtime. Scope is limited and covered by new regression/idempotency tests.Overview
Fixes delegated sub-agent execution by loading MCP-provided tools at the start of
Agent.execute_taskandAgent.aexecute_task, via a new idempotent_ensure_mcp_tools_loaded()guarded by a private_mcps_preparedflag.Also marks MCPs as prepared during
_prepare_kickoff()to avoid double-loading, and adds targeted tests covering the directexecute_taskdelegation path, idempotency (no duplicate tools), and the no-MCP no-op case.Written by Cursor Bugbot for commit 5cdef51. This will update automatically on new commits. Configure here.