Skip to content

Dynamic Tools#

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.

{/ more /}

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

Don't use MCPClientSessionManager for single-server, long-running workflows where persistent connections are more efficient.

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