Surface MCP reconnect signals when expired OAuth tokens cannot refresh (#43947)

## Why

Refresh failures could leave expired MCP OAuth credentials reporting ordinary errors instead of signaling that authentication is required, preventing tool calls from offering a reconnect signal.

## What changed

- Classify provider failures and timeouts as `AuthorizationRequired` when the access token has expired. Keep proactive refresh failures as ordinary errors while the token remains valid.
- Reread stored credentials after a failed refresh and adopt a valid login completed during the request, enforcing the existing issuer binding for refresh tokens. Preserve stored credentials for later retries.
- Convert authentication-required tool-call errors into a reconnect message with `mcp/www_authenticate` metadata, without exposing provider or transport details.

## Testing

Add coverage for startup and runtime OAuth recovery, proactive refresh failures, concurrent login adoption, issuer mismatches, credential preservation, and successful retries. Verify failed local refreshes do not send tool calls and server-rejected calls are not replayed.

GitOrigin-RevId: 9cf64b0452685d2085df5dc0bcecee16ffe42e47
This commit is contained in:
Andrei Eternal authored and copyberry committed 2026-09-09 00:03:41 +00:00
1 parent 7c098d8741
commit 5ac0b8768d
4 files changed
+627 -16

No files matched your search

@@ -5,6 +5,7 @@ use std::time::SystemTime;
use std::time::UNIX_EPOCH;
use anyhow::Context;
use anyhow::Error;
use anyhow::Result;
use codex_keyring_store::DefaultKeyringStore;
use codex_keyring_store::KeyringStore;
@@ -24,6 +25,7 @@ use super::OAuthPersistorInner;
use super::StoredOAuthTokens;
use super::WrappedOAuthTokenResponse;
use super::compute_expires_at_millis;
use super::expires_in_from_timestamp;
use super::refresh_lock::RefreshCredentialLock;
use super::token_needs_refresh;
use super::validate_refresh_token_issuer;
@@ -155,8 +157,7 @@ impl OAuthPersistor {
return Ok(());
}
// Preserve RMCP's `AuthorizationRequired` marker only for credentials known to be
// unrefreshable. Network and provider failures below remain ordinary errors.
// Without a refresh token, authorization is required before contacting the provider.
if !latest.has_refresh_token() {
return Err(AuthError::AuthorizationRequired).with_context(|| {
format!(
@@ -192,8 +193,8 @@ impl OAuthPersistor {
refreshed_tokens(token_response, &latest, &self.inner)
}
Ok(Err(error @ AuthError::TokenRefreshRejected(_))) => {
// RMCP 3 distinguishes definitive refresh-token rejection from transient
// provider failures. Only a rejected token requires a fresh authorization.
// Definitive rejection requires authorization even if the access token has not
// expired yet. Other refresh failures below check actual access-token expiry.
warn!(
error = %error,
"MCP OAuth refresh token was rejected; reauthorization required"
@@ -210,22 +211,26 @@ impl OAuthPersistor {
error = %error,
"MCP OAuth provider refresh failed"
);
return Err(error).with_context(|| {
format!(
"failed to refresh OAuth tokens for server {}",
self.inner.server_name
)
});
let error = Error::new(error).context(format!(
"failed to refresh OAuth tokens for server {}",
self.inner.server_name
));
return self
.recover_after_failed_refresh(keyring_store, &mut guard, &latest, error)
.await;
}
Err(_) => {
warn!(
timeout_ms = refresh_request_timeout.as_millis(),
"MCP OAuth provider refresh timed out; the outcome is unknown and a later serialized retry is permitted"
);
anyhow::bail!(
let error = anyhow::anyhow!(
"timed out after {refresh_request_timeout:?} refreshing OAuth tokens for server {}",
self.inner.server_name
);
return self
.recover_after_failed_refresh(keyring_store, &mut guard, &latest, error)
.await;
}
};
@@ -267,6 +272,63 @@ impl OAuthPersistor {
debug!("persisted refreshed MCP OAuth credentials and completed the transaction");
Ok(())
}
async fn recover_after_failed_refresh<K: KeyringStore + Clone + 'static>(
&self,
keyring_store: &K,
manager: &mut AuthorizationManager,
previous: &StoredOAuthTokens,
error: Error,
) -> Result<()> {
// A failed proactive refresh does not require a new login while the access token is
// still valid. Use actual expiry, not the 30-second refresh buffer.
if !token_has_expired(previous.expires_at) {
return Err(error);
}
// Browser login can finish while the provider request is pending. Reread the pinned
// authority before prompting, and never delete or overwrite a replacement credential.
let replacement = self.inner.credential_store.load(
keyring_store,
&self.inner.server_name,
&self.inner.url,
)?;
if let Some(replacement) = replacement
&& !token_has_expired(replacement.expires_at)
&& !replacement.client_id.trim().is_empty()
&& !replacement
.token_response
.0
.access_token()
.secret()
.trim()
.is_empty()
{
// Match the existing pre-refresh adoption rule: refresh credentials must remain
// bound to the issuer already validated for this authorization manager.
if replacement.has_refresh_token()
&& (replacement.bound_issuer().is_none()
|| replacement.bound_issuer() != previous.bound_issuer())
{
return Err(AuthError::AuthorizationRequired).context(
"replacement MCP OAuth refresh credentials do not match the validated issuer",
);
}
debug!("adopting new MCP OAuth credentials after a failed refresh");
install_tokens_in_manager(manager, &replacement).await?;
*self.inner.last_credentials.lock().await = Some(replacement);
return Ok(());
}
warn!("MCP OAuth access token is expired and refresh failed; reauthorization required");
// Keep AuthorizationRequired as the source so both startup classification and runtime
// tool-call recovery recognize it; retain the original failure as diagnostic context.
Err(Error::new(AuthError::AuthorizationRequired).context(error))
}
}
fn token_has_expired(expires_at: Option<u64>) -> bool {
expires_at.is_some_and(|expires_at| expires_in_from_timestamp(expires_at).is_none())
}
/// Installs tokens without resolving metadata again, so callers can pin the validated snapshot.
@@ -49,6 +49,7 @@ use crate::oauth::OAuthRuntime;
use crate::oauth::ResolvedOAuthCredentialStore;
use crate::oauth::StoredOAuthTokens;
use crate::oauth::WrappedOAuthTokenResponse;
use crate::oauth::compute_expires_at_millis;
use crate::oauth::compute_store_key;
use crate::oauth::delete_oauth_tokens;
use crate::oauth::load_oauth_tokens_from_file;
@@ -533,8 +534,14 @@ async fn issuerless_newer_credentials_are_not_adopted_before_refresh() -> Result
}
#[tokio::test(flavor = "current_thread")]
async fn transient_refresh_failure_does_not_require_reauthorization() -> Result<()> {
let (_env, server, initial) = test_context().await?;
async fn proactive_refresh_failure_with_unexpired_token_does_not_require_reauthorization()
-> Result<()> {
let (_env, server, mut initial) = test_context().await?;
initial
.token_response
.0
.set_expires_in(Some(&Duration::from_secs(/*secs*/ 30)));
initial.expires_at = compute_expires_at_millis(&initial.token_response.0);
Mock::given(method("POST"))
.and(path("/oauth/token"))
.and(body_string_contains("grant_type=refresh_token"))
@@ -638,6 +645,139 @@ async fn assert_caller_cancellation(mode: crate::McpOAuthRefreshMode) -> Result<
Ok(())
}
#[tokio::test(flavor = "current_thread")]
async fn failed_refresh_adopts_login_completed_during_request() -> Result<()> {
for expires_in in [Some(Duration::from_secs(/*secs*/ 3600)), None] {
let (_env, server, initial) = test_context().await?;
let mut replacement = initial.clone();
replacement.client_id = "new-login-client".to_string();
replacement
.token_response
.0
.set_access_token(AccessToken::new("new-login-access-token".to_string()));
replacement
.token_response
.0
.set_expires_in(expires_in.as_ref());
replacement.expires_at = compute_expires_at_millis(&replacement.token_response.0);
save_oauth_tokens_to_file(&initial)?;
let persistor = persistor_for(&initial).await?;
let provider_replacement = replacement.clone();
Mock::given(method("POST"))
.and(path("/oauth/token"))
.and(body_string_contains("grant_type=refresh_token"))
.respond_with(move |_request: &wiremock::Request| {
// A browser login writes independently of the refresh transaction lock.
save_oauth_tokens_to_file(&provider_replacement).expect("complete new login");
ResponseTemplate::new(200).set_body_string("not an OAuth token response")
})
.expect(1)
.mount(&server)
.await;
persistor.refresh_if_needed().await?;
let live = persistor.stored_credentials().await.expect("adopted login");
let mut expected_live = replacement.clone();
expected_live.token_response.0.set_expires_in(None);
assert_eq!(live, expected_live);
persistor.persist_if_needed().await?;
let stored = load_oauth_tokens_from_file(&initial.server_name, &initial.url)?
.expect("new login must remain stored");
assert_tokens_match_without_expiry(&stored, &replacement);
server.verify().await;
}
Ok(())
}
#[tokio::test(flavor = "current_thread")]
async fn failed_refresh_does_not_adopt_unbound_replacement_credentials() -> Result<()> {
for issuer in [None, Some("https://different-issuer.example.test")] {
let (_env, server, initial) = test_context().await?;
let mut replacement = initial.clone();
replacement.issuer = issuer.map(str::to_string);
replacement.expires_at = Some(u64::MAX);
replacement
.token_response
.0
.set_access_token(AccessToken::new("replacement-access-token".to_string()));
save_oauth_tokens_to_file(&initial)?;
let persistor = persistor_for(&initial).await?;
let provider_replacement = replacement.clone();
Mock::given(method("POST"))
.and(path("/oauth/token"))
.and(body_string_contains("grant_type=refresh_token"))
.respond_with(move |_request: &wiremock::Request| {
save_oauth_tokens_to_file(&provider_replacement).expect("complete new login");
ResponseTemplate::new(200).set_body_string("invalid JSON")
})
.expect(1)
.mount(&server)
.await;
let error = persistor
.refresh_if_needed()
.await
.expect_err("an unbound replacement must not enter the authorization manager");
assert!(is_authentication_required_error(&error));
let live = persistor
.stored_credentials()
.await
.expect("original credentials");
let mut expected_live = initial.clone();
expected_live.token_response.0.set_expires_in(None);
assert_eq!(live, expected_live);
let stored = load_oauth_tokens_from_file(&initial.server_name, &initial.url)?
.expect("the replacement must not be deleted or overwritten");
assert_tokens_match_without_expiry(&stored, &replacement);
server.verify().await;
}
Ok(())
}
#[tokio::test(flavor = "current_thread")]
async fn expired_refresh_failure_preserves_credentials_for_a_later_retry() -> Result<()> {
let (_env, server, initial) = test_context().await?;
save_oauth_tokens_to_file(&initial)?;
let persistor = persistor_for(&initial).await?;
let failure = Mock::given(method("POST"))
.and(path("/oauth/token"))
.respond_with(ResponseTemplate::new(200).set_body_string("invalid JSON"))
.expect(1)
.mount_as_scoped(&server)
.await;
let error = persistor
.refresh_if_needed()
.await
.expect_err("refresh failed");
assert!(is_authentication_required_error(&error));
let stored = load_oauth_tokens_from_file(&initial.server_name, &initial.url)?
.expect("failed refresh must preserve stored credentials");
assert_tokens_match_without_expiry(&stored, &initial);
drop(failure);
Mock::given(method("POST"))
.and(path("/oauth/token"))
.and(body_string_contains("refresh_token=refresh-token"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"access_token": "retry-access-token",
"token_type": "Bearer",
"expires_in": 3600,
})))
.expect(1)
.mount(&server)
.await;
persistor.refresh_if_needed().await?;
let stored = load_oauth_tokens_from_file(&initial.server_name, &initial.url)?
.expect("successful retry must persist new credentials");
assert_eq!(
stored.token_response.0.access_token().secret(),
"retry-access-token"
);
server.verify().await;
Ok(())
}
#[tokio::test(flavor = "current_thread")]
async fn provider_timeout_releases_lock_and_preserves_durable_credentials() -> Result<()> {
let (_env, server, initial) = test_context().await?;
@@ -653,6 +793,7 @@ async fn provider_timeout_releases_lock_and_preserves_durable_credentials() -> R
.await
.expect_err("provider request should reach its explicit timeout");
assert!(error.to_string().contains("timed out after 50ms"));
assert!(is_authentication_required_error(&error));
let _lock = tokio::time::timeout(
Duration::from_millis(/*millis*/ 100),
+24 -3
View File
@@ -92,6 +92,7 @@ use crate::oauth::validate_refresh_token_issuer;
use crate::oauth_http_client::OAuthHttpClientAdapter;
use crate::oauth_refresh_mode::McpOAuthRefreshMode;
use crate::protocol_mode::McpProtocolMode;
use crate::startup_error::is_authentication_required_error;
use crate::stdio_server_launcher::StdioServerCommand;
use crate::stdio_server_launcher::StdioServerLauncher;
use crate::stdio_server_launcher::StdioServerProcessHandle;
@@ -798,7 +799,27 @@ impl RmcpClient {
meta: Option<serde_json::Value>,
timeout: Option<Duration>,
) -> Result<CallToolResult> {
self.refresh_oauth_if_needed().await?;
let authentication_required_result = |error| {
if !is_authentication_required_error(&error) {
return Err(error);
}
// Local expiry and server rejection use the same reconnect signal without
// exposing token-endpoint or transport details in the tool result.
let mut result = CallToolResult::error(vec![ContentBlock::text(
"MCP authentication required. Reconnect to continue using this server.",
)]);
result.meta = Some(
serde_json::Map::from_iter([(
"mcp/www_authenticate".to_string(),
Value::String("Bearer error=\"invalid_token\"".to_string()),
)])
.into(),
);
Ok(result)
};
if let Err(error) = self.refresh_oauth_if_needed().await {
return authentication_required_result(error);
}
let arguments = match arguments {
Some(Value::Object(map)) => Some(map),
Some(other) => {
@@ -863,14 +884,14 @@ impl RmcpClient {
let Some(ClientOperationError::Service(ServiceError::TransportSend(transport))) =
error.downcast_ref()
else {
return Err(error);
return authentication_required_result(error);
};
let Some(StreamableHttpError::AuthRequired(challenge)) =
transport
.error
.downcast_ref::<StreamableHttpError<StreamableHttpClientAdapterError>>()
else {
return Err(error);
return authentication_required_result(error);
};
// The transport has already handled automatic refresh. Preserve the challenge
// for interactive login without replaying the rejected tool call.