fix: inject MCP tools during delegation (fixes #4571)#4572
Closed
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
Closed
fix: inject MCP tools during delegation (fixes #4571)#4572devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
Conversation
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>
Contributor
Author
|
Prompt hidden (unlisted session) |
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Contributor
Author
|
Closing due to inactivity for more than 7 days. Configure here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: inject MCP tools during delegation (fixes #4571)
Summary
When an agent with MCP servers is invoked via delegation (
DelegateWorkTool→agent.execute_task()), its MCP tools were never loaded becauseCrew._prepare_tools()only runs for agents during the normalcrew.kickoff()flow — not for the delegated-to agent.The fix adds a
_inject_mcp_tools()helper inagent/utils.pythat is called fromprepare_tools(). Sinceprepare_tools()is used by bothexecute_task()andaexecute_task(), MCP tools are now loaded on-demand whenever the agent hasmcpsconfigured, 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(), modifiedprepare_tools()to call itlib/crewai/tests/mcp/test_mcp_delegation.py— 11 new testsReview & Testing Checklist for Human
Crew._prepare_tools()already callsagent.get_mcp_tools(). Nowprepare_tools()also calls it via_inject_mcp_tools(). The dedup prevents duplicate tools, butget_mcp_tools()still fires twice (potentially creating extra MCP client connections). Verify this doesn't cause performance regressions or MCP client state issues.Agent.get_mcp_toolsat the class level. Manually verify with an actual MCP server that delegation correctly surfaces MCP tools to the sub-agent.except Exceptionin_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