mirror of
https://github.com/openai/codex.git
synced 2026-11-17 14:58:36 +00:00
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:
1 parent
7257826ab2
commit
b3ffe3d001
14 files changed
+408
-57
No files matched your search
@@ -15,7 +15,9 @@ use codex_connectors::ConnectorRuntimeContextKey;
|
||||
use codex_exec_server::Environment;
|
||||
use codex_login::CodexAuth;
|
||||
use codex_protocol::mcp::ClientMcpExtensions;
|
||||
use codex_rmcp_client::StoredOAuthCredentialSnapshot;
|
||||
use codex_rmcp_client::StoredOAuthTokens;
|
||||
use codex_rmcp_client::stored_oauth_credential_snapshot;
|
||||
use codex_rmcp_client::stored_oauth_credentials;
|
||||
use rmcp::model::ElicitationCapability;
|
||||
use tracing::warn;
|
||||
@@ -96,7 +98,7 @@ pub(crate) struct McpServerConnectionIdentity {
|
||||
transport: McpServerTransportConfig,
|
||||
environment_id: String,
|
||||
oauth_store: Option<(OAuthCredentialsStoreMode, AuthKeyringBackendKind)>,
|
||||
oauth_credentials: Result<Option<StoredOAuthTokens>, String>,
|
||||
oauth_credentials: Result<Option<StoredOAuthCredentialSnapshot>, String>,
|
||||
resolved_environment: Result<Option<Arc<Environment>>, String>,
|
||||
local_stdio_fallback_cwd: Option<PathBuf>,
|
||||
referenced_environment_variables: Vec<(String, Option<OsString>)>,
|
||||
@@ -143,7 +145,7 @@ impl McpServerConnectionIdentity {
|
||||
None
|
||||
};
|
||||
let oauth_credentials = stored_oauth_url.map_or(Ok(None), |url| {
|
||||
stored_oauth_credentials(
|
||||
stored_oauth_credential_snapshot(
|
||||
config.oauth_credential_name(server_name).as_ref(),
|
||||
url,
|
||||
store_mode,
|
||||
@@ -211,8 +213,52 @@ impl McpServerConnectionIdentity {
|
||||
&& self.agent_plugin == other.agent_plugin
|
||||
}
|
||||
|
||||
pub(crate) fn oauth_credentials(&self) -> Result<&Option<StoredOAuthTokens>, &String> {
|
||||
self.oauth_credentials.as_ref()
|
||||
pub(crate) fn oauth_credentials(&self) -> Result<Option<&StoredOAuthTokens>, &String> {
|
||||
self.oauth_credentials.as_ref().map(|credentials| {
|
||||
credentials
|
||||
.as_ref()
|
||||
.map(StoredOAuthCredentialSnapshot::credentials)
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn oauth_credentials_changed(
|
||||
&self,
|
||||
server_name: &str,
|
||||
config: &McpServerConfig,
|
||||
) -> bool {
|
||||
let Some((store_mode, keyring_backend_kind)) = self.oauth_store else {
|
||||
return false;
|
||||
};
|
||||
let McpServerTransportConfig::StreamableHttp { url, .. } = &self.transport else {
|
||||
return false;
|
||||
};
|
||||
|
||||
let credential_name = config.oauth_credential_name(server_name);
|
||||
let current_credentials = match self.oauth_credentials.as_ref() {
|
||||
Ok(Some(credentials)) => credentials.reload(
|
||||
credential_name.as_ref(),
|
||||
url,
|
||||
store_mode,
|
||||
keyring_backend_kind,
|
||||
),
|
||||
Ok(None) | Err(_) => stored_oauth_credentials(
|
||||
credential_name.as_ref(),
|
||||
url,
|
||||
store_mode,
|
||||
keyring_backend_kind,
|
||||
),
|
||||
};
|
||||
|
||||
match current_credentials {
|
||||
Ok(Some(current_credentials)) => {
|
||||
self.oauth_credentials() != Ok(Some(¤t_credentials))
|
||||
}
|
||||
Ok(None) => false,
|
||||
Err(error) => {
|
||||
warn!(server_name, %error, "failed to read stored MCP OAuth credentials");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user