Skip to content

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

Closed
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1771861337-fix-mcp-delegation-loading
Closed

fix: inject MCP tools during delegation (fixes #4571)#4572
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1771861337-fix-mcp-delegation-loading

Conversation

@devin-ai-integration
Copy link
Contributor

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

When an agent with MCP servers configured is used as a sub-agent via
delegation, its MCP tools were not loaded because the Crew's
_prepare_tools() is not called for the delegated-to agent.

This fix adds _inject_mcp_tools() to agent/utils.py and calls it from
prepare_tools(), which is invoked by both execute_task() and
aexecute_task(). MCP tools are now loaded on-demand when the agent has
mcps configured, with deduplication and graceful error handling.

Also adds 11 tests covering:
- MCP tool injection with/without mcps
- Deduplication of existing tools
- Graceful failure handling
- prepare_tools integration
- Full delegation flow

Co-Authored-By: João <joao@crewai.com>
@devin-ai-integration
Copy link
Contributor Author

Prompt hidden (unlisted session)

@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@devin-ai-integration
Copy link
Contributor Author

Closing due to inactivity for more than 7 days. Configure here.

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

0 participants