shenyu icon indicating copy to clipboard operation
shenyu copied to clipboard

[Task] Recover expired outbound MCP sessions without replaying tool calls

Open BobSong-dev opened this issue 1 month ago • 0 comments

Background

Parent #7949. This task belongs to outbound session lifecycle management. Remote session lifetime is different from request cleanup. Agent Gateway ingress remains stateless.

Existing capabilities and reuse

At master 87c22dde7, RequestScopedMcpClient retains an optional upstream MCP-Session-Id, cancels tracked requests and performs session deletion on close, but non-2xx responses (including 404) follow the generic upstream-error path. Reuse request isolation and the legacy pending-request, duplicate-ID and disconnect fixes (#7638); those are not new work in this issue.

Implementation scope

  • Detect protocol-defined remote session expiry and 404; invalidate only the affected binding and safely reinitialize for subsequent eligible work, with bounded or coalesced recovery and catalog generation checks.
  • Never replay an already-issued tools/call during recovery. Return an explicit outcome for that invocation and avoid claiming the remote side effect did not occur.
  • Define idle reclamation, close deadlines and failure isolation so one failed cleanup cannot block other targets or leak all generations.
  • For supported deployments using the legacy MCP Server, first inventory existing session-level caps and TTL/idle behavior; fill only verified gaps and document maximum sessions and multi-replica routing constraints.

Non-goals

Stateful Agent Gateway ingress, exactly-once remote execution, automatic tool retries, rebuilding fixed pending-request isolation, or gateway-wide runtime draining (#7929).

Acceptance criteria

  • [ ] Expired sessions cannot serve new work; concurrent expiry yields bounded reinitialization without resurrecting removed targets or credentials.
  • [ ] A dispatched tools/call is observed at most once by the test upstream even when it returns session-expiry or disconnects.
  • [ ] Idle and close-failure cases release local resources within bounds and preserve unrelated servers.
  • [ ] Stateless upstreams remain supported without invented session IDs.
  • [ ] Legacy session-level findings and supported multi-replica routing are documented; any required cap or TTL gaps have executable coverage.

Tests

Real MCP HTTP fixture with 404 and expiry, concurrent calls, reconnect, late responses, hanging or failed DELETE and idle expiry; assert side-effect counts and resource cleanup. Extend legacy-server lifecycle tests only for missing session-level behavior; retain request-ID isolation regressions.

Dependencies

Completed protocol contract, #7951 and #7952. Coordinate with #7953 credential revocation. #7929 may reuse close hooks later but is not implemented here.

BobSong-dev avatar Oct 21 '26 11:10 BobSong-dev