crewai - ✅(Solved) Fix [BUG]When i use mcp at sub agent, it will not load [3 pull requests, 4 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
crewAIInc/crewAI#4571Fetched 2026-04-08 00:41:12
View on GitHub
Comments
4
Participants
3
Timeline
13
Reactions
0
Author
Timeline (top)
commented ×4cross-referenced ×3referenced ×3labeled ×2

When i use mcp at sub agent, it will not load.

Root Cause

When i use mcp at sub agent, it will not load.

Fix Action

Fixed

PR fix notes

PR #4572: fix: inject MCP tools during delegation (fixes #4571)

Description (problem / solution / changelog)

fix: inject MCP tools during delegation (fixes #4571)

Summary

When an agent with MCP servers is invoked via delegation (DelegateWorkToolagent.execute_task()), its MCP tools were never loaded because Crew._prepare_tools() only runs for agents during the normal crew.kickoff() flow — not for the delegated-to agent.

The fix adds a _inject_mcp_tools() helper in agent/utils.py that is called from prepare_tools(). Since prepare_tools() is used by both execute_task() and aexecute_task(), MCP tools are now loaded on-demand whenever the agent has mcps configured, with name-based deduplication and a try/except to avoid breaking task execution if MCP loading fails.

Changed files:

  • lib/crewai/src/crewai/agent/utils.py — added _inject_mcp_tools(), modified prepare_tools() to call it
  • lib/crewai/tests/mcp/test_mcp_delegation.py — 11 new tests

Review & Testing Checklist for Human

  • Double MCP loading in normal (non-delegation) path: In the standard Crew flow, Crew._prepare_tools() already calls agent.get_mcp_tools(). Now prepare_tools() also calls it via _inject_mcp_tools(). The dedup prevents duplicate tools, but get_mcp_tools() still fires twice (potentially creating extra MCP client connections). Verify this doesn't cause performance regressions or MCP client state issues.
  • End-to-end delegation test with real MCP server: All tests mock Agent.get_mcp_tools at the class level. Manually verify with an actual MCP server that delegation correctly surfaces MCP tools to the sub-agent.
  • Broad except Exception in _inject_mcp_tools: This silently swallows all errors during MCP tool loading. Consider whether specific exceptions should propagate (e.g., configuration errors vs. transient connection failures).

Notes

Changed files

  • lib/crewai/src/crewai/agent/utils.py (modified, +47/-1)
  • lib/crewai/tests/mcp/test_mcp_delegation.py (added, +225/-0)

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

Description (problem / solution / changelog)

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

<!-- CURSOR_SUMMARY -->

[!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.

<sup>Written by Cursor Bugbot for commit 5cdef51401b5bc9a6dacca0db9d253d505693858. This will update automatically on new commits. Configure here.</sup>

<!-- /CURSOR_SUMMARY -->

Changed files

  • lib/crewai/src/crewai/agent/core.py (modified, +36/-0)
  • lib/crewai/tests/agents/test_agent.py (modified, +100/-0)

PR #4625: fix: prepare MCP tools in execute_task for delegated agents

Description (problem / solution / changelog)

Summary

  • Fixes MCP tools not being available when a sub-agent receives a delegated task
  • MCP tools are only resolved during the kickoff preparation path, but execute_task() (used for delegation) never triggers MCP resolution
  • Adds MCP tool resolution at the start of execute_task() when the resolver hasn't been initialized yet, ensuring delegated agents have access to their MCP tools

Fixes #4571

Test plan

  • Create a main agent with delegation and a sub-agent with MCP servers
  • Verify the sub-agent can use MCP tools when receiving delegated tasks
  • Verify agents that go through normal kickoff still work correctly (idempotent check)
  • Verify agents without MCP servers are unaffected

🤖 Generated with Claude Code

Changed files

  • lib/crewai/src/crewai/agent/core.py (modified, +9/-0)
RAW_BUFFERClick to expand / collapse

Description

When i use mcp at sub agent, it will not load.

Steps to Reproduce

  1. main_agent=Agent(allow_delegation=True,)
  2. sub_agent = Agent(mcps=[MCPServerHTTP(url="http://localhost:8022/mcp", )])
  3. task = Task(agent=main_agent)
  4. Crew( agents=[main_agent, sub_agent], tasks=[ task], process=Process.sequential, verbose=True, )

then mcps will not load when delegation task to sub_agent

Expected behavior

sub_agent can load mcps

Screenshots/Code snippets

no

Operating System

Ubuntu 20.04

Python Version

3.10

crewAI Version

1.7.2

crewAI Tools Version

1.7.2

Virtual Environment

Venv

Evidence

in source code , mcps will prepare when kick_off. when agent cowork, it will use agent.execute_task, but execute_task will not prepare mcps.

Possible Solution

prepare mcps when agent.execute_task

Additional context

no

extent analysis

Fix Plan

To fix the issue, we need to prepare MCPS when agent.execute_task is called. Here are the steps:

  • Modify the execute_task method in the Agent class to prepare MCPS before executing the task.
  • Add a check to ensure that MCPS is prepared only when the agent is a sub-agent.

Example Code

class Agent:
    # ...

    def execute_task(self, task):
        if self.mcps and not task.mcps_prepared:
            # Prepare MCPS
            for mcp in self.mcps:
                mcp.prepare()
            task.mcps_prepared = True
        # Execute the task
        # ...

Alternatively, you can also modify the Task class to prepare MCPS when it is executed:

class Task:
    # ...

    def execute(self, agent):
        if agent.mcps and not self.mcps_prepared:
            # Prepare MCPS
            for mcp in agent.mcps:
                mcp.prepare()
            self.mcps_prepared = True
        # Execute the task
        # ...

Verification

To verify that the fix worked, you can add a print statement or a log message to check if MCPS is being prepared when agent.execute_task is called. You can also check the task execution output to ensure that MCPS is loaded correctly.

Extra Tips

  • Make sure to test the fix in a development environment before deploying it to production.
  • Consider adding a flag or a configuration option to enable or disable MCPS preparation for sub-agents.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

sub_agent can load mcps

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING