Recover MCP servers after OAuth reauthentication (#37337)

## Why

An OAuth-backed Streamable HTTP MCP server can fail startup when its stored credentials are rejected. If a client then completes OAuth sign-in and replaces those credentials, the failed server needs to become available without restarting the Codex session.

## What changed

- Track the credential store and OAuth tokens used for each MCP connection, and detect replacements after authentication failures.
- Refresh affected MCP servers before the next turn and include their recovered tools in the model request.
- Classify HTTP authentication challenges and rejected refresh tokens as requiring reauthentication, with sign-in guidance appropriate to local and client-managed environments.

## Testing

- Cover recovery from expired credentials through an externally written replacement token.
- Cover authentication-error classification, startup messaging, and `Auto` store migration from file credentials to the keyring.

GitOrigin-RevId: 9b090089d4e7a44ffc182f4e25da20a18e97b70f
This commit is contained in:
jif authored and copyberry committed 2026-08-07 00:17:46 +00:00
1 parent 7257826ab2
commit b3ffe3d001
14 files changed
+408 -57

No files matched your search

@@ -59,15 +59,14 @@ pub(super) fn mcp_startup_failure_reason(
return None;
}
match auth_state {
Some(McpAuthState::LoggedOut(McpLoginRequirement::Reauthentication)) => {
Some(McpStartupFailureReason::ReauthenticationRequired)
}
Some(
McpAuthState::LoggedOut(McpLoginRequirement::Reauthentication) | McpAuthState::OAuth,
) => Some(McpStartupFailureReason::ReauthenticationRequired),
Some(
McpAuthState::Unsupported
| McpAuthState::Unknown
| McpAuthState::LoggedOut(McpLoginRequirement::Login)
| McpAuthState::BearerToken
| McpAuthState::OAuth,
| McpAuthState::BearerToken,
)
| None => None,
}
@@ -91,13 +90,18 @@ pub(super) fn mcp_init_error_display(
format!(
"GitHub MCP does not support OAuth. Log in by adding a personal access token (https://github.com/settings/personal-access-tokens) to your environment and config.toml:\n[mcp_servers.{server_name}]\nbearer_token_env_var = CODEX_GITHUB_PERSONAL_ACCESS_TOKEN"
)
} else if matches!(
error,
StartupOutcomeError::Failed { error, .. } if error.contains("Auth required")
) {
format!(
"The {server_name} MCP server is not logged in. Run `codex mcp login {server_name}`."
} else if error.is_authentication_required()
|| matches!(
error,
StartupOutcomeError::Failed { error, .. } if error.contains("Auth required")
)
{
let recovery_hint = if config.is_some_and(|config| !config.is_local_environment()) {
"Use your client's MCP OAuth sign-in flow.".to_string()
} else {
format!("Run `codex mcp login {server_name}`.")
};
format!("The {server_name} MCP server is not logged in. {recovery_hint}")
} else if matches!(
error,
StartupOutcomeError::Failed { error, .. }