mcp.client.connectionΒΆ
MCP Connection Management.
This module provides connection management utilities for MCP clients. It handles connection pooling, health monitoring, and connection lifecycle management for different transport types.
AttributesΒΆ
ClassesΒΆ
Information about an MCP connection. |
|
Connection status states. |
|
MCP connection manager with health monitoring and auto-reconnection. |
Module ContentsΒΆ
- class mcp.client.connection.ConnectionInfoΒΆ
Information about an MCP connection.
- client: mcp.client.mcp_client.MCPClient | None = NoneΒΆ
- status: ConnectionStatusΒΆ
- transport: mcp.client.transport.MCPTransportΒΆ
- class mcp.client.connection.ConnectionStatusΒΆ
-
Connection status states.
- CONNECTED = 'connected'ΒΆ
- CONNECTING = 'connecting'ΒΆ
- DISCONNECTED = 'disconnected'ΒΆ
- ERROR = 'error'ΒΆ
- RECONNECTING = 'reconnecting'ΒΆ
- class mcp.client.connection.MCPConnection(name: str, transport: mcp.client.transport.MCPTransport, auto_reconnect: bool = True, max_reconnect_attempts: int = 5, reconnect_delay: float = 1.0, max_reconnect_delay: float = 60.0, health_check_interval: float = 30.0, connection_timeout: float = 30.0)ΒΆ
MCP connection manager with health monitoring and auto-reconnection.
- This class manages individual MCP connections with features like:
Health monitoring and scoring
Automatic reconnection with backoff
Connection lifecycle management
Error tracking and recovery
Performance metrics
Examples
Basic connection management:
from haive.mcp.client import MCPConnection, StdioTransport transport = StdioTransport("npx", ["-y", "@modelcontextprotocol/server-filesystem"]) connection = MCPConnection("filesystem", transport) await connection.connect() client = connection.get_client() tools = await client.list_tools() await connection.disconnect()
With health monitoring:
connection = MCPConnection( "filesystem", transport, health_check_interval=30.0, auto_reconnect=True ) await connection.start_monitoring() # Connection will be monitored and auto-reconnected
Connection status checking:
if connection.is_healthy(): client = connection.get_client() result = await client.call_tool("read_file", {"path": "/tmp/test"})
- async __aenter__()ΒΆ
Async context manager entry.
- async __aexit__(exc_type, exc_val, exc_tb)ΒΆ
Async context manager exit.
- async connect(timeout: float | None = None) mcp.client.mcp_client.MCPClient ΒΆ
Connect to the MCP server.
- Parameters:
timeout β Connection timeout (uses default if None)
- Returns:
Connected MCP client
- Raises:
MCPConnectionError β If connection fails
- get_client() mcp.client.mcp_client.MCPClient ΒΆ
Get the MCP client if connected.
- Returns:
MCP client instance
- Raises:
MCPConnectionError β If not connected
- get_info() ConnectionInfo ΒΆ
Get connection information.
- Returns:
ConnectionInfo object with current state
- async health_check() Dict[str, Any] ΒΆ
Perform a health check on the connection.
- Returns:
Health check results
- is_healthy(threshold: float = 0.7) bool ΒΆ
Check if connection is healthy.
- Parameters:
threshold β Health score threshold (0.0-1.0)
- Returns:
True if health score is above threshold
- async reconnect() mcp.client.mcp_client.MCPClient | None ΒΆ
Attempt to reconnect to the server.
- Returns:
Connected client if successful, None if failed
- auto_reconnect = TrueΒΆ
- client: mcp.client.mcp_client.MCPClient | None = NoneΒΆ
- connection_count = 0ΒΆ
- connection_timeout = 30.0ΒΆ
- error_count = 0ΒΆ
- health_check_interval = 30.0ΒΆ
- health_score = 1.0ΒΆ
- last_operation_time = 0.0ΒΆ
- max_reconnect_attempts = 5ΒΆ
- max_reconnect_delay = 60.0ΒΆ
- nameΒΆ
- reconnect_attempts = 0ΒΆ
- reconnect_delay = 1.0ΒΆ
- statusΒΆ
- total_uptime = 0.0ΒΆ
- transportΒΆ
- mcp.client.connection.loggerΒΆ