Skip to content

Fix allowed_tools filtering to correctly handle empty list - #2

Closed
chetantoshniwal wants to merge 1 commit into
mainfrom
fix/mcp-allowed-tools-empty-list-handling
Closed

chetantoshniwal wants to merge 1 commit into
mainfrom
fix/mcp-allowed-tools-empty-list-handling

Conversation

@chetantoshniwal

@chetantoshniwal chetantoshniwal commented May 25, 2026

Copy link
Copy Markdown
Owner

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.functions property 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 to None, 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 None checks across 4 files:

  • python/packages/core/agent_framework/_mcp.py — core filtering logic
  • python/packages/anthropic/agent_framework_anthropic/_chat_client.py — MCP config serialization
  • python/packages/foundry/agent_framework_foundry/_chat_client.py — MCP config serialization
  • python/packages/openai/agent_framework_openai/_chat_client.py — MCP config serialization

Now:

  • 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() cast for consistency with arbitrary Collection inputs.


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>
@chetantoshniwal chetantoshniwal changed the title [Generated by SRE Agent] Fix allowed_tools filtering to correctly handle empty list Fix allowed_tools filtering to correctly handle empty list May 25, 2026
@chetantoshniwal
chetantoshniwal deleted the fix/mcp-allowed-tools-empty-list-handling branch May 25, 2026 05:39
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.

1 participant