Overview
EveryMCPApp initialises a ServerRegistry that knows how to start and authenticate each server defined in mcp_agent.config.yaml. The registry works hand in hand with MCPConnectionManager to provide two patterns:
- Short-lived connections using
gen_client, perfect for one-off operations. - Persistent connections managed by the connection manager, ideal for agents and long-running workflows.
Inspect configured servers
Afterapp.run() the registry is accessible via the context:
stdio, sse, streamable_http, websocket), applies authentication (api_key or OAuth), and exposes helpers such as register_init_hook.
Ephemeral sessions with gen_client
Use gen_client when you need a connection for the duration of a with block. It spins up the server process (for stdio transports), performs initialisation, yields an MCPAgentClientSession, and tears everything down automatically—a handy pattern for scripts, CLI utilities, or inspection. The basic finder agent uses the same underlying helpers when it initialises an agent.
Persistent connections and connection pooling
TheServerRegistry owns a MCPConnectionManager (registry.connection_manager) that maintains long-lived connections in a background task group. You can either interact with it directly or rely on helpers such as mcp_agent.mcp.gen_client.connect.
Connection persistence in agents
Agents use the connection manager behind the scenes. Setconnection_persistence=True (the default) to keep servers warm between tool calls or False to close the transport after each request.
Aggregating multiple servers
MCPAggregator builds a “server-of-servers” using the same registry and connection manager. You can namespace tool calls or expose a merged surface area:
examples/basic/mcp_server_aggregator sample and is commonly used when turning an entire app into a single MCP server.
OAuth-aware connections
When server definitions includeauth.oauth, the registry and connection manager automatically coordinate with the app’s token manager. The OAuth examples highlight three recurring patterns:
examples/basic/oauth_basic_agent– an agent maintains persistent connections to the GitHub MCP server using the client-only loopback flow.examples/oauth/interactive_tool– a client opens a temporarygen_clientsession, receives anauth/request, and walks through the browser login.examples/oauth/pre_authorize– tokens are seeded before an asynchronous workflow runs so the background execution never needs interactive auth.
ClientSession interface; the difference lies in how tokens are acquired and stored.
Initialisation hooks and authentication
You can register custom logic that runs immediately after a server initialises. It is perfect for seeding credentials, warming caches, or performing health checks.mcp.servers[].auth.oauth), MCPApp automatically injects an OAuthHttpxAuth handler so MCPConnectionManager can obtain and refresh tokens using the shared TokenManager. This means you do not need to ship long-lived access tokens in your config.
Error handling & retries
MCPConnectionManagerkeeps track of connection health and will surface errors via logs (ProgressAction.FATAL_ERROR) without crashing your application.- Call
disconnect_all()orclose()if you want to force reconnection after rotating credentials. - When a connection fails during initialisation, any awaiting
get_servercall unblocks with an exception so that workflows can decide whether to retry or degrade gracefully.
