Skip to content

mcp: proactively re-mint managed proxy tokens before expiry during agent runs - #15539

Draft
Legoben wants to merge 1 commit into
bens/mcp_re-mint_expired_managed_proxy_tokens_on_demand_instead_of_failing_or_misrouting_to_oauthfrom
bens/mcp_proactively_re-mint_managed_proxy_tokens_before_expiry_during_agent_runs
Draft

mcp: proactively re-mint managed proxy tokens before expiry during agent runs#15539
Legoben wants to merge 1 commit into
bens/mcp_re-mint_expired_managed_proxy_tokens_on_demand_instead_of_failing_or_misrouting_to_oauthfrom
bens/mcp_proactively_re-mint_managed_proxy_tokens_before_expiry_during_agent_runs

Conversation

@Legoben

@Legoben Legoben commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Problem

The previous PR heals an expired token reactively (first failed call pays a reconnect round-trip). For long agent runs we can do better: the mutation's expires_at tells us exactly when the token dies, and re-minting early is cheap and overlap-safe.

Changes

  • Resolution now also records expires_at per managed installation (previously a dead field on the mutation output).
  • New managed_mcp_refresh::refresh_loop, raced as a third select! arm in with_credential_refreshes (same pattern as the git/Bedrock credential loops — never resolves, dropped when the run finishes): sleeps until 5 minutes before the earliest expiry (never spinning faster than 60s), re-mints due entries with the full startup retry budget, and respawns via the new TemplatableMCPServerManager::respawn_with_installation — which skips the refresher (config is pre-refreshed) and defers to any reactive reconnect already in flight.
  • Stop conditions: runtime-kind tokens are anchored to the task's start, so a re-mint that doesn't extend the deadline drops the entry (the sandbox ends then anyway); re-mint failures also drop the entry and leave recovery to the reactive path. Respawn happens only when the rendered config actually changed.

Known trade-off (accepted): a proactive respawn tears down the live transport; a tool call in flight at that instant gets TransportClosed and is retried once against the fresh connection by ReconnectingPeer.

Flag-gated by McpSelfHeal (the loop is only armed when managed entries with expirations exist).

Tests

Schedule math (wake at expiry−lead, 60s clamp for due/past tokens, due-window boundaries) plus a resolution test asserting expires_at is captured into the schedule.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant