connectWithRetry method
Future<void>
connectWithRetry(
- Future<
ClientTransport> openTransport(), { - int maxRetries = 3,
- Duration delay = const Duration(seconds: 2),
- ProtocolEra era = ProtocolEra.auto,
Connect, retrying a failed attempt up to maxRetries times in all.
A failed attempt closes its transport, so every attempt gets a fresh
one from openTransport.
Implementation
Future<void> connectWithRetry(
Future<ClientTransport> Function() openTransport, {
int maxRetries = 3,
Duration delay = const Duration(seconds: 2),
ProtocolEra era = ProtocolEra.auto,
}) async {
for (var attempt = 1;; attempt++) {
try {
await connect(await openTransport(), era: era);
return;
} catch (e) {
if (attempt >= maxRetries) {
final error = McpError('Failed to connect after $maxRetries attempts: $e');
_errorStreamController.add(error);
throw error;
}
_logger.debug('Connection attempt $attempt failed: $e. Retrying in $delay');
await Future<void>.delayed(delay);
}
}
}