Heuristic:1mcp app Agent Exponential Backoff Retry Tuning
| Knowledge Sources | |
|---|---|
| Domains | Reliability, Optimization |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Tune MCP server connection retries with exponential backoff (base delay × 2^(retry-1)) to balance startup speed against transient failure recovery.
Description
The `McpLoadingManager` uses an exponential backoff strategy for retrying failed MCP server connections. The delay for each retry attempt is calculated as `retryDelayMs * Math.pow(2, retryCount - 1)`. With the default `retryDelayMs` of 2000ms and `maxRetries` of 3, the delays are 2s, 4s, and 8s — giving transiently failing servers up to 14 seconds to recover before being marked as Failed. After the initial loading wave, a background retry timer (default interval: 60s) periodically re-attempts failed servers, processing up to 3 servers per cycle to avoid thundering-herd effects.
OAuth-required errors are never retried — the server enters `AwaitingOAuth` state immediately, since the issue requires user interaction, not time. Non-retryable errors (`NonRetryableClientConnectionError`) also stop the retry loop immediately.
Usage
Use this heuristic when:
- Tuning startup behavior for environments with flaky MCP servers (increase `maxRetries` or `retryDelayMs`)
- Reducing startup latency in controlled environments with reliable servers (set `maxRetries=0` for immediate failure)
- Balancing background recovery load (adjust `backgroundRetryIntervalMs` and `backgroundRetryMaxServersPerCycle`)
The Insight (Rule of Thumb)
- Action: Set `maxRetries`, `retryDelayMs`, `backgroundRetryIntervalMs`, and `backgroundRetryMaxServersPerCycle` via CLI flags or app config.
- Default values:
- `maxRetries` = 3
- `retryDelayMs` = 2000 (2 seconds)
- `backgroundRetryIntervalMs` = 60000 (60 seconds)
- `backgroundRetryMaxServersPerCycle` = 3
- `maxConcurrentLoads` = 5
- CLI flags: `--async-max-retries`, `--async-retry-delay`, `--async-background-retry-interval`, `--async-background-retry-max-servers`, `--async-max-concurrent-loads`
- Trade-off: Higher retries improve reliability for transient failures but delay startup readiness. Zero retries gives fastest startup but no resilience.
- Never retry: OAuth-required errors and `NonRetryableClientConnectionError` — these require intervention, not time.
Reasoning
Exponential backoff is a well-established strategy for handling transient failures in distributed systems. The exponential growth gives the failing service progressively more time to recover while avoiding excessive waiting. The base delay of 2s with 3 retries covers most transient network issues (DNS resolution, TLS handshake, process startup) within a 14-second window — short enough to not significantly delay async startup (which does not block the HTTP server).
The background retry mechanism ensures that servers that failed during initial loading get periodic recovery attempts without overwhelming the system. Capping at 3 servers per cycle prevents a burst of reconnection attempts when many servers fail simultaneously.
Code Evidence
Exponential backoff calculation from `src/core/loading/mcpLoadingManager.ts:445`:
const delay = this.config.retryDelayMs * Math.pow(2, retryCount - 1); // Exponential backoff
Default policy from `src/core/loading/backendLoadingPolicy.ts:13-20`:
export const DEFAULT_BACKEND_LOADING_POLICY: BackendLoadingPolicy = {
maxConcurrentLoads: DEFAULT_MAX_CONCURRENT_LOADS,
maxRetries: 3,
retryDelayMs: 2000,
enableBackgroundRetry: true,
backgroundRetryIntervalMs: 60000,
backgroundRetryMaxServersPerCycle: 3,
};
OAuth error short-circuit from `src/core/loading/mcpLoadingManager.ts:430`:
if (lastError.name === 'OAuthRequiredError') {
// ... set AwaitingOAuth state
return; // Don't retry OAuth errors
}
Non-retryable error break from `src/core/loading/mcpLoadingManager.ts:433-436`:
if (lastError instanceof NonRetryableClientConnectionError) {
logger.warn('mcpLoadingManager.failed.to.load.non.retryable.24de04a0', { error: error });
break;
}