Enforce environment MCP policies (#39335)

## What changed

- Add environment-provided MCP restrictions for configured and plugin-provided servers.
- Disable attachment-scoped servers while their environment configuration is pending, failed, or unselected, while preserving selected-plugin access and the controller-owned Apps server.
- Apply the resolved policy to runtime startup, model tool exposure, telemetry, OAuth flows, and skill dependency installation.

## Testing

- Cover policy filtering for configured and plugin servers, environment state transitions, Apps ownership, and managed OAuth rejection.

GitOrigin-RevId: 7ca5d4dda14068e758497f88835d5cba685e159e
This commit is contained in:
sayan-oai authored and copyberry committed 2026-08-19 01:26:16 +00:00
1 parent d35e5495f9
commit fde2156057
36 files changed
+650 -88

No files matched your search

+41 -25
View File
@@ -66,6 +66,9 @@ pub(crate) async fn maybe_prompt_and_install_mcp_dependencies(
}
let unprompted_missing = filter_prompted_mcp_dependencies(sess, &missing).await;
// Do not prompt for servers that managed or attachment policy would reject.
let unprompted_missing =
admit_mcp_dependencies(sess, config.as_ref(), unprompted_missing).await;
if unprompted_missing.is_empty() {
return;
}
@@ -73,39 +76,54 @@ pub(crate) async fn maybe_prompt_and_install_mcp_dependencies(
if should_install_mcp_dependencies(sess, turn_context, &unprompted_missing, cancellation_token)
.await
{
maybe_install_mcp_dependencies(
sess,
turn_context,
config.as_ref(),
mentioned_skills,
elicitation_reviewer,
)
.await;
// Policy may have changed while waiting for the installation prompt.
let missing = admit_mcp_dependencies(sess, config.as_ref(), unprompted_missing).await;
maybe_install_mcp_dependencies(sess, turn_context, missing, elicitation_reviewer).await;
}
}
pub(crate) async fn maybe_install_mcp_dependencies(
// Resolve proposed servers through the existing policy-aware catalog before installing them.
async fn admit_mcp_dependencies(
sess: &Session,
turn_context: &TurnContext,
config: &crate::config::Config,
mentioned_skills: &[SkillMetadata],
elicitation_reviewer: Option<ElicitationReviewerHandle>,
) {
if mentioned_skills.is_empty()
|| !config
.features
.enabled(codex_features::Feature::SkillMcpDependencyInstall)
{
return;
mut candidates: HashMap<String, McpServerConfig>,
) -> HashMap<String, McpServerConfig> {
if candidates.is_empty() {
return candidates;
}
let codex_home = config.codex_home.clone();
let installed = sess.runtime_mcp_servers(config).await;
let missing = collect_missing_mcp_dependencies(mentioned_skills, &installed);
// Stage candidates in memory so global requirements apply before catalog resolution.
let mut candidate_config = config.clone();
let mut servers = config.mcp_servers.get().clone();
servers.extend(candidates.clone());
if let Err(err) = candidate_config.mcp_servers.set(servers) {
warn!("failed to validate MCP dependencies for mentioned skills: {err}");
return HashMap::new();
}
// Keep only the same candidate when attachment policy leaves it enabled.
let catalog = sess.runtime_mcp_config(&candidate_config).await;
candidates.retain(|name, config| {
catalog
.mcp_server_catalog
.server(name)
.is_some_and(|server| server.config().enabled && server.config() == config)
});
candidates
}
async fn maybe_install_mcp_dependencies(
sess: &Session,
turn_context: &TurnContext,
missing: HashMap<String, McpServerConfig>,
elicitation_reviewer: Option<ElicitationReviewerHandle>,
) {
if missing.is_empty() {
return;
}
let config = turn_context.config.as_ref();
let codex_home = config.codex_home.clone();
let mut servers = match load_global_mcp_servers(&codex_home).await {
Ok(servers) => servers,
Err(err) => {
@@ -114,7 +132,6 @@ pub(crate) async fn maybe_install_mcp_dependencies(
}
};
let mut updated = false;
let mut added = Vec::new();
for (name, config) in missing {
if servers.contains_key(&name) {
@@ -122,10 +139,9 @@ pub(crate) async fn maybe_install_mcp_dependencies(
}
servers.insert(name.clone(), config.clone());
added.push((name, config));
updated = true;
}
if !updated {
if added.is_empty() {
return;
}