Skip to content

Commit 32a9b9f

Browse files
committed
Implement proper fix: also set the token expiration time when it's expired
The old load path in codex-rs/rmcp-client/src/oauth.rs only set expires_in when the stored expiry was in the future: // codex-rs/rmcp-client/src/oauth.rs:369-374 (original) if let Some(expires_at) = entry.expires_at && let Some(seconds) = expires_in_from_timestamp(expires_at) { let duration = Duration::from_secs(seconds); token_response.set_expires_in(Some(&duration)); } And expires_in_from_timestamp returned None for expired tokens: // codex-rs/rmcp-client/src/oauth.rs:444-453 (original) if expires_at <= now_ms { None } else { Some((expires_at - now_ms) / 1000) } So when the stored token was already expired, set_expires_in was never called, RMCP saw “no expiry,” and it didn’t auto-refresh on handshake.
1 parent 3e491df commit 32a9b9f

File tree

2 files changed

+2
-20
lines changed

2 files changed

+2
-20
lines changed

codex-rs/rmcp-client/src/oauth.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,8 @@ fn load_oauth_tokens_from_file(server_name: &str, url: &str) -> Result<Option<St
366366
token_response.set_scopes(Some(scopes.into_iter().map(Scope::new).collect()));
367367
}
368368

369-
if let Some(expires_at) = entry.expires_at
370-
&& let Some(seconds) = expires_in_from_timestamp(expires_at)
371-
{
369+
if let Some(expires_at) = entry.expires_at {
370+
let seconds = expires_in_from_timestamp(expires_at).unwrap_or(0);
372371
let duration = Duration::from_secs(seconds);
373372
token_response.set_expires_in(Some(&duration));
374373
}

codex-rs/rmcp-client/src/rmcp_client.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::time::Duration;
99
use anyhow::Result;
1010
use anyhow::anyhow;
1111
use futures::FutureExt;
12-
use oauth2::TokenResponse;
1312
use mcp_types::CallToolRequestParams;
1413
use mcp_types::CallToolResult;
1514
use mcp_types::InitializeRequestParams;
@@ -57,8 +56,6 @@ use crate::utils::convert_to_rmcp;
5756
use crate::utils::create_env_for_mcp_server;
5857
use crate::utils::run_with_timeout;
5958

60-
const REFRESH_SKEW_SECS: u64 = 60;
61-
6259
enum PendingTransport {
6360
ChildProcess(TokioChildProcess),
6461
StreamableHttp {
@@ -401,13 +398,6 @@ async fn create_oauth_transport_and_runtime(
401398
let auth_client = AuthClient::new(http_client, manager);
402399
let auth_manager = auth_client.auth_manager.clone();
403400

404-
// If the stored token is expired or about to expire, refresh before the handshake.
405-
if should_refresh_initial_token(&initial_tokens.token_response.0) {
406-
if let Err(err) = auth_manager.lock().await.refresh_token().await {
407-
warn!("failed to refresh OAuth token before handshake: {err}");
408-
}
409-
}
410-
411401
let transport = StreamableHttpClientTransport::with_client(
412402
auth_client,
413403
StreamableHttpClientTransportConfig::with_uri(url.to_string()),
@@ -423,10 +413,3 @@ async fn create_oauth_transport_and_runtime(
423413

424414
Ok((transport, runtime))
425415
}
426-
427-
fn should_refresh_initial_token(token: &OAuthTokenResponse) -> bool {
428-
match token.expires_in() {
429-
Some(duration) => duration.as_secs() <= REFRESH_SKEW_SECS,
430-
None => token.refresh_token().is_some(),
431-
}
432-
}

0 commit comments

Comments
 (0)