-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: add MCP tool filtering support #861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Hi @rm-openai! 👋 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this. Would however prefer an interface that looked something like this:
ToolFilterCallable = Callable[[ToolFilterContext, Tool], MaybeAwaitable[bool]]
class ToolFilterStatic(TypedDict):
allowed_tool_names: NotRequired[list[str]]
blocked_tool_names: NotRequired[list[str]]
and then the MCPServer class would take tool_filter: ToolFilterCallable | ToolFilterStatic | None
thoughts?
@rm-openai Thanks for your suggestions! |
a873952
to
3307f96
Compare
Hi @rm-openai! 👋 Thank you for the detailed feedback! I've completely redesigned the implementation following your suggested interface pattern. 🔄 Major Changes in This Update:1. Implemented Your Suggested Interface:
2. Enhanced Dynamic Filtering Support: # Context-aware filtering
def context_filter(context: ToolFilterContext, tool) -> bool:
# Access agent information
agent_name = context.agent.name
# Access server information
server_name = context.server_name
# Custom logic based on runtime context
return some_dynamic_logic(agent_name, server_name, tool)
# Async filtering for complex operations
async def async_filter(context: ToolFilterContext, tool) -> bool:
result = await some_async_check(context, tool)
return result
server = MCPServerStdio(
params={"command": "npx", "args": [...]},
tool_filter=context_filter # or async_filter
) 3. Simplified Static Filtering: from agents.mcp import create_static_tool_filter
server = MCPServerStdio(
tool_filter=create_static_tool_filter(
allowed_tool_names=["read_file", "write_file"],
blocked_tool_names=["delete_file"]
)
) 4. Architecture Improvements:
Would you be able to take another look when you have a moment? Thanks again for the great input! |
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:
Solution
Implements a two-level filtering system:
Server-level filtering:
Agent-level filtering:
Features
allowed_tools
/excluded_tools
parameters for all MCP server typesmcp_config
Related Issues
#376, #851, #830, #863
Testing
All existing tests pass + 8 new test cases covering various filtering scenarios.