Skip to content

fix: prepare MCP tools during execute_task for delegated sub-agents#4616

Open
Anandesh-Sharma wants to merge 1 commit intocrewAIInc:mainfrom
Anandesh-Sharma:fix/mcp-sub-agent-prepare-4571
Open

fix: prepare MCP tools during execute_task for delegated sub-agents#4616
Anandesh-Sharma wants to merge 1 commit intocrewAIInc:mainfrom
Anandesh-Sharma:fix/mcp-sub-agent-prepare-4571

Conversation

@Anandesh-Sharma
Copy link

@Anandesh-Sharma Anandesh-Sharma commented Feb 26, 2026

Summary

  • MCP tools were only loaded during kickoff() and the crew's _prepare_tools() path, but NOT during execute_task() which is the code path used when a main agent delegates work to a sub-agent via DelegateWorkTool
  • Added _ensure_mcp_tools_loaded() method to Agent that idempotently loads MCP tools into the agent's tools list, gated by a _mcps_prepared flag
  • Called at the start of both execute_task() and aexecute_task() so sub-agents with MCP servers receive their tools when delegated to

Root Cause

When DelegateWorkTool._execute() delegates to a sub-agent, it calls selected_agent.execute_task(task, context) directly. The execute_task method never prepared MCP tools -- it relied on either:

  1. kickoff() -> _prepare_kickoff() (standalone agent path)
  2. Crew._prepare_tools() -> _inject_mcp_tools() (crew task execution path)

Neither of these paths run when a sub-agent receives delegated work, so self.mcps was configured but the tools were never resolved and added to self.tools.

Fix Details

  • New _ensure_mcp_tools_loaded() method checks self.mcps and the _mcps_prepared flag
  • If MCP tools haven't been loaded yet, calls get_mcp_tools() and appends to self.tools (with duplicate-name protection)
  • Sets _mcps_prepared = True after loading, making subsequent calls no-ops
  • Also sets the flag in _prepare_kickoff() to prevent double-loading when kickoff is the entry point

Test plan

  • Added test_execute_task_loads_mcp_tools_for_sub_agent -- verifies MCP tools are loaded when execute_task is called directly (the delegation path)
  • Added test_ensure_mcp_tools_loaded_is_idempotent -- verifies no duplicate tools on repeat calls
  • Added test_ensure_mcp_tools_loaded_no_mcps -- verifies no-op when agent has no MCP config
  • All 3 new tests pass

Closes #4571

🤖 Generated with Claude Code


Note

Medium Risk
Changes the agent execution path to dynamically mutate Agent.tools based 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_task and Agent.aexecute_task, via a new idempotent _ensure_mcp_tools_loaded() guarded by a private _mcps_prepared flag.

Also marks MCPs as prepared during _prepare_kickoff() to avoid double-loading, and adds targeted tests covering the direct execute_task delegation 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.

…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>
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Additional Locations (1)

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]When i use mcp at sub agent, it will not load

1 participant