The service client type (must implement ServiceClient)
Create a new ConnectionStateManager.
The service client type (must implement ServiceClient)
Optionaloptions: ConnectionTelemetry | ConnectionStateOptionsOptional configuration (telemetry, healthCheckTimeoutMs).
Also accepts a bare ConnectionTelemetry for backwards compatibility.
Clear all listeners. Useful for cleanup during shutdown.
Set the client and mark as connected. Validates the connection with a health check before completing. Includes OpenTelemetry tracing for the connection process.
The authenticated service client
Skip health check validation (for testing)
true if connection was successful
Disable automatic reconnection. Aborts any in-flight reconnect attempt and removes the listener.
Disconnect from the backend service. Calls ServiceClient.disconnect if the client implements it, then clears the client reference and sets state to 'disconnected'.
Enable automatic reconnection with exponential backoff.
When the connection state transitions to 'error', the manager will
automatically attempt to reconnect using the stored client instance.
Subsequent attempts use exponential backoff up to maxDelayMs.
This handles reconnection to the API backend, NOT MCP transport
reconnection (which is client-side, handled by the SDK's
StreamableHTTPReconnectionOptions).
Optionaloptions: ReconnectOptionsReconnect configuration (all fields optional with sensible defaults)
Get the current service client instance. Returns null if not connected.
Get the connection state history. Returns the most recent state changes (up to maxHistorySize). History is returned in chronological order (oldest first).
Get the last error that occurred (if any).
Get the current connection state.
Check if the client is currently connected.
Register a listener for connection state changes.
Function to call when state changes
Unsubscribe function
Reset the connection manager to initial state. Useful for testing or complete reset scenarios.
Set the connection state to 'connecting'. Call this before attempting to connect.
Set connection error state.
The error that occurred
Manages the connection state to a backend service.
Generic implementation that works with any client implementing ServiceClient.
Features:
Example