feat(mcp): MCP server startup auto-retry with exponential backoff#3578
Merged
feat(mcp): MCP server startup auto-retry with exponential backoff#3578
Conversation
Retry transient MCP server connection errors (Connection, Timeout) up
to `mcp.max_connect_attempts` times (default 3, range 1–10) with
exponential backoff (500 ms, 1 s, 2 s, … capped at 8 s).
Key changes:
- New `McpError::ManagerShuttingDown` variant; exhaustive
`is_retryable_connect_error` ensures all future variants are
deliberately classified
- Generic `retry_loop` helper driven by a `CancellationToken`; uses
`tokio::select! biased` so shutdown exits any backoff sleep immediately
- `connect_with_retry` wraps `connect_entry` using `retry_loop`
- `McpConfig::max_connect_attempts` with deserialize-time validation
(rejects 0 and >10 with a clear error); migration step 40 renames
the old `max_connect_retries` key in existing configs
- TUI spinner messages emitted via existing `status_tx` channel:
"Connecting to MCP server {id}..." / "Reconnecting … (attempt N/M)..."
- Integration test enforces `max_connect_attempts` presence across all
three `default.toml` copies
- `add_server` retry is explicitly out of scope (tracked as follow-up)
Closes #3568
164bacb to
4685ada
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ConnectionandTimeouterrors up tomcp.max_connect_attemptstimes (default 3, range 1–10) with exponential backoff (500 ms → 1 s → 2 s, capped at 8 s per interval)CancellationTokenis threaded through the retry loop;tokio::select! biasedensures any in-progress sleep exits within milliseconds ofshutdown_all_sharedbeing calledChanges
McpError::ManagerShuttingDown { server_id }— new terminal lifecycle variant, maps toNoneincode(), never retried; replaces string-literal guard in managerretry_loop— genericCancellationToken-driven helper with closed-form backoffmin(500 * 2^(n-1), 8000) ms; unit-testable viatokio::time::pause()connect_with_retry— thin wrapper bindingconnect_entryintoretry_loopis_retryable_connect_error— exhaustive match on allMcpErrorvariants (compile error on new unclassified variants)McpConfig::max_connect_attempts: u8— validated at deserialization (rejects 0 and >10); migration step 40 renamesmax_connect_retriesin existing configsstatus_txchannel (no TUI code change)max_connect_attemptspresence across all threedefault.tomlcopiesadd_serverretry explicitly out of scope; tracked as follow-up (see security finding F1)Test plan
cargo nextest run -p zeph-mcp --lib --bins— 440 tests pass (7 new retry-specific unit tests)cargo nextest run -p zeph-config --lib --bins— 342 tests pass (8 new config/migration tests + default.toml consistency integration test)cargo +nightly fmt --check— cleancargo clippy --workspace -- -D warnings— cleanMcpError::Connection(classified retryable); recommend adding structured HTTP status to the variant before extending retry toadd_serverCloses #3568