Skip to content

cli-55: Add retry logic for MCP connections#613

Merged
dwash96 merged 5 commits into
cecli-dev:v0.100.14from
szmania:cli-55-add-retry-logic
Jul 21, 2026
Merged

cli-55: Add retry logic for MCP connections#613
dwash96 merged 5 commits into
cecli-dev:v0.100.14from
szmania:cli-55-add-retry-logic

Conversation

@szmania

@szmania szmania commented Jul 19, 2026

Copy link
Copy Markdown

Purpose:
This PR introduces robust retry logic for MCP server connections within the McpServerManager. Previously, transient connection failures could lead to immediate disconnections. This change enhances the reliability and resilience of MCP server connections by implementing exponential backoff and retry mechanisms.

Design Considerations:

  • Exponential Backoff: Implemented an exponential backoff strategy with a base delay of 1 second, a backoff factor of 2.0, and a maximum delay of 30 seconds. This prevents hammering the server during repeated failures and allows for recovery.
  • Max Retries: Configured a maximum of 3 retry attempts to balance resilience with responsiveness.
  • Cancellation Handling: asyncio.CancelledError is now explicitly re-raised to ensure proper propagation of cancellation signals, preventing silent suppression of critical control flow.
  • Logging: Enhanced logging to provide clear warnings on each retry attempt and a final error message upon exhaustion of all retries.
  • LocalServer Exemption: LocalServer connections are exempt from retry logic as they are local and not subject to network-related transient failures.
  • Unnamed Server Error Suppression: Error logging for "unnamed-server" is suppressed to avoid unnecessary noise during internal or temporary server operations.
  • Integration Simplification: The from_servers method and add_server_with_retry helper were simplified, as the retry logic is now encapsulated within connect_server.

What Changed:

  • Modified cecli/mcp/manager.py:
    • Added a for loop with max_retries, delay, backoff, and max_delay variables to connect_server.
    • Introduced asyncio.sleep for delays between retries.
    • Added _log_warning for intermediate failures and _log_error for final failure.
    • Explicitly re-raised asyncio.CancelledError.
    • Removed the redundant retry loop from add_server_with_retry in from_servers.
  • Modified tests/mcp/test_manager.py:
    • Updated test_connect_server_failure to reflect the new retry count (3 calls to connect).
    • Added assertions for tool_warning and tool_error calls.
  • Created tests/mcp/test_manager_retry.py:
    • Implemented 28 comprehensive test cases (TC-001 to TC-028) covering core retry behavior, edge cases (cancellation, no retry), timing, logging, integration with /load-mcp, /load-session, ResourceManager, and regression tests.
    • Ensured all new retry logic and existing functionalities are thoroughly validated.

Tests:
A new test file tests/mcp/test_manager_retry.py has been added, containing 28 detailed test cases to validate the new retry logic and ensure no regressions were introduced. These tests cover:

  • Successful connection after 1st, 2nd, or 3rd attempt.
  • Failure after all retries are exhausted.
  • No retry for LocalServer or non-existent servers.
  • Proper propagation of asyncio.CancelledError.
  • Exponential backoff timing and capping.
  • Correct logging of warnings and errors.
  • Integration with from_servers, /load-mcp command, /load-session command, and ResourceManager tool.
  • Regression coverage for existing functionalities.
  • Retry behavior on tool-loading failures.

Your Name added 5 commits July 17, 2026 14:20
<description>Added retry logic to `McpServerManager.connect_server` and simplified `add_server_with_retry` to use async retry with exponential backoff.</description>
@dwash96
dwash96 changed the base branch from main to v0.100.14 July 21, 2026 02:40
@dwash96
dwash96 merged commit 17f5694 into cecli-dev:v0.100.14 Jul 21, 2026
12 checks passed
@dwash96 dwash96 mentioned this pull request Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants