# AG2 Multi-MCP Session Management: Dynamic Server Connections with MCPClientSessionManager

AG2's `MCPClientSessionManager` revolutionizes how you connect to multiple MCP (Model Context Protocol) servers by enabling on-demand session creation within your agent workflows. Instead of maintaining persistent connections, you can now dynamically open sessions to different servers—whether they use stdio or SSE transports—right inside your tool functions.

This article explores how to leverage `MCPClientSessionManager` for flexible, resource-efficient multi-server agent architectures, with practical examples for building research assistants, data pipelines, and intelligent routing systems.

Traditional MCP integration patterns require opening sessions at application startup and keeping them alive for the entire workflow duration. While this works for single-server scenarios, it becomes cumbersome when you need to:

- Connect to multiple servers dynamically
- Switch between servers based on user queries
- Manage resources efficiently
- Handle both local (stdio) and remote (SSE) servers

`MCPClientSessionManager` solves these challenges by providing a clean, context-manager-based API for opening sessions on-demand, exactly when and where you need them.

**Key Features:**

- **On-Demand Session Creation**: Open MCP sessions only when needed, inside tool functions or agent workflows
- **Multi-Transport Support**: Seamlessly handle both `stdio` (process-based) and `SSE` (HTTP-based) protocols
- **Dynamic Server Selection**: Let agents choose which server to connect to based on runtime conditions
- **Automatic Resource Management**: Context managers ensure proper cleanup, preventing resource leaks
- **Session Isolation**: Each query gets a fresh session, preventing state pollution between requests
- **Tool-Based Integration**: Wrap session management in tools for LLM-driven server selection

**Why This Matters:**

Building multi-server agent systems traditionally requires complex connection pooling, manual resource management, and rigid server selection logic. `MCPClientSessionManager` abstracts away these complexities, allowing you to focus on building intelligent agent workflows that can dynamically adapt to different data sources and services.

**When to Use MCPClientSessionManager:**

Use `MCPClientSessionManager` when you need:

- **Multi-Server Workflows**: Connect to multiple MCP servers (arXiv, Wikipedia, databases, APIs) in a single workflow
- **Dynamic Server Selection**: Let agents decide which server to use based on the query context
- **Resource Efficiency**: Avoid keeping connections open when not in use
- **Mixed Transport Types**: Work with both local stdio servers and remote SSE endpoints
- **Tool-Based Architecture**: Integrate MCP servers as tools that agents can invoke on-demand

## Understanding MCPClientSessionManager  
`MCPClientSessionManager` is a utility class that simplifies managing MCP client sessions. Unlike traditional approaches where you open a session at startup and keep it alive, `MCPClientSessionManager` enables:

- **On-demand session creation**: Create sessions only when needed within your workflow
- **Dynamic server switching**: Select which MCP server to connect to at runtime
- **Multi-transport management**: Handle both `stdio` (process-based) and `SSE` (HTTP-based) protocols
- **Automatic cleanup**: Context managers ensure proper resource management

### Key Components  
**1. StdioConfig**: Configuration for stdio-based MCP servers (local processes) - Starts a Python process that communicates via stdin/stdout - Ideal for local tools like arXiv paper search, file system operations, or database queries - Example: Local arXiv paper search server

**2. SseConfig**: Configuration for SSE-based MCP servers (HTTP endpoints) - Connects to a remote server via Server-Sent Events - Perfect for remote APIs, cloud services, or distributed MCP servers - Example: Remote Wikipedia API server

**3. MCPConfig**: Container for multiple server configurations - Holds all available servers in one configuration object - Enables dynamic server selection at runtime - Supports mixing stdio and SSE servers

**4. MCPClientSessionManager**: The session manager class - Provides `open_session()` method that returns an async context manager - Automatically initializes sessions when opened - Tracks active sessions internally - Ensures proper cleanup on exit

## Basic Setup  
The simplest way to use `MCPClientSessionManager` is to open a session within an async context manager:
```python
from autogen.mcp.mcp_client import MCPClientSessionManager, StdioConfig

# Configure a stdio-based server
arxiv_server = StdioConfig(
    command="python3",
    args=["mcp/mcp_arxiv.py", "stdio", "--storage-path", "/tmp/arxiv_papers"],
    transport="stdio",
    server_name="ArxivServer",
)

# Open a session on-demand
async with MCPClientSessionManager().open_session(arxiv_server) as session:
    # Session is automatically initialized
    tools = await session.list_tools()
    # Use the session...
    # Session automatically closes when exiting the context
```
This pattern ensures:
- Session is initialized automatically
- Resources are cleaned up properly
- No manual connection management needed

## Configuring Multiple Servers  
For multi-server workflows, use `MCPConfig` to hold all server configurations:
```python
from autogen.mcp.mcp_client import MCPConfig, StdioConfig, SseConfig

# Configure a stdio-based MCP server (local process)
ArxivServer = StdioConfig(
    command="python3",
    args=["mcp/mcp_arxiv.py", "stdio", "--storage-path", "/tmp/arxiv_papers"],
    transport="stdio",
    server_name="ArxivServer",
)

# Configure an SSE-based MCP server (HTTP endpoint)
WikipediaServer = SseConfig(
    url="http://127.0.0.1:8000/sse",
    timeout=10,
    sse_read_timeout=60,
    server_name="WikipediaServer",
)

# Create an MCPConfig with both servers
mcp_config = MCPConfig(servers=[ArxivServer, WikipediaServer])

print(f"Configured {len(mcp_config.servers)} MCP servers:")
for server in mcp_config.servers:
    print(f"  - {server.server_name}")
```
This configuration allows you to dynamically select which server to use at runtime.
