get_connection_status
Check the current connection state of an MCP server to verify connectivity, view server details, transport type, connection duration, and usage statistics.
Instructions
Check the current MCP server connection state.
Returns detailed information about the active connection including server information, transport type, connection duration, and usage statistics.
Returns: Dictionary with connection status including: - success: Always True - connected: Boolean indicating if currently connected - connection: Full ConnectionState if connected, None otherwise - message: Human-readable status message - metadata: Request timing and connection duration info
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- The handler function for the 'get_connection_status' tool. It retrieves the current connection state using ConnectionManager.get_status(), computes metadata like duration, and returns a structured status dictionary. Includes logging and user-facing updates via ctx.@mcp.tool async def get_connection_status(ctx: Context) -> dict[str, Any]: """Check the current MCP server connection state. Returns detailed information about the active connection including server information, transport type, connection duration, and usage statistics. Returns: Dictionary with connection status including: - success: Always True - connected: Boolean indicating if currently connected - connection: Full ConnectionState if connected, None otherwise - message: Human-readable status message - metadata: Request timing and connection duration info """ start_time = time.perf_counter() try: state = ConnectionManager.get_status() connected = state is not None elapsed_ms = (time.perf_counter() - start_time) * 1000 metadata: dict[str, Any] = { "request_time_ms": round(elapsed_ms, 2), } if connected and state: # Calculate connection duration if state.connected_at: duration_seconds = ( time.perf_counter() - state.connected_at.timestamp() ) metadata["connection_duration_seconds"] = round(duration_seconds, 2) message = f"Connected to {state.server_url}" connection_data = state.model_dump(mode="json") # User-facing debug update await ctx.debug(f"Connection status: connected to {state.server_url}") # Detailed technical log logger.debug( "Connection status checked", extra={ "connected": True, "server_url": state.server_url, "statistics": state.statistics, }, ) else: message = "Not connected to any MCP server" connection_data = None # User-facing debug update await ctx.debug("Connection status: not connected") # Detailed technical log logger.debug("Connection status checked", extra={"connected": False}) return { "success": True, "connected": connected, "connection": connection_data, "message": message, "metadata": metadata, } except Exception as e: # Status check should never fail, but handle gracefully elapsed_ms = (time.perf_counter() - start_time) * 1000 # User-facing error update await ctx.error(f"Unexpected error checking connection status: {str(e)}") # Detailed technical log logger.exception("Unexpected error checking connection status") return { "success": True, # Still return success with disconnected state "connected": False, "connection": None, "message": f"Unable to determine connection status: {str(e)}", "metadata": { "request_time_ms": round(elapsed_ms, 2), "error": str(e), }, }
- The @mcp.tool decorator registers the get_connection_status function as an MCP tool.@mcp.tool