fix(mcp): prevent UnboundLocalError and log warning when native MCP returns no tools#5130
Open
r266-tech wants to merge 1 commit intocrewAIInc:mainfrom
Open
fix(mcp): prevent UnboundLocalError and log warning when native MCP returns no tools#5130r266-tech wants to merge 1 commit intocrewAIInc:mainfrom
r266-tech wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
…eturns no tools Two fixes in MCPToolResolver._resolve_native(): 1. Re-raise RuntimeError when it is not a cancel-scope/task error. Previously, unrecognized RuntimeErrors were silently swallowed, leaving tools_list unassigned and causing UnboundLocalError at the iteration loop (line 412). 2. Log a warning and return early when no tools are discovered, matching the behavior of _resolve_external(). Also log when tool_filter removes all tools. Closes crewAIInc#5116
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.
Closes #5116
Problem
MCPToolResolver._resolve_native()has two issues:UnboundLocalError: When the MCP server connection raises a
RuntimeErrorthat doesn't contain "cancel scope" or "task" in its message, the exception is silently swallowed (not re-raised). This leavestools_listunassigned, causingUnboundLocalError: cannot access local variable 'tools_list' where it is not associated with a valueat the iteration loop (line 412).No warning for empty tools: When the MCP server returns no tools (or
tool_filterremoves all tools), no warning is logged. In contrast,_resolve_external()logs a clear warning:"No tools discovered from MCP server: {server_url}".Fix
Re-raise unrecognized RuntimeError: Added
raiseat the end of theRuntimeErrorhandler so that non-cancel-scope errors propagate instead of being silently swallowed.Log warning and return early: Added checks after getting
tools_listand after tool filtering to log a warning and return([], [])when no tools are available, matching_resolve_external()behavior.Changes
lib/crewai/src/crewai/mcp/tool_resolver.py: +14 linesNote
Low Risk
Low risk: small control-flow and logging changes in MCP native tool discovery; main impact is different error propagation and earlier returns when no tools are available.
Overview
Fixes native MCP tool resolution to avoid silently swallowing unexpected
RuntimeErrors during discovery (preventing anUnboundLocalErrorlater) by re-raising unrecognized runtime errors.Adds explicit empty-results handling: logs a warning and returns
([], [])when the MCP server returns no tools, and whentool_filterremoves all tools, aligning behavior with external MCP resolution.Written by Cursor Bugbot for commit 42cbee5. This will update automatically on new commits. Configure here.