Skip to content

fix(roadmap): retry the first MCP connection of a new Job pod #4372

Description

@dan-petty

Problem

A new Job pod can fail its first connection to the MCP server. Kube-router programs the network policy of a new pod a few seconds after the pod starts. In that time, a connection to mcp-server on port 8001 is rejected at once (curl exit 7), although the policy admits the pod. The MCP client in src/devops_cli/roadmap/mcp.py does not retry a first connection. It raises RoadmapMcpUnavailableError, and the job stops.

Evidence (2026-11-10)

  1. A throwaway pod with the label app.kubernetes.io/name=devops-cli-job ran curl in the same second as its start. It got exit 7 (connection refused) and HTTP code 000.
  2. The same pod form, after sleep 20, got HTTP 401 from /implementation/mcp (reached, no token sent). A pod with another label got exit 7 after the same wait (blocked, as it must be).
  3. roadmap-service connects to the same Service without a fault.

Expected

A Job that starts with roadmap.implement.mcp or roadmap.stage.mcp on does not fail because of the start delay of the network policy. The client retries a refused first connection for a short, bounded time before it raises RoadmapMcpUnavailableError.

Notes

Use the retry library that the HTTP code of the project already uses (see the retry work of #4235), not a new loop. Today the risk is small because both switches are off and a job clones its repository before it calls a tool. Keep the bound in the constants module.

Proposed design

Problem statement

A new roadmap Job pod can fail its first MCP connection before kube-router programs the network policy. The MCP client raises RoadmapMcpUnavailableError on that first refusal and the job stops. Add a short bounded retry with the project's existing retry library so the job survives the policy start delay.

Acceptance criteria

Requirement: Retry a refused first connection

The roadmap MCP client SHALL retry a connection-refused error from the first RoadmapMcpToolset.__aenter__ attempt until the connection succeeds or the retry budget expires.

Scenario: First attempt refused, later attempt succeeds

  • WHEN The first __aenter__ attempt raises ConnectionRefusedError and a later attempt within the budget succeeds
  • THEN The toolset enters successfully and no RoadmapMcpUnavailableError is raised.

Requirement: Bounded retry budget from constants

The roadmap MCP client MUST read the total first-connection retry budget from ROADMAP_MCP_CONNECT_RETRY_SECONDS in src/devops_cli/config/constants.py.

Scenario: Budget comes from the constants module

  • WHEN The retry wrapper is constructed
  • THEN The total retry budget equals ROADMAP_MCP_CONNECT_RETRY_SECONDS and src/devops_cli/roadmap/mcp.py holds no retry-budget literal.

Requirement: Exponential backoff between attempts

The client SHALL wait with exponential backoff between connection attempts, starting at ROADMAP_MCP_CONNECT_RETRY_INITIAL_SECONDS and capping each delay at ROADMAP_MCP_CONNECT_RETRY_MAX_SECONDS.

Scenario: Backoff grows and caps

  • WHEN The retry wrapper records each delay before an attempt
  • THEN The delays grow from the initial value and never exceed the maximum value.

Requirement: Exhausted retry raises the existing error

When the retry budget expires, the client SHALL raise RoadmapMcpUnavailableError with the target URL and the last connection error as its cause.

Scenario: Every attempt is refused

  • WHEN Every first-connection attempt within the budget raises a connection-refused error
  • THEN RoadmapMcpUnavailableError is raised, names the target URL, and chains the last connection error.

Requirement: Authentication failure is not retried

The client MUST NOT retry a first connection that fails with an authentication error or HTTP 401.

Scenario: Authentication failure fails fast

  • WHEN The first __aenter__ attempt raises an authentication error or an HTTP 401 error
  • THEN RoadmapMcpUnavailableError is raised after one attempt and no retry occurs.

Requirement: Existing retry library is used

The client SHALL implement the first-connection retry with tenacity.AsyncRetrying and SHALL NOT add a hand-written retry loop.

Scenario: Retry uses tenacity

  • WHEN A test patches tenacity.AsyncRetrying and calls __aenter__ on a refused connection
  • THEN The patched AsyncRetrying is called and the retry path contains no hand-written loop.

Requirement: Retry scope is the first connection only

The retry MUST apply only to RoadmapMcpToolset.__aenter__ and MUST NOT change remote tool-call timeout or error handling.

Scenario: Tool call failure is not retried

  • WHEN A remote tool call fails after a successful connection
  • THEN The existing roadmap.mcp.timeout_seconds bound and ToolReturn error path run and no connection retry occurs.

Existing Tool Considered

No alternatives check has run. Use tenacity, the open, maintained retry library that the project's HTTP code already uses from #4235, through AsyncRetrying for a bounded exponential retry around RoadmapMcpToolset.__aenter__. It covers async bounded retries. It lacks a built-in MCP connection lifecycle hook, so the client needs a small wrapper and a total-budget constant in src/devops_cli/config/constants.py.

Key questions

  • Does the retry cover both stage and implement MCP roles?: Yes. build_roadmap_mcp_toolset returns RoadmapMcpToolset for both planning and implementation, so a retry in __aenter__ covers both roles. [sources: src/devops_cli/roadmap/mcp.py]
  • Which retry library should the first MCP connection use?: Use tenacity.AsyncRetrying for the first RoadmapMcpToolset.__aenter__ attempt. The HTTP code already uses this library from fix(ci): retry the download of a release asset #4235. Do not add a hand-written retry loop. (best practice) [sources: src/devops_cli/roadmap/mcp.py]
    • Chosen: tenacity AsyncRetrying. It is the project's existing retry library and supports async callables with bounded backoff.
    • Rejected: hand-written asyncio loop. The issue forbids a new loop and it adds code to keep.
    • Rejected: httpx transport retries. Transport retries do not cover the MCP toolset __aenter__ lifecycle.
  • What retry budget and backoff should the first connection use?: Retry for at most 30 seconds total. Start at 0.5 seconds, grow exponentially, and cap each delay at 5 seconds. The evidence shows a 20-second wait reaches the server. (best practice) [sources: src/devops_cli/roadmap/mcp.py, src/devops_cli/config/constants.py]
    • Chosen: 30 s total, 0.5 s initial, 5 s cap. It covers the observed policy delay with margin and keeps the job start bounded.
    • Rejected: 60 s total, fixed 5 s delay. It waits longer than needed after the policy is ready.
    • Rejected: no retry. It leaves the reported first-connection failure in place.
  • Which failures should the retry handle?: Retry only connection-refused and transient connect errors from the first __aenter__. Raise RoadmapMcpUnavailableError at once for authentication, HTTP 401, and other non-connection errors. (best practice) [sources: src/devops_cli/roadmap/mcp.py, docs/ERRORS.md:107]
    • Chosen: connection-refused only. It matches the reported race and avoids hiding auth or configuration faults.
    • Rejected: every exception. It repeats auth and configuration failures and delays the stop.
    • Rejected: no exception. It does not solve the reported race.
  • Where should the retry budget live?: Put ROADMAP_MCP_CONNECT_RETRY_SECONDS in src/devops_cli/config/constants.py. Put the initial delay and the maximum delay beside it. (best practice) [sources: src/devops_cli/config/constants.py, src/devops_cli/roadmap/mcp.py]
    • Chosen: constants module. The issue requires the bound there and the module already holds shared limits.
    • Rejected: settings. The bound is not a per-deployment setting and it adds configuration surface.
    • Rejected: literal in mcp.py. It hides the limit and violates the issue note.
  • How should the retry be tested?: Add offline tests in tests/roadmap/test_roadmap_mcp.py. Use a fake MCPToolset.__aenter__ that refuses then succeeds, that exhausts the budget, and that returns HTTP 401. (best practice) [sources: tests/roadmap/test_roadmap_mcp.py, src/devops_cli/roadmap/mcp.py]
    • Chosen: unit tests with a fake toolset. They run offline and check retry count, budget, and fast auth failure.
    • Rejected: cluster integration test. It needs a live pod and network policy and cannot run offline.
    • Rejected: no test. It leaves the retry and error paths unverified.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Alternatives check of 2026-11-11: covers.
Tool: jd/tenacity (https://github.com/jd/tenacity), Apache-2.0, last activity 2026-11-01, maintained.

Activity

  1. added
    source/agent
    type/bug
    scope/roadmap
    status/new
    value/low
    effort/low
    on Nov 10, 2026
  2. dan-petty commented on Nov 10, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: scope/roadmap. The (roadmap) scope of the title gives the scope.
    • Priority: P3-Low. The body states the risk is small today because both feature switches are off.
    • Value: Low. The issue is latent and does not affect current users because the MCP switches are off.
    • Effort: Low. The change uses an existing retry library and adds a bounded retry in one client file with a constant.
    • Related: fix(k8s): MCP server policies miss the Service runner Jobs #4355, which asks for a different change. Both deal with MCP server connectivity for Job pods under the same network policy, but #4355 fixes the policy label selector while the candidate adds client-side retry for the transient programming window.
  3. added
    priority/p3-low
    on Nov 10, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 10, 2026
  5. devops-cli-service commented on Nov 10, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 1ca0133 (see proposed design in issue body).

  6. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Alternatives check: covers

    Reason: Searches pointed at tenacity as the retry library and its package/repository facts confirm it is the open, maintained project that the issue names; its async retrying supports a bounded, exponentially backed-off retry around RoadmapMcpToolset.__aenter__. It covers the whole need because the retry is a client-side wrapper the project must write anyway, and no ADR rejects tenacity (ADR 0007 only names it as the retry library whose parameters supply the waits).

    Tool: jd/tenacity (https://github.com/jd/tenacity)

    • License: Apache-2.0
    • Last activity: 2026-11-01
    • Maintained: yes

    Covers:

    • Async retrying of a coroutine callable such as RoadmapMcpToolset.__aenter__
    • Bounded total retry time, so the first-connection budget can come from ROADMAP_MCP_CONNECT_RETRY_SECONDS
    • Exponential backoff between attempts, with an initial delay and a cap on each delay
    • Retry only on chosen exception types, so a connection-refused error is retried while auth/HTTP 401 failures are not
    • Raises the last error to the caller, so the client can wrap it in RoadmapMcpUnavailableError with the cause chained

    Lacks:

    • No MCP connection lifecycle hook of its own: the client still needs a small wrapper inside __aenter__
    • No knowledge of Kubernetes network-policy programming delay, so the budget constants must be chosen in the project's constants module
    • Does not by itself separate connection-refused from authentication errors; the client must supply that exception predicate
    • Does not change or replace the existing remote tool-call timeout and ToolReturn error path

    Tools considered:

    Searches run:

    • tenacity python retry library github repository, results: 5
    • tenacity AsyncRetrying async retry exponential backoff bounded, results: 5
    • tenacity readthedocs AsyncRetrying wait_exponential stop_after_delay, results: 5
    • tenacity AsyncRetrying documentation async retries Python, results: 5

    Sources:

    Checked on 2026-11-11. A person decides what this means for the item. The check changes no label and closes no issue.

  7. added
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 14, 2026
  8. removed
    status/in-review
    on Nov 14, 2026
  9. added a commit that references this issue on Nov 14, 2026
  10. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-14 15:02 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p3-lowscope/roadmapsource/agenttype/bugvalue/low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions