mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
Require a local runtime for MCP HTTP headers helpers (#65119)
## Why MCP servers can use the host's HTTP client without a local runtime. That fallback also let locally configured `http_headers_helper` commands run on the host when only remote executors were attached. ## What changed Reject locally configured HTTP headers helpers unless the host has a local runtime. A host that retains its startup runtime can still run helpers when it exposes only remote executors. HTTP servers without a headers helper can still use the host's HTTP client. ## Testing Cover helper rejection with no executors and with both laptop and cloud executors attached. Verify that the helper command leaves no marker file, local stdio startup still fails, and plain HTTP still works. Cover helper client resolution when a local runtime is available, including when only remote executors are exposed. GitOrigin-RevId: 0aff6cc1f9f6122aace54ed4c19ef93e26817c64
This commit is contained in:
@@ -59,8 +59,10 @@ use codex_protocol::mcp::ClientMcpExtensions;
|
||||
use codex_protocol::mcp::McpServerInfo;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::EnvironmentConfigState;
|
||||
use codex_protocol::protocol::GranularApprovalConfig;
|
||||
use codex_protocol::protocol::McpStartupFailureReason;
|
||||
use codex_protocol::protocol::TurnEnvironmentSelection;
|
||||
use codex_rmcp_client::ElicitationResponse;
|
||||
use codex_rmcp_client::InProcessTransportFactory;
|
||||
use codex_rmcp_client::McpAuthState;
|
||||
@@ -5417,9 +5419,53 @@ async fn executor_owned_chatgpt_mcp_accepts_only_safe_explicit_authorization() -
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn no_local_runtime_fails_local_stdio_but_keeps_local_http_server() {
|
||||
async fn no_local_runtime_rejects_mcp_commands_but_keeps_plain_http() {
|
||||
assert_host_mcp_commands_blocked(&[]).await;
|
||||
}
|
||||
|
||||
/// Both executors are remote from the worker, even when one runs on the user's laptop.
|
||||
#[tokio::test]
|
||||
async fn laptop_and_cloud_executors_cannot_run_mcp_commands_on_the_host() {
|
||||
assert_host_mcp_commands_blocked(&[
|
||||
("laptop", "ws://127.0.0.1:1"),
|
||||
("cloud", "ws://127.0.0.1:2"),
|
||||
])
|
||||
.await;
|
||||
}
|
||||
|
||||
async fn assert_host_mcp_commands_blocked(executors: &[(&str, &str)]) {
|
||||
let codex_home = tempdir().expect("tempdir");
|
||||
let environments = Arc::new(environment_manager_without_environments());
|
||||
let mut selections = Vec::new();
|
||||
let mut ready = HashMap::new();
|
||||
for &(id, url) in executors {
|
||||
environments
|
||||
.upsert_environment(id.to_owned(), url.to_owned(), /*connect_timeout*/ None)
|
||||
.expect("register executor");
|
||||
let executor = environments.get_environment(id).expect("attached executor");
|
||||
assert!(executor.is_remote());
|
||||
ready.insert(id.to_owned(), executor);
|
||||
selections.push(TurnEnvironmentSelection {
|
||||
environment_id: id.to_owned(),
|
||||
cwd: PathUri::parse("file:///workspace").expect("executor workspace"),
|
||||
workspace_roots: Vec::new(),
|
||||
config: EnvironmentConfigState::FromThread,
|
||||
selected_capability_roots: Default::default(),
|
||||
});
|
||||
}
|
||||
let runtime_context = McpRuntimeContext::new(environments, codex_home.path().to_path_buf())
|
||||
.with_selected_environments(selections.into(), ready);
|
||||
let mcp_servers = HashMap::from([
|
||||
(
|
||||
McpServerKey::unscoped("helper"),
|
||||
EffectiveMcpServer::from_host_config(
|
||||
serde_json::from_value(serde_json::json!({
|
||||
"url": "http://127.0.0.1:1",
|
||||
"http_headers_helper": "echo {} > helper-ran && echo {}",
|
||||
}))
|
||||
.expect("HTTP server with an implicit local environment"),
|
||||
),
|
||||
),
|
||||
(
|
||||
McpServerKey::unscoped("stdio"),
|
||||
EffectiveMcpServer::from_host_config(McpServerConfig {
|
||||
@@ -5502,10 +5548,7 @@ async fn no_local_runtime_fails_local_stdio_but_keeps_local_http_server() {
|
||||
submit_id: String::new(),
|
||||
tx_event: None,
|
||||
startup_cancellation_token: cancel_token.clone(),
|
||||
runtime_context: McpRuntimeContext::new(
|
||||
Arc::new(environment_manager_without_environments()),
|
||||
PathBuf::from("/tmp"),
|
||||
),
|
||||
runtime_context,
|
||||
codex_apps_tools_cache: ConnectorRuntimeManager::<ToolInfo>::default(),
|
||||
tool_catalog_cache: McpToolCatalogCache::default(),
|
||||
codex_apps_tools_cache_key: ConnectorRuntimeContextKey::personal(
|
||||
@@ -5521,6 +5564,15 @@ async fn no_local_runtime_fails_local_stdio_but_keeps_local_http_server() {
|
||||
)
|
||||
.await;
|
||||
|
||||
let outcome = manager.test_client("helper").client().await;
|
||||
assert!(!codex_home.path().join("helper-ran").exists());
|
||||
let Err(StartupOutcomeError::Failed { error, .. }) = outcome else {
|
||||
panic!("HTTP helper startup should fail without a local runtime");
|
||||
};
|
||||
assert_eq!(
|
||||
error,
|
||||
"HTTP headers helper for MCP server `helper` requires a local runtime"
|
||||
);
|
||||
assert!(manager.contains_server("stdio"));
|
||||
assert!(manager.contains_server("http"));
|
||||
assert!(
|
||||
|
||||
@@ -1321,9 +1321,23 @@ impl McpRuntimeContext {
|
||||
server_name: &str,
|
||||
config: &codex_config::McpServerConfig,
|
||||
) -> Result<Option<Arc<Environment>>, String> {
|
||||
// Resolve `"local"` through the shared registry when available. Local
|
||||
// HTTP is the one current exception: it can use the ambient HTTP client
|
||||
// even when no local Environment is configured.
|
||||
// Ambient HTTP and dynamically attached executors do not grant permission
|
||||
// to run a headers helper on the host.
|
||||
if config.is_local_environment()
|
||||
&& matches!(
|
||||
config.transport,
|
||||
codex_config::McpServerTransportConfig::StreamableHttp {
|
||||
http_headers_helper: Some(_),
|
||||
..
|
||||
}
|
||||
)
|
||||
&& !self.environment_manager.has_local_runtime()
|
||||
{
|
||||
return Err(format!(
|
||||
"HTTP headers helper for MCP server `{server_name}` requires a local runtime"
|
||||
));
|
||||
}
|
||||
|
||||
if let Some(environment) = self
|
||||
.ready_environments
|
||||
.get(&config.environment_id)
|
||||
@@ -1336,6 +1350,7 @@ impl McpRuntimeContext {
|
||||
return Ok(Some(environment));
|
||||
}
|
||||
|
||||
// HTTP can use the ambient client without a registered local executor.
|
||||
if config.is_local_environment() {
|
||||
return match config.transport {
|
||||
codex_config::McpServerTransportConfig::Stdio { .. } => Err(format!(
|
||||
@@ -1652,6 +1667,42 @@ mod tests {
|
||||
assert!(resolved_runtime.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn http_headers_helper_requires_a_host_runtime() -> anyhow::Result<()> {
|
||||
let config: McpServerConfig = serde_json::from_value(serde_json::json!({
|
||||
"url": "http://127.0.0.1:1",
|
||||
"http_headers_helper": "echo {}",
|
||||
}))
|
||||
.expect("HTTP server with an implicit local environment");
|
||||
let remote_url = "ws://127.0.0.1:1".to_string();
|
||||
let hosted = environment_manager_without_environments();
|
||||
hosted.upsert_environment(
|
||||
"remote".to_string(),
|
||||
remote_url.clone(),
|
||||
/*connect_timeout*/ None,
|
||||
)?;
|
||||
let hosted = McpRuntimeContext::new(Arc::new(hosted), std::env::temp_dir());
|
||||
assert_eq!(
|
||||
hosted.resolve_http_client("http", &config).err().as_deref(),
|
||||
Some("HTTP headers helper for MCP server `http` requires a local runtime")
|
||||
);
|
||||
|
||||
let paths = codex_exec_server::ExecServerRuntimeOptions::new(
|
||||
std::env::current_exe()?,
|
||||
/*codex_linux_sandbox_exe*/ None,
|
||||
)?;
|
||||
let remote_only = EnvironmentManager::create_for_tests(Some(remote_url), Some(paths)).await;
|
||||
assert!(remote_only.try_local_environment().is_none());
|
||||
for manager in [EnvironmentManager::default_for_tests(), remote_only] {
|
||||
let local = McpRuntimeContext::new(Arc::new(manager), std::env::temp_dir());
|
||||
let client = local
|
||||
.resolve_http_client("http", &config)
|
||||
.expect("a local runtime permits the helper");
|
||||
assert!(!Arc::ptr_eq(&client, &local.local_http_client()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn local_http_client_is_shared_across_resolution_and_context_clones() {
|
||||
for environment_manager in [
|
||||
|
||||
@@ -409,6 +409,12 @@ impl EnvironmentManager {
|
||||
self.local_environment.as_ref().map(Arc::clone)
|
||||
}
|
||||
|
||||
/// Whether the host configured a local runtime at startup. A local app can
|
||||
/// retain this runtime even when it exposes only remote executors.
|
||||
pub fn has_local_runtime(&self) -> bool {
|
||||
self.local_environment.is_some() || self.local_runtime_paths.is_some()
|
||||
}
|
||||
|
||||
/// Returns a named environment instance.
|
||||
pub fn get_environment(&self, environment_id: &str) -> Option<Arc<Environment>> {
|
||||
self.environments
|
||||
|
||||
Reference in New Issue
Block a user