You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Add MCP Tool Filtering Support
This PR implements tool filtering capabilities for MCP servers,
addressing multiple community requests for this feature.
### Problem
Currently, Agent SDK automatically fetches all available tools from MCP
servers without the ability to select specific tools. This creates
several issues:
- Unwanted tools occupy LLM context and affect tool selection accuracy
- Security concerns when limiting tool access scope
- Tool name conflicts between multiple servers
- Need for different tool subsets across different agents
### Solution
Implements a two-level filtering system:
**Server-level filtering:**
```python
server = MCPServerStdio(
params={"command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path"]},
allowed_tools=["read_file", "write_file"], # whitelist
excluded_tools=["delete_file"] # blacklist
)
```
**Agent-level filtering:**
```python
agent = Agent(
name="Assistant",
mcp_servers=[server1, server2],
mcp_config={
"allowed_tools": {"server1": ["read_file", "write_file"]},
"excluded_tools": {"server2": ["dangerous_tool"]}
}
)
```
### Features
- ✅ Server-level `allowed_tools`/`excluded_tools` parameters for all MCP
server types
- ✅ Agent-level filtering via `mcp_config`
- ✅ Hierarchical filtering (server-level first, then agent-level)
- ✅ Comprehensive test coverage (8 test cases)
- ✅ Updated documentation with examples
### Related Issues
#376, #851, #830, #863
### Testing
All existing tests pass + 8 new test cases covering various filtering
scenarios.
You can filter which tools are available to your Agent by configuring tool filters on MCP servers. The SDK supports both static and dynamic tool filtering.
54
+
55
+
### Static tool filtering
56
+
57
+
For simple allow/block lists, you can use static filtering:
**When both `allowed_tool_names` and `blocked_tool_names` are configured, the processing order is:**
87
+
1. First apply `allowed_tool_names` (allowlist) - only keep the specified tools
88
+
2. Then apply `blocked_tool_names` (blocklist) - exclude specified tools from the remaining tools
89
+
90
+
For example, if you configure `allowed_tool_names=["read_file", "write_file", "delete_file"]` and `blocked_tool_names=["delete_file"]`, only `read_file` and `write_file` tools will be available.
91
+
92
+
### Dynamic tool filtering
93
+
94
+
For more complex filtering logic, you can use dynamic filters with functions:
tool_filter=custom_filter # or context_aware_filter or async_filter
130
+
)
131
+
```
132
+
133
+
The `ToolFilterContext` provides access to:
134
+
-`run_context`: The current run context
135
+
-`agent`: The agent requesting the tools
136
+
-`server_name`: The name of the MCP server
137
+
44
138
## Caching
45
139
46
140
Every time an Agent runs, it calls `list_tools()` on the MCP server. This can be a latency hit, especially if the server is a remote server. To automatically cache the list of tools, you can pass `cache_tools_list=True` to [`MCPServerStdio`][agents.mcp.server.MCPServerStdio], [`MCPServerSse`][agents.mcp.server.MCPServerSse], and [`MCPServerStreamableHttp`][agents.mcp.server.MCPServerStreamableHttp]. You should only do this if you're certain the tool list will not change.
0 commit comments