mcp.client.connection ===================== .. py:module:: mcp.client.connection .. autoapi-nested-parse:: 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 ---------- .. autoapisummary:: mcp.client.connection.logger Classes ------- .. autoapisummary:: mcp.client.connection.ConnectionInfo mcp.client.connection.ConnectionStatus mcp.client.connection.MCPConnection Module Contents --------------- .. py:class:: ConnectionInfo Information about an MCP connection. .. py:attribute:: client :type: Optional[mcp.client.mcp_client.MCPClient] :value: None .. py:attribute:: health_score :type: float :value: 1.0 .. py:attribute:: last_connected :type: Optional[float] :value: None .. py:attribute:: last_error :type: Optional[str] :value: None .. py:attribute:: metadata :type: Dict[str, Any] .. py:attribute:: name :type: str .. py:attribute:: reconnect_attempts :type: int :value: 0 .. py:attribute:: status :type: ConnectionStatus .. py:attribute:: transport :type: mcp.client.transport.MCPTransport .. py:class:: ConnectionStatus Bases: :py:obj:`str`, :py:obj:`enum.Enum` Connection status states. .. py:attribute:: CONNECTED :value: 'connected' .. py:attribute:: CONNECTING :value: 'connecting' .. py:attribute:: DISCONNECTED :value: 'disconnected' .. py:attribute:: ERROR :value: 'error' .. py:attribute:: RECONNECTING :value: 'reconnecting' .. py:class:: 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 .. rubric:: 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"}) .. py:method:: __aenter__() :async: Async context manager entry. .. py:method:: __aexit__(exc_type, exc_val, exc_tb) :async: Async context manager exit. .. py:method:: connect(timeout: Optional[float] = None) -> mcp.client.mcp_client.MCPClient :async: Connect to the MCP server. :param timeout: Connection timeout (uses default if None) :returns: Connected MCP client :raises MCPConnectionError: If connection fails .. py:method:: disconnect() -> None :async: Disconnect from the MCP server. .. py:method:: get_client() -> mcp.client.mcp_client.MCPClient Get the MCP client if connected. :returns: MCP client instance :raises MCPConnectionError: If not connected .. py:method:: get_info() -> ConnectionInfo Get connection information. :returns: ConnectionInfo object with current state .. py:method:: health_check() -> Dict[str, Any] :async: Perform a health check on the connection. :returns: Health check results .. py:method:: is_connected() -> bool Check if connection is active. :returns: True if connected, False otherwise .. py:method:: is_healthy(threshold: float = 0.7) -> bool Check if connection is healthy. :param threshold: Health score threshold (0.0-1.0) :returns: True if health score is above threshold .. py:method:: reconnect() -> Optional[mcp.client.mcp_client.MCPClient] :async: Attempt to reconnect to the server. :returns: Connected client if successful, None if failed .. py:method:: start_monitoring() -> None :async: Start background health monitoring. .. py:method:: stop_monitoring() -> None :async: Stop background health monitoring. .. py:attribute:: auto_reconnect :value: True .. py:attribute:: client :type: Optional[mcp.client.mcp_client.MCPClient] :value: None .. py:attribute:: connection_count :value: 0 .. py:attribute:: connection_timeout :value: 30.0 .. py:attribute:: error_count :value: 0 .. py:attribute:: health_check_interval :value: 30.0 .. py:attribute:: health_score :value: 1.0 .. py:attribute:: last_connected :type: Optional[float] :value: None .. py:attribute:: last_error :type: Optional[str] :value: None .. py:attribute:: last_operation_time :value: 0.0 .. py:attribute:: max_reconnect_attempts :value: 5 .. py:attribute:: max_reconnect_delay :value: 60.0 .. py:attribute:: name .. py:attribute:: reconnect_attempts :value: 0 .. py:attribute:: reconnect_delay :value: 1.0 .. py:attribute:: status .. py:attribute:: total_uptime :value: 0.0 .. py:attribute:: transport .. py:data:: logger