.agents/references/local-mcp-server-lifecycle.md
Use this reference for changes to Python-managed MCP servers, MCPServerManager, client-session request ordering, tool caching or filtering, local MCP retries, cancellation, or cleanup. Hosted MCP is a provider tool and follows the OpenAI API contract; use $openai-knowledge for that protocol surface. Read Tool identity and routing for server-prefixed names and Tool execution lifecycle for approval and invocation behavior after an MCP tool is converted to a FunctionTool.
MCPServer owns its transport, ClientSession, and AsyncExitStack from connect() through cleanup(). Partial connection failure still requires closing every context already entered.MCPServerManager preserves task affinity in sequential mode and uses one long-lived worker task per server in parallel mode. Timeouts must run inside that owning task; on Python versions without asyncio.timeout(), cancel the current worker task and translate only timer-originated cancellation to TimeoutError.BaseException failures into recoverable connection errors.active_servers is the agent-facing list; with drop_failed_servers=True it excludes failed connections while preserving configured order.reconnect(failed_only=True) retries the deduplicated failed set without disturbing healthy connections. A full reconnect cleans up all servers first and rebuilds manager state.active_servers, failed_servers, or which workers remain registered.call_tool() still permits sibling cancellation and protocol races.list_tools() and call_tool() retries use the configured attempt count and backoff. Validate required arguments locally before starting retries so deterministic input errors never reach the server or consume retry budget.failure_error_function. Explicit None means propagate; cancellation of the parent run must not become model-visible tool failure output.cache_tools_list=True assumes server schemas are stable until invalidate_tools_cache() marks them dirty. Connection or filter changes must not accidentally make a stale filtered list authoritative.run_context and agent. A filter exception excludes that tool and logs the failure rather than exposing it by default.FunctionTool.docs/mcp.mdsrc/agents/mcp/server.pysrc/agents/mcp/manager.pysrc/agents/mcp/util.pytests/mcp/test_mcp_server_manager.pytests/mcp/test_connect_disconnect.pytests/mcp/test_client_session_retries.pytests/mcp/test_caching.pytests/mcp/test_tool_filtering.pytests/mcp/test_server_errors.pytests/mcp/test_runner_calls_mcp.py