Skip to content

feat: Add MCP connection keepalive/reload mechanism for CLI-37#562

Open
szmania wants to merge 14 commits into
cecli-dev:v0.100.9from
szmania:cli-37-mcp-connection-keepalive
Open

feat: Add MCP connection keepalive/reload mechanism for CLI-37#562
szmania wants to merge 14 commits into
cecli-dev:v0.100.9from
szmania:cli-37-mcp-connection-keepalive

Conversation

@szmania

@szmania szmania commented Jun 7, 2026

Copy link
Copy Markdown

Purpose

Implement a keepalive/reload mechanism for MCP server connections to prevent connection errors during periods of inactivity. Addresses Jira issue CLI-37: MCP connection keepalive/reload mechanism.

Changes

  • Added ConnectionState enum to track connection status (CONNECTED, UNHEALTHY, DISCONNECTED)
  • Implemented start_keepalive() and _keepalive_loop() methods to send periodic OPTIONS requests
  • Added exponential backoff reconnection logic with jitter
  • Added configurable keepalive interval (5–300 seconds)
  • Added comprehensive test suite including:
    • Configuration validation
    • Integration tests with mock server
    • Concurrency and resilience tests
    • Logging and metrics verification

Configuration Example

The keepalive_interval can be added to your MCP server configuration. Here is an example of what an MCP object looks like in JSON format:

{
  "name": "my-mcp-server",
  "url": "http://localhost:8000",
  "type": "http",
  "keepalive_interval": 30,
  "headers": {
    "Authorization": "Bearer my-token"
  },
  "enabled": true
}

Testing

All tests pass:

  • test_keepalive_config.py - Validates configuration parameters
  • test_keepalive_concurrency.py - Tests task lifecycle and isolation
  • test_keepalive_integration.py - Tests with mock server
  • test_keepalive_logging.py - Verifies proper logging
  • test_keepalive_resilience.py - Tests recovery scenarios
  • test_keepalive_unit.py - Tests state transitions

Related Issues

Fixes CLI-37: MCP connection keepalive/reload mechanism

Notes

  • The keepalive_interval is configurable in the server config:
    • If not provided, it defaults to 300 seconds.
    • The value must be between 5 and 300 seconds.
  • Jitter (±10%) is added to prevent timing analysis.
  • Reconnection uses exponential backoff (1s, 2s, 4s, 8s, 16s, 32s, capped at 300s).
  • Authentication headers are preserved in keepalive requests.
  • Connection state transitions are properly logged.

Checklist

  • Implementation complete
  • Tests added and passing
  • Documentation updated
  • No sensitive information in logs
  • Backward compatible
  • Follows existing code patterns

Your Name added 7 commits June 6, 2026 09:04
Co-authored-by: cecli (openai/code)
…7-mcp-connection-keepalive

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
@szmania szmania changed the title feat: implement MCP connection keepalive logic feat: Add MCP connection keepalive/reload mechanism for CLI-37 Jun 20, 2026
…li-37-mcp-connection-keepalive

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
@szmania szmania changed the base branch from main to v0.100.9 June 20, 2026 04:33
Your Name added 3 commits June 19, 2026 21:44
Co-authored-by: cecli (openai/gemini_cli_local/gemini-2.5-pro)
Co-authored-by: cecli (openai/gemini_cli_local/gemini-2.5-pro)
Co-authored-by: cecli (openai/gemini_cli_local/gemini-2.5-pro)
@szmania

szmania commented Jun 20, 2026

Copy link
Copy Markdown
Author

@dwash96 This one is ready

Your Name added 3 commits June 20, 2026 11:14
Co-authored-by: cecli (openai/gemini_cli_local/gemini-2.5-pro)
Co-authored-by: cecli (openai/gemini_cli_local/gemini-2.5-pro)
Co-authored-by: cecli (openai/gemini_cli_local/gemini-2.5-pro)
@szmania

szmania commented Jun 20, 2026

Copy link
Copy Markdown
Author

This addresses this error when being on cecli tui for a certain amoutn of time: File "C:\Users\<user>\AppData\Roaming\uv\tools\cecli-dev\Lib\site-packages\httpx\_transports\default.py", line 118, in map_httpcore_exceptions raise mapped_exc(message) from exc httpx.RemoteProtocolError: peer closed connection without sending complete message body (incomplete chunked read)

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.

1 participant