Skip to content

fix(mcp): prevent UnboundLocalError and log warning when native MCP returns no tools#5130

Open
r266-tech wants to merge 1 commit intocrewAIInc:mainfrom
r266-tech:fix/mcp-tool-resolver-warning
Open

fix(mcp): prevent UnboundLocalError and log warning when native MCP returns no tools#5130
r266-tech wants to merge 1 commit intocrewAIInc:mainfrom
r266-tech:fix/mcp-tool-resolver-warning

Conversation

@r266-tech
Copy link
Copy Markdown

@r266-tech r266-tech commented Mar 26, 2026

Closes #5116

Problem

MCPToolResolver._resolve_native() has two issues:

  1. UnboundLocalError: When the MCP server connection raises a RuntimeError that doesn't contain "cancel scope" or "task" in its message, the exception is silently swallowed (not re-raised). This leaves tools_list unassigned, causing UnboundLocalError: cannot access local variable 'tools_list' where it is not associated with a value at the iteration loop (line 412).

  2. No warning for empty tools: When the MCP server returns no tools (or tool_filter removes all tools), no warning is logged. In contrast, _resolve_external() logs a clear warning: "No tools discovered from MCP server: {server_url}".

Fix

  1. Re-raise unrecognized RuntimeError: Added raise at the end of the RuntimeError handler so that non-cancel-scope errors propagate instead of being silently swallowed.

  2. Log warning and return early: Added checks after getting tools_list and 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 lines

Note

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 an UnboundLocalError later) by re-raising unrecognized runtime errors.

Adds explicit empty-results handling: logs a warning and returns ([], []) when the MCP server returns no tools, and when tool_filter removes all tools, aligning behavior with external MCP resolution.

Written by Cursor Bugbot for commit 42cbee5. This will update automatically on new commits. Configure here.

…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
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] MCPToolResolver does not log warning when native MCP config returns no tools

2 participants