Add remote runtime OAuth for MCP

Route Streamable HTTP OAuth discovery, registration, token exchange, and refresh through the selected MCP runtime HTTP client so remote MCP servers do not silently fall back to local network access. Keep browser launch and callback handling local while storing tokens in the existing local MCP OAuth store.

Add regression coverage for injected OAuth HTTP requests and stored-token refresh through the exec-server transport.

Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
Ahmed Ibrahim
2026-04-27 09:33:42 +00:00
co-authored by Codex
parent 4f1d5f00f0
commit 177e39f4b7
22 changed files with 1500 additions and 350 deletions
+8 -5
View File
@@ -253,20 +253,23 @@ pub async fn list_accessible_connectors_from_mcp_tools_with_environment_manager(
});
}
let environment = environment_manager
.default_environment()
.unwrap_or_else(|| environment_manager.local_environment());
let runtime_environment =
McpRuntimeEnvironment::new(environment.clone(), config.cwd.to_path_buf());
let auth_status_entries = compute_auth_statuses(
mcp_servers.iter(),
config.mcp_oauth_credentials_store_mode,
auth.as_ref(),
runtime_environment.clone(),
)
.await;
let (tx_event, rx_event) = unbounded();
drop(rx_event);
let environment = environment_manager
.default_environment()
.unwrap_or_else(|| environment_manager.local_environment());
let (mcp_connection_manager, cancel_token) = McpConnectionManager::new(
&mcp_servers,
config.mcp_oauth_credentials_store_mode,
@@ -275,7 +278,7 @@ pub async fn list_accessible_connectors_from_mcp_tools_with_environment_manager(
INITIAL_SUBMIT_ID.to_owned(),
tx_event,
PermissionProfile::default(),
McpRuntimeEnvironment::new(environment, config.cwd.to_path_buf()),
runtime_environment,
config.codex_home.to_path_buf(),
codex_apps_tools_cache_key(auth.as_ref()),
ToolPluginProvenance::default(),
+27 -8
View File
@@ -11,7 +11,7 @@ use codex_protocol::request_user_input::RequestUserInputArgs;
use codex_protocol::request_user_input::RequestUserInputQuestion;
use codex_protocol::request_user_input::RequestUserInputQuestionOption;
use codex_protocol::request_user_input::RequestUserInputResponse;
use codex_rmcp_client::perform_oauth_login;
use codex_rmcp_client::perform_oauth_login_with_client;
use tokio_util::sync::CancellationToken;
use tracing::warn;
@@ -20,6 +20,8 @@ use crate::session::session::Session;
use crate::session::turn_context::TurnContext;
use crate::skills::model::SkillToolDependency;
use codex_mcp::McpOAuthLoginSupport;
use codex_mcp::McpRuntimeEnvironment;
use codex_mcp::http_client_for_server;
use codex_mcp::mcp_permission_prompt_is_auto_approved;
use codex_mcp::oauth_login_support;
use codex_mcp::resolve_oauth_scopes;
@@ -126,14 +128,29 @@ pub(crate) async fn maybe_install_mcp_dependencies(
}
for (name, server_config) in added {
let oauth_config = match oauth_login_support(&server_config.transport).await {
McpOAuthLoginSupport::Supported(config) => config,
McpOAuthLoginSupport::Unsupported => continue,
McpOAuthLoginSupport::Unknown(err) => {
warn!("MCP server may or may not require login for dependency {name}: {err}");
let runtime_environment = McpRuntimeEnvironment::new(
turn_context
.environment
.clone()
.unwrap_or_else(|| sess.services.environment_manager.local_environment()),
turn_context.cwd.to_path_buf(),
);
let http_client = match http_client_for_server(&server_config, runtime_environment) {
Ok(http_client) => http_client,
Err(err) => {
warn!("failed to resolve MCP OAuth environment for dependency {name}: {err}");
continue;
}
};
let oauth_config =
match oauth_login_support(&server_config.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 dependency {name}: {err}");
continue;
}
};
sess.notify_background_event(
turn_context,
@@ -148,7 +165,7 @@ pub(crate) async fn maybe_install_mcp_dependencies(
server_config.scopes.clone(),
oauth_config.discovered_scopes.clone(),
);
let first_attempt = perform_oauth_login(
let first_attempt = perform_oauth_login_with_client(
&name,
&oauth_config.url,
config.mcp_oauth_credentials_store_mode,
@@ -158,6 +175,7 @@ pub(crate) async fn maybe_install_mcp_dependencies(
server_config.oauth_resource.as_deref(),
config.mcp_oauth_callback_port,
config.mcp_oauth_callback_url.as_deref(),
http_client.clone(),
)
.await;
@@ -171,7 +189,7 @@ pub(crate) async fn maybe_install_mcp_dependencies(
)
.await;
if let Err(err) = perform_oauth_login(
if let Err(err) = perform_oauth_login_with_client(
&name,
&oauth_config.url,
config.mcp_oauth_credentials_store_mode,
@@ -181,6 +199,7 @@ pub(crate) async fn maybe_install_mcp_dependencies(
server_config.oauth_resource.as_deref(),
config.mcp_oauth_callback_port,
config.mcp_oauth_callback_url.as_deref(),
http_client,
)
.await
{
+7
View File
@@ -32,6 +32,7 @@ use crate::tasks::UndoTask;
use crate::tasks::UserShellCommandMode;
use crate::tasks::UserShellCommandTask;
use crate::tasks::execute_user_shell_command;
use codex_mcp::McpRuntimeEnvironment;
use codex_mcp::collect_mcp_snapshot_from_manager;
use codex_mcp::compute_auth_statuses;
use codex_protocol::models::ContentItem;
@@ -538,12 +539,18 @@ pub async fn list_mcp_tools(sess: &Session, config: &Arc<Config>, sub_id: String
.mcp_manager
.effective_servers(config, auth.as_ref())
.await;
let environment = sess
.services
.environment_manager
.default_environment()
.unwrap_or_else(|| sess.services.environment_manager.local_environment());
let snapshot = collect_mcp_snapshot_from_manager(
&mcp_connection_manager,
compute_auth_statuses(
mcp_servers.iter(),
config.mcp_oauth_credentials_store_mode,
auth.as_ref(),
McpRuntimeEnvironment::new(environment, config.cwd.to_path_buf()),
)
.await,
)
+15 -9
View File
@@ -219,8 +219,20 @@ impl Session {
.tool_plugin_provenance(config.as_ref())
.await;
let mcp_servers = with_codex_apps_mcp(mcp_servers, auth.as_ref(), &mcp_config);
let auth_statuses =
compute_auth_statuses(mcp_servers.iter(), store_mode, auth.as_ref()).await;
let runtime_environment = McpRuntimeEnvironment::new(
turn_context
.environment
.clone()
.unwrap_or_else(|| self.services.environment_manager.local_environment()),
turn_context.cwd.to_path_buf(),
);
let auth_statuses = compute_auth_statuses(
mcp_servers.iter(),
store_mode,
auth.as_ref(),
runtime_environment.clone(),
)
.await;
{
let mut guard = self.services.mcp_startup_cancellation_token.lock().await;
guard.cancel();
@@ -234,13 +246,7 @@ impl Session {
turn_context.sub_id.clone(),
self.get_tx_event(),
turn_context.permission_profile(),
McpRuntimeEnvironment::new(
turn_context
.environment
.clone()
.unwrap_or_else(|| self.services.environment_manager.local_environment()),
turn_context.cwd.to_path_buf(),
),
runtime_environment,
config.codex_home.to_path_buf(),
codex_apps_tools_cache_key(auth.as_ref()),
tool_plugin_provenance,
+5
View File
@@ -445,15 +445,20 @@ impl Session {
let auth_manager_clone = Arc::clone(&auth_manager);
let config_for_mcp = Arc::clone(&config);
let mcp_manager_for_mcp = Arc::clone(&mcp_manager);
let environment_manager_for_mcp = Arc::clone(&environment_manager);
let auth_and_mcp_fut = async move {
let auth = auth_manager_clone.auth().await;
let mcp_servers = mcp_manager_for_mcp
.effective_servers(&config_for_mcp, auth.as_ref())
.await;
let environment = environment_manager_for_mcp
.default_environment()
.unwrap_or_else(|| environment_manager_for_mcp.local_environment());
let auth_statuses = compute_auth_statuses(
mcp_servers.iter(),
config_for_mcp.mcp_oauth_credentials_store_mode,
auth.as_ref(),
McpRuntimeEnvironment::new(environment, config_for_mcp.cwd.to_path_buf()),
)
.await;
(auth, mcp_servers, auth_statuses)