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
Copy file name to clipboardExpand all lines: docs/mcp.md
+66-3Lines changed: 66 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -43,16 +43,24 @@ agent=Agent(
43
43
44
44
## Tool filtering
45
45
46
-
You can filter which tools are available to your Agent using server-level filtering:
46
+
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.
47
+
48
+
### Static tool filtering
49
+
50
+
For simple allow/block lists, you can use static filtering:
excluded_tools=["delete_file"], # This tool will be filtered out
72
+
tool_filter=create_static_tool_filter(
73
+
blocked_tool_names=["delete_file"]
74
+
)
75
+
)
76
+
77
+
```
78
+
79
+
**When both `allowed_tool_names` and `blocked_tool_names` are configured, the processing order is:**
80
+
1. First apply `allowed_tool_names` (allowlist) - only keep the specified tools
81
+
2. Then apply `blocked_tool_names` (blocklist) - exclude specified tools from the remaining tools
82
+
83
+
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.
84
+
85
+
### Dynamic tool filtering
86
+
87
+
For more complex filtering logic, you can use dynamic filters with functions:
tool_filter=custom_filter # or context_aware_filter or async_filter
65
123
)
66
124
```
67
125
126
+
The `ToolFilterContext` provides access to:
127
+
-`run_context`: The current run context
128
+
-`agent`: The agent requesting the tools
129
+
-`server_name`: The name of the MCP server
130
+
68
131
## Caching
69
132
70
133
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