()
| 369 | |
| 370 | // Attempt reconnection with exponential backoff |
| 371 | const reconnectWithBackoff = async () => { |
| 372 | for ( |
| 373 | let attempt = 1; |
| 374 | attempt <= MAX_RECONNECT_ATTEMPTS; |
| 375 | attempt++ |
| 376 | ) { |
| 377 | // Check if server was disabled while we were waiting |
| 378 | if (isMcpServerDisabled(client.name)) { |
| 379 | logMCPDebug( |
| 380 | client.name, |
| 381 | `Server disabled during reconnection, stopping retry`, |
| 382 | ) |
| 383 | reconnectTimersRef.current.delete(client.name) |
| 384 | return |
| 385 | } |
| 386 | |
| 387 | updateServer({ |
| 388 | ...client, |
| 389 | type: 'pending', |
| 390 | reconnectAttempt: attempt, |
| 391 | maxReconnectAttempts: MAX_RECONNECT_ATTEMPTS, |
| 392 | }) |
| 393 | |
| 394 | const reconnectStartTime = Date.now() |
| 395 | try { |
| 396 | const result = await reconnectMcpServerImpl( |
| 397 | client.name, |
| 398 | client.config, |
| 399 | ) |
| 400 | const elapsed = Date.now() - reconnectStartTime |
| 401 | |
| 402 | if (result.client.type === 'connected') { |
| 403 | logMCPDebug( |
| 404 | client.name, |
| 405 | `${transportType} reconnection successful after ${elapsed}ms (attempt ${attempt})`, |
| 406 | ) |
| 407 | reconnectTimersRef.current.delete(client.name) |
| 408 | onConnectionAttempt(result) |
| 409 | return |
| 410 | } |
| 411 | |
| 412 | logMCPDebug( |
| 413 | client.name, |
| 414 | `${transportType} reconnection attempt ${attempt} completed with status: ${result.client.type}`, |
| 415 | ) |
| 416 | |
| 417 | // On final attempt, update state with the result |
| 418 | if (attempt === MAX_RECONNECT_ATTEMPTS) { |
| 419 | logMCPDebug( |
| 420 | client.name, |
| 421 | `Max reconnection attempts (${MAX_RECONNECT_ATTEMPTS}) reached, giving up`, |
| 422 | ) |
| 423 | reconnectTimersRef.current.delete(client.name) |
| 424 | onConnectionAttempt(result) |
| 425 | return |
| 426 | } |
| 427 | } catch (error) { |
| 428 | const elapsed = Date.now() - reconnectStartTime |
no test coverage detected