Move MCP OAuth orchestration downstream

Keep app-server OAuth callsites thin by moving server/runtime-aware OAuth discovery, scope resolution, and runtime HTTP client selection into codex-mcp helpers.

Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
Ahmed Ibrahim
2026-04-27 10:03:34 +00:00
co-authored by Codex
parent a4de53c661
commit a3abb8ba6e
7 changed files with 249 additions and 170 deletions
@@ -310,11 +310,9 @@ use codex_mcp::McpRuntimeEnvironment;
use codex_mcp::McpServerStatusSnapshot;
use codex_mcp::McpSnapshotDetail;
use codex_mcp::collect_mcp_server_status_snapshot_with_detail;
use codex_mcp::discover_supported_scopes;
use codex_mcp::effective_mcp_servers;
use codex_mcp::http_client_for_server;
use codex_mcp::perform_oauth_login_return_url_for_server;
use codex_mcp::read_mcp_resource as read_mcp_resource_without_thread;
use codex_mcp::resolve_oauth_scopes;
use codex_model_provider::ProviderAccountError;
use codex_model_provider::create_model_provider;
use codex_models_manager::collaboration_mode_presets::CollaborationModesConfig;
@@ -356,7 +354,6 @@ use codex_protocol::protocol::USER_MESSAGE_BEGIN;
use codex_protocol::protocol::W3cTraceContext;
use codex_protocol::user_input::MAX_USER_INPUT_TEXT_CHARS;
use codex_protocol::user_input::UserInput as CoreInputItem;
use codex_rmcp_client::perform_oauth_login_return_url_with_client;
use codex_rollout::state_db::StateDbHandle;
use codex_rollout::state_db::get_state_db;
use codex_rollout::state_db::reconcile_rollout;
@@ -5942,13 +5939,8 @@ impl CodexMessageProcessor {
return;
};
let (url, http_headers, env_http_headers) = match &server.transport {
McpServerTransportConfig::StreamableHttp {
url,
http_headers,
env_http_headers,
..
} => (url.clone(), http_headers.clone(), env_http_headers.clone()),
match &server.transport {
McpServerTransportConfig::StreamableHttp { .. } => {}
_ => {
let error = JSONRPCErrorError {
code: INVALID_REQUEST_ERROR_CODE,
@@ -5969,39 +5961,16 @@ impl CodexMessageProcessor {
config.cwd.to_path_buf(),
),
};
let http_client = match http_client_for_server(server, runtime_environment) {
Ok(http_client) => http_client,
Err(err) => {
let error = JSONRPCErrorError {
code: INVALID_REQUEST_ERROR_CODE,
message: format!("failed to resolve MCP server OAuth environment: {err}"),
data: None,
};
self.outgoing.send_error(request_id, error).await;
return;
}
};
let discovered_scopes = if scopes.is_none() && server.scopes.is_none() {
discover_supported_scopes(&server.transport, http_client.clone()).await
} else {
None
};
let resolved_scopes =
resolve_oauth_scopes(scopes, server.scopes.clone(), discovered_scopes);
match perform_oauth_login_return_url_with_client(
match perform_oauth_login_return_url_for_server(
&name,
&url,
server,
config.mcp_oauth_credentials_store_mode,
http_headers,
env_http_headers,
&resolved_scopes.scopes,
server.oauth_resource.as_deref(),
scopes,
timeout_secs,
config.mcp_oauth_callback_port,
config.mcp_oauth_callback_url.as_deref(),
http_client,
runtime_environment,
)
.await
{
@@ -5,14 +5,9 @@ use codex_app_server_protocol::McpServerOauthLoginCompletedNotification;
use codex_app_server_protocol::ServerNotification;
use codex_config::types::McpServerConfig;
use codex_core::config::Config;
use codex_mcp::McpOAuthLoginSupport;
use codex_mcp::McpOAuthLoginOutcome;
use codex_mcp::McpRuntimeEnvironment;
use codex_mcp::http_client_for_server;
use codex_mcp::oauth_login_support;
use codex_mcp::resolve_oauth_scopes;
use codex_mcp::should_retry_without_scopes;
use codex_rmcp_client::perform_oauth_login_silent_with_client;
use tracing::warn;
use codex_mcp::perform_oauth_login_silent_for_server;
use super::CodexMessageProcessor;
@@ -33,33 +28,6 @@ impl CodexMessageProcessor {
config.cwd.to_path_buf(),
),
};
let http_client = match http_client_for_server(&server, runtime_environment) {
Ok(http_client) => http_client,
Err(err) => {
warn!(
"failed to resolve MCP OAuth environment for plugin install {name}: {err}"
);
continue;
}
};
let oauth_config = match oauth_login_support(&server.transport, http_client.clone())
.await
{
McpOAuthLoginSupport::Supported(config) => config,
McpOAuthLoginSupport::Unsupported => continue,
McpOAuthLoginSupport::Unknown(err) => {
warn!(
"MCP server may or may not require login for plugin install {name}: {err}"
);
continue;
}
};
let resolved_scopes = resolve_oauth_scopes(
/*explicit_scopes*/ None,
server.scopes.clone(),
oauth_config.discovered_scopes.clone(),
);
let store_mode = config.mcp_oauth_credentials_store_mode;
let callback_port = config.mcp_oauth_callback_port;
@@ -68,41 +36,20 @@ impl CodexMessageProcessor {
let notification_name = name.clone();
tokio::spawn(async move {
let first_attempt = perform_oauth_login_silent_with_client(
let final_result = perform_oauth_login_silent_for_server(
&name,
&oauth_config.url,
&server,
store_mode,
oauth_config.http_headers.clone(),
oauth_config.env_http_headers.clone(),
&resolved_scopes.scopes,
server.oauth_resource.as_deref(),
/*explicit_scopes*/ None,
callback_port,
callback_url.as_deref(),
http_client.clone(),
runtime_environment,
)
.await;
let final_result = match first_attempt {
Err(err) if should_retry_without_scopes(&resolved_scopes, &err) => {
perform_oauth_login_silent_with_client(
&name,
&oauth_config.url,
store_mode,
oauth_config.http_headers,
oauth_config.env_http_headers,
&[],
server.oauth_resource.as_deref(),
callback_port,
callback_url.as_deref(),
http_client,
)
.await
}
result => result,
};
let (success, error) = match final_result {
Ok(()) => (true, None),
Ok(McpOAuthLoginOutcome::Completed) => (true, None),
Ok(McpOAuthLoginOutcome::Unsupported) => return,
Err(err) => (false, Some(err.to_string())),
};