mirror of
https://github.com/openai/codex.git
synced 2026-11-17 14:58:36 +00:00
[5/6] Wire executor-backed MCP stdio (#18212)
## Summary - Add the executor-backed RMCP stdio transport. - Wire MCP stdio placement through the executor environment config. - Cover local and executor-backed stdio paths with the existing MCP test helpers. ## Stack ```text o #18027 [6/6] Fail exec client operations after disconnect │ @ #18212 [5/6] Wire executor-backed MCP stdio │ o #18087 [4/6] Abstract MCP stdio server launching │ o #18020 [3/6] Add pushed exec process events │ o #18086 [2/6] Support piped stdin in exec process API │ o #18085 [1/6] Add MCP server environment config │ o main ``` --------- Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
1 parent
e3f44ca3b3
commit
996aa23e4c
31 files changed
+1815
-76
No files matched your search
@@ -1,4 +1,6 @@
|
||||
use anyhow::Result;
|
||||
use anyhow::anyhow;
|
||||
use codex_config::types::McpServerEnvVar;
|
||||
use reqwest::ClientBuilder;
|
||||
use reqwest::header::HeaderMap;
|
||||
use reqwest::header::HeaderName;
|
||||
@@ -9,17 +11,52 @@ use std::ffi::OsString;
|
||||
|
||||
pub(crate) fn create_env_for_mcp_server(
|
||||
extra_env: Option<HashMap<OsString, OsString>>,
|
||||
env_vars: &[String],
|
||||
) -> HashMap<OsString, OsString> {
|
||||
DEFAULT_ENV_VARS
|
||||
env_vars: &[McpServerEnvVar],
|
||||
) -> Result<HashMap<OsString, OsString>> {
|
||||
let additional_env_vars = local_stdio_env_var_names(env_vars)?;
|
||||
let env = DEFAULT_ENV_VARS
|
||||
.iter()
|
||||
.copied()
|
||||
.chain(env_vars.iter().map(String::as_str))
|
||||
.chain(additional_env_vars)
|
||||
.filter_map(|var| env::var_os(var).map(|value| (OsString::from(var), value)))
|
||||
.chain(extra_env.unwrap_or_default())
|
||||
.collect();
|
||||
Ok(env)
|
||||
}
|
||||
|
||||
pub(crate) fn create_env_overlay_for_remote_mcp_server(
|
||||
extra_env: Option<HashMap<OsString, OsString>>,
|
||||
env_vars: &[McpServerEnvVar],
|
||||
) -> HashMap<OsString, OsString> {
|
||||
// Remote stdio should inherit PATH/HOME/etc. from the executor side, not
|
||||
// from the orchestrator process. Only forward variables explicitly named
|
||||
// by the MCP config plus literal env overrides from that config.
|
||||
env_vars
|
||||
.iter()
|
||||
.filter(|var| !var.is_remote_source())
|
||||
.filter_map(|var| env::var_os(var.name()).map(|value| (OsString::from(var.name()), value)))
|
||||
.chain(extra_env.unwrap_or_default())
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) fn remote_mcp_env_var_names(env_vars: &[McpServerEnvVar]) -> Vec<String> {
|
||||
env_vars
|
||||
.iter()
|
||||
.filter(|var| var.is_remote_source())
|
||||
.map(|var| var.name().to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn local_stdio_env_var_names(env_vars: &[McpServerEnvVar]) -> Result<impl Iterator<Item = &str>> {
|
||||
if let Some(remote_var) = env_vars.iter().find(|var| var.is_remote_source()) {
|
||||
return Err(anyhow!(
|
||||
"env_vars entry `{}` uses source `remote`, which requires remote MCP stdio",
|
||||
remote_var.name()
|
||||
));
|
||||
}
|
||||
Ok(env_vars.iter().map(McpServerEnvVar::name))
|
||||
}
|
||||
|
||||
pub(crate) fn build_default_headers(
|
||||
http_headers: Option<HashMap<String, String>>,
|
||||
env_http_headers: Option<HashMap<String, String>>,
|
||||
@@ -182,7 +219,8 @@ mod tests {
|
||||
let env = create_env_for_mcp_server(
|
||||
Some(HashMap::from([(OsString::from("TZ"), expected.clone())])),
|
||||
&[],
|
||||
);
|
||||
)
|
||||
.expect("local MCP env should build");
|
||||
assert_eq!(env.get(OsStr::new("TZ")), Some(&expected));
|
||||
}
|
||||
|
||||
@@ -193,10 +231,92 @@ mod tests {
|
||||
let value = "from-env";
|
||||
let expected = OsString::from(value);
|
||||
let _guard = EnvVarGuard::set(custom_var, value);
|
||||
let env = create_env_for_mcp_server(/*extra_env*/ None, &[custom_var.to_string()]);
|
||||
let env = create_env_for_mcp_server(/*extra_env*/ None, &[custom_var.into()])
|
||||
.expect("local MCP env should build");
|
||||
assert_eq!(env.get(OsStr::new(custom_var)), Some(&expected));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial(extra_rmcp_env)]
|
||||
fn create_remote_env_overlay_only_forwards_explicit_variables() {
|
||||
let default_var = DEFAULT_ENV_VARS[0];
|
||||
let custom_var = "EXTRA_REMOTE_RMCP_ENV";
|
||||
let custom_value = OsString::from("from-env");
|
||||
let _default_guard = EnvVarGuard::set(default_var, "from-default");
|
||||
let _custom_guard = EnvVarGuard::set(custom_var, &custom_value);
|
||||
|
||||
let env =
|
||||
create_env_overlay_for_remote_mcp_server(/*extra_env*/ None, &[custom_var.into()]);
|
||||
|
||||
assert_eq!(
|
||||
env,
|
||||
HashMap::from([(OsString::from(custom_var), custom_value)])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial(extra_rmcp_env)]
|
||||
fn create_remote_env_overlay_does_not_copy_remote_source_variables() {
|
||||
let remote_var = "REMOTE_ONLY_RMCP_ENV";
|
||||
let local_var = "LOCAL_RMCP_ENV";
|
||||
let local_value = OsString::from("from-local-env");
|
||||
let _remote_guard = EnvVarGuard::set(remote_var, "should-not-be-copied");
|
||||
let _local_guard = EnvVarGuard::set(local_var, &local_value);
|
||||
|
||||
let env = create_env_overlay_for_remote_mcp_server(
|
||||
/*extra_env*/ None,
|
||||
&[
|
||||
McpServerEnvVar::Config {
|
||||
name: remote_var.to_string(),
|
||||
source: Some("remote".to_string()),
|
||||
},
|
||||
McpServerEnvVar::Config {
|
||||
name: local_var.to_string(),
|
||||
source: Some("local".to_string()),
|
||||
},
|
||||
],
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
env,
|
||||
HashMap::from([(OsString::from(local_var), local_value)])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remote_mcp_env_var_names_returns_remote_source_names() {
|
||||
let names = remote_mcp_env_var_names(&[
|
||||
"LEGACY".into(),
|
||||
McpServerEnvVar::Config {
|
||||
name: "LOCAL".to_string(),
|
||||
source: Some("local".to_string()),
|
||||
},
|
||||
McpServerEnvVar::Config {
|
||||
name: "REMOTE".to_string(),
|
||||
source: Some("remote".to_string()),
|
||||
},
|
||||
]);
|
||||
|
||||
assert_eq!(names, vec!["REMOTE".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_local_env_rejects_remote_source_variables() {
|
||||
let err = create_env_for_mcp_server(
|
||||
/*extra_env*/ None,
|
||||
&[McpServerEnvVar::Config {
|
||||
name: "REMOTE".to_string(),
|
||||
source: Some("remote".to_string()),
|
||||
}],
|
||||
)
|
||||
.expect_err("remote source should require remote stdio");
|
||||
|
||||
assert!(
|
||||
err.to_string().contains("requires remote MCP stdio"),
|
||||
"unexpected error: {err}"
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
#[serial(extra_rmcp_env)]
|
||||
@@ -207,7 +327,8 @@ mod tests {
|
||||
let expected = raw_path.to_os_string();
|
||||
let _guard = EnvVarGuard::set("PATH", raw_path);
|
||||
|
||||
let env = create_env_for_mcp_server(/*extra_env*/ None, &[]);
|
||||
let env =
|
||||
create_env_for_mcp_server(/*extra_env*/ None, &[]).expect("local MCP env should build");
|
||||
|
||||
assert_eq!(env.get(OsStr::new("PATH")), Some(&expected));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user