็™ป้Œฒใ—ใฆๆ‹›ๅพ…ใƒชใƒณใ‚ฏใ‚’ๅ…ฑๆœ‰ใ™ใ‚‹ใจใ€ๅ‹•็”ปๅ†็”Ÿๅ ฑ้…ฌใจ็ดนไป‹ๅ ฑ้…ฌใ‚’็ฒๅพ—ใงใใพใ™ใ€‚

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture ๆŠ•็จฟใฏๅ€‹ไบบใฎๆ„่ฆ‹ใงใ™ใ€‚
ๅ‚ๅŠ  May 2026
286 ใƒ•ใ‚ฉใƒญใƒผไธญ    469 ใƒ•ใ‚กใƒณ
# Practical and Useful Patterns for OpenAI Agent SDK ๐ŸŒ Connect your own MCP servers to agents. Choose from HTTP, SSE, or stdio transports, and manage connections, approvals, and metadata injection at scale. Local MCP servers run on your own infrastructure, giving you low latency, high security, and full freedom for custom tools. ๐Ÿ“Œ Title: MCP -- Streamable HTTP / SSE / stdio ๐Ÿ”— URL: ๐Ÿงฉ Overview Local MCP servers let you connect self-hosted MCP servers as agent tools. Choose from three transports: `MCPServerStreamableHttp` (HTTP-based with auth, retry, and timeout support), `MCPServerSse` (Server-Sent Events), and `MCPServerStdio` (local CLI process). Use `MCPServerManager` for bulk connection management across multiple servers, `require_approval` for per-tool approval control, and `tool_meta_resolver` for tenant metadata injection. ๐Ÿ›  How to Use Import `Agent` and `Runner` from `agents`, and `MCPServerStreamableHttp` and `MCPServerStdio` from `agents.mcp`. For HTTP connections, pass `MCPServerStreamableHttp` to the agent's `mcp_servers`, putting the connection details in the `params` dict: `params={"url": "http://internal-mcp.corp:8080/mcp", "headers": {"Authorization": "Bearer "}, "timeout": 30}`. For stdio connections, use `MCPServerStdio` with `params={"command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/workspace"]}` to launch a local CLI process. ๐Ÿ— Practical Usage Patterns **Authenticated Internal MCP with MCPServerStreamableHttp** Connect to internal services with low latency, auth headers, retry, and timeouts. Create an `MCPServerStreamableHttp` with `params={"url": "http://internal-mcp.corp:8080/mcp", ...}`, set the `Authorization` header inside `params["headers"]` using `os.getenv('MCP_TOKEN')`, and configure `"timeout": 30`. Retries are a top-level keyword outside `params`, e.g. `max_retry_attempts=3`. Pass it to the agent's `mcp_servers` for an authenticated internal MCP connection. **Local CLI Tools with MCPServerStdio** Connect filesystem servers or custom CLI tools via stdio transport. Create an `MCPServerStdio` with `params={"command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/workspace"]}` for a filesystem server. For custom tools, use `params={"command": "python", "args": ["-m", "my_custom_mcp_server"], "env": {"DB_URL": os.getenv("DB_URL")}}` to pass environment variables. Add both to the agent's `mcp_servers` list to use multiple stdio tools simultaneously. **Bulk Server Management with MCPServerManager** Efficiently manage multiple MCP servers with reconnection control. Import `MCPServerManager` from `agents.mcp` and pass multiple MCP server instances to `servers`. Call `await manager.connect_all()` for bulk connection, check ` for active ones, and use `await manager.reconnect(failed_only=True)` to reconnect only failed servers. **Per-Tool Approval Map + tool_meta_resolver** Set different approval policies per tool and inject tenant metadata at execution time. Define a per-tool approval policy as a dict mapping `"search_docs"` and `"read_file"` to `"never"`, and `"write_file"`, `"delete_file"`, `"execute_query"` to `"always"`. Write a resolver that takes an `MCPToolMetaContext`, reads tenant data from ` and returns a metadata dict such as `{"tenant_id": str(tenant_id), "source": "agents-sdk"}` โ€” it attaches per-call `_meta` rather than rewriting the tool arguments. Pass the approval map to `require_approval` and the resolver to `tool_meta_resolver` on `MCPServerStreamableHttp` for per-tool approval control with automatic tenant metadata. ๐Ÿ’ก Use Cases ๐Ÿข Connect to internal MCP servers with auth, timeout, and low latency via MCPServerStreamableHttp ๐Ÿ“ Attach local filesystem servers and custom CLI tools to agents via MCPServerStdio ๐Ÿ”„ Manage multiple servers with MCPServerManager, reconnect only failed ones with failed_only=True ๐Ÿ” Per-tool approval maps + tool_meta_resolver for automatic tenant ID injection in multi-tenant setups โš ๏ธ Caveats - MCPServerStdio launches local processes, so the target command must be installed in your container environment. - MCPServerManager's reconnect handles connection errors only -- you still need separate error handling for tool execution failures. - Do not include secrets in tool_meta_resolver injections, as they may appear in logs. โœจ Master the three transports and MCPServerManager, and you can connect anything from internal services to local tools. Start with stdio for local experimentation, then move to HTTP for production! #OpenAIAgentSDK# #AIAgent#
ใ‚‚ใฃใจ่ฆ‹ใ‚‹