# Practical and Useful Patterns for OpenAI Agent SDK
🌍 Don't expose all MCP tools blindly -- use filters, prompts, and caching for smart control. From permission scoping to dynamic filtering, here are patterns for safe and fast MCP usage.
Minimize permissions with tool filters, fetch dynamic instructions via prompts, and reduce latency with caching -- three features that take MCP operations to production grade.
📌 Title: MCP -- Tool filtering / Prompts / Caching
🔗 URL:
🧩 Overview
Passing all MCP server tools directly to an agent risks unnecessary tool calls and permission overreach. Use `create_static_tool_filter` to statically restrict allowed tools, or dynamic filter functions to conditionally expose tools based on runtime context (role, tenant, etc.). Fetch dynamic instructions from MCP servers with `get_prompt`, improve latency by caching the tool list with `cache_tools_list=True`, and clear the cache with `invalidate_tools_cache()` when tools change.
🛠 How to Use
Import `MCPServerStreamableHttp` and `create_static_tool_filter` from `agents.mcp`. Pass `create_static_tool_filter(allowed_tool_names=["read_file", "write_file"])` to the `tool_filter` parameter of `MCPServerStreamableHttp` to restrict the agent to only `read_file` and `write_file` tools.
🏗 Practical Usage Patterns
**Permission Scoping with create_static_tool_filter**
Restrict available tools to the minimum required for each agent's role.
For a read-only agent, create a filter with `create_static_tool_filter(allowed_tool_names=["read_file", "search_docs", "list_files"])` and pass it to `MCPServerStreamableHttp`'s `tool_filter`. For full access, set `allowed_tool_names=["read_file", "write_file", "delete_file", "search_docs"]`. The same MCP server can expose different tool sets depending on the agent's role.
**Dynamic Filter to Hide danger_ Tools**
Dynamically hide dangerous tools based on agent role.
Import `ToolFilterContext` from `agents.mcp` and define a `role_based_filter` function. When ` is `"CodeReviewer"` and `"danger_")`, return `False` to hide the tool; otherwise return `True`. Pass this function to `MCPServerStreamableHttp`'s `tool_filter` for dynamic role-based tool filtering.
**Dynamic Instructions from MCP Server with get_prompt**
Fetch prompts from the MCP server to dynamically compose agent instructions.
Create an `MCPServerStreamableHttp` and use it in an `async with mcp:` context. Call `prompt_result = await mcp.get_prompt("operation_guidelines")` to fetch a prompt from the MCP server (an optional second argument takes a dict of prompt arguments), then embed `prompt_result.messages[0].content.text` into the agent's `instructions` to dynamically compose agent behavior.
**Latency Improvement with cache_tools_list**
Cache the tool list to reduce unnecessary requests to the MCP server.
Set `cache_tools_list=True` on `MCPServerStreamableHttp` to cache the tool list and reduce unnecessary requests to the MCP server. When tools are added or changed, call `await mcp.invalidate_tools_cache()` to clear the cache.
💡 Use Cases
🔒 Enforce least-privilege with create_static_tool_filter allowing only read_file/write_file
🎭 Hide danger_ tools from CodeReviewer with dynamic filters for role-based access control
📜 Fetch latest operational guidelines from MCP server via get_prompt for dynamic agent instructions
⚡ Enable cache_tools_list=True to reduce repeated tool-list fetch latency
⚠️ Caveats
- Typos in `allowed_tool_names` won't raise errors -- the non-existent tool name is simply ignored. Double-check your tool names.
- Dynamic filters run on every tool list retrieval, so avoid heavy logic that could impact latency.
- With `cache_tools_list=True`, changes to tools won't be reflected until you call `invalidate_tools_cache()`.
✨ Combine filters, prompts, and caching to improve MCP tool safety, flexibility, and performance simultaneously. Start with static filters for permission scoping, then add dynamic filters and caching as needed!
#
OpenAIAgentSDK# #
AIAgent#