Fix allowed_tools filtering to correctly handle empty list - #2
Closed
chetantoshniwal wants to merge 1 commit into
Closed
chetantoshniwal wants to merge 1 commit into
chetantoshniwal wants to merge 1 commit into
Conversation
The allowed_tools parameter in MCPTool.functions and MCP server config helpers used a falsy check (if not allowed_tools / if allowed_tools) which treats an empty list identically to None. This meant passing allowed_tools=[] would bypass filtering and return all tools, rather than returning no tools as expected. Changed to explicit None checks so that: - allowed_tools=None -> no filtering (return all tools) - allowed_tools=[] -> explicit empty allowlist (return no tools) - allowed_tools=[...] -> filter to listed names only Also normalized serialization to use list() for consistency with arbitrary Collection inputs. Co-authored-by: Azure SRE Agent <noreply@microsoft.com>
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.
Summary
Fixes incorrect behavior where passing
allowed_tools=[]to MCP tool constructors or chat client helpers would expose all tools instead of no tools.Problem
The
MCPTool.functionsproperty and MCP server config helper methods used Python falsy checks (if not self.allowed_tools/if allowed_tools) to determine whether filtering should be applied. Since an empty list[]is falsy in Python, it was treated identically toNone, causing the filter to be skipped entirely.This meant:
allowed_tools=None→ all tools (correct, documented behavior)allowed_tools=["tool_a"]→ only tool_a (correct)allowed_tools=[]→ all tools exposed (incorrect — should return no tools)Fix
Changed to explicit
Nonechecks across 4 files:python/packages/core/agent_framework/_mcp.py— core filtering logicpython/packages/anthropic/agent_framework_anthropic/_chat_client.py— MCP config serializationpython/packages/foundry/agent_framework_foundry/_chat_client.py— MCP config serializationpython/packages/openai/agent_framework_openai/_chat_client.py— MCP config serializationNow:
allowed_tools=None→ no filtering (return all tools)allowed_tools=[]→ explicit empty allowlist (return no tools)allowed_tools=[...]→ filter to listed names onlyAlso normalized serialization to use
list()cast for consistency with arbitraryCollectioninputs.