haive.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.
Classes¶
Information about an MCP connection. |
|
Connection status states. |
|
MCP connection manager with health monitoring and auto-reconnection. |
Module Contents¶
- class haive.mcp.client.connection.ConnectionStatus[source]¶
-
Connection status states.
Initialize self. See help(type(self)) for accurate signature.
- class haive.mcp.client.connection.MCPConnection(name, transport, auto_reconnect=True, max_reconnect_attempts=5, reconnect_delay=1.0, max_reconnect_delay=60.0, health_check_interval=30.0, connection_timeout=30.0)[source]¶
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"})
Initialize MCP connection manager.
- Parameters:
name (str) – Connection name/identifier
transport (haive.mcp.client.transport.MCPTransport) – Transport implementation
auto_reconnect (bool) – Enable automatic reconnection
max_reconnect_attempts (int) – Maximum reconnection attempts
reconnect_delay (float) – Initial reconnection delay (seconds)
max_reconnect_delay (float) – Maximum reconnection delay (seconds)
health_check_interval (float) – Health check interval (seconds)
connection_timeout (float) – Connection timeout (seconds)
- async connect(timeout=None)[source]¶
Connect to the MCP server.
- Parameters:
timeout (Optional[float]) – Connection timeout (uses default if None)
- Returns:
Connected MCP client
- Raises:
MCPConnectionError – If connection fails
- Return type:
- get_client()[source]¶
Get the MCP client if connected.
- Returns:
MCP client instance
- Raises:
MCPConnectionError – If not connected
- Return type:
- get_info()[source]¶
Get connection information.
- Returns:
ConnectionInfo object with current state
- Return type:
- async health_check()[source]¶
Perform a health check on the connection.
- Returns:
Health check results
- Return type:
Dict[str, Any]
- is_connected()[source]¶
Check if connection is active.
- Returns:
True if connected, False otherwise
- Return type:
- async reconnect()[source]¶
Attempt to reconnect to the server.
- Returns:
Connected client if successful, None if failed
- Return type:
Optional[haive.mcp.client.mcp_client.MCPClient]