Files
codex/codex-rs/codex-mcp/src/parent_socket.rs
T
Charlie Marsh b0b5cfcab8 Connect plugin MCP servers over launcher-provided Unix sockets (#64828)
## Why

A launcher that already hosts an MCP server needs a way for Codex to
use it without starting a separate subprocess for the plugin.

## What changed

The stdio app server accepts socket bindings from its immediate parent
on Unix. A binding applies to a local plugin's stdio MCP server only
when the plugin ID, server name, and binding ID match a single entry.
Codex uses the stdio wire protocol over the socket and retains the
original declaration for policy checks.

Before sending MCP traffic, Codex requires a user-owned socket
directory with mode `0700` and verifies the listener's PID and UID.

## Testing

Add tests for binding scope and ambiguous matches, and for rejecting
unexpected listener PIDs, insecure directory permissions, symlinked
socket directories, and relative socket paths.

GitOrigin-RevId: 3d669dd59a0812b04727e4a1e458774007f054ba
2026-11-30 23:34:32 +00:00

87 lines
2.7 KiB
Rust

//! Match launcher-supplied MCP endpoints to host plugins' stdio declarations.
//!
//! The launcher supplies one binding object or an array in `CODEX_MCP_PARENT_BINDING`.
//! Each resolved registration needs exactly one matching endpoint. Policy operates
//! on the original stdio declaration, including the launch command identity.
use std::sync::Arc;
use codex_config::McpServerTransportConfig;
use codex_rmcp_client::ParentSocket;
use serde::Deserialize;
#[cfg(any(unix, test))]
use serde_json::Value;
use crate::McpServerSource;
use crate::ResolvedMcpServer;
#[derive(Debug, Deserialize)]
struct Binding {
plugin_id: String,
server_name: String,
id: String,
#[serde(flatten)]
socket: ParentSocket,
}
/// Host plugin endpoints captured once by a stdio app-server. Other runtimes default to none.
#[derive(Debug, Clone, Default)]
pub struct ParentMcpBindings(Arc<[Binding]>);
impl ParentMcpBindings {
/// Ignore inherited bindings unless the process that advertised them is our immediate parent.
pub fn from_launcher() -> Self {
#[cfg(unix)]
if let Ok(pid) = std::env::var("CODEX_MCP_PARENT_PID")
&& pid.parse::<u32>().ok() == Some(std::os::unix::process::parent_id())
&& let Ok(json) = std::env::var("CODEX_MCP_PARENT_BINDING")
&& let Ok(value) = serde_json::from_str(&json)
{
return Self::from_value(value);
}
Self::default()
}
#[cfg(any(unix, test))]
fn from_value(value: Value) -> Self {
let entries = match value {
Value::Array(entries) => entries,
single => vec![single],
};
Self(
entries
.into_iter()
.filter_map(|entry| serde_json::from_value(entry).ok())
.collect(),
)
}
pub(crate) fn for_registration(
&self,
registration: &ResolvedMcpServer,
) -> Option<ParentSocket> {
let McpServerSource::Plugin(plugin) = registration.source() else {
return None;
};
let config = registration.config();
let McpServerTransportConfig::Stdio { env: Some(env), .. } = &config.transport else {
return None;
};
if !config.is_local_environment() {
return None;
}
let id = env.get("CODEX_MCP_PARENT_BINDING_ID")?;
let mut matches = self.0.iter().filter(|binding| {
plugin.plugin_id() == binding.plugin_id
&& registration.name() == binding.server_name
&& *id == binding.id
});
let binding = matches.next()?;
matches.next().is_none().then(|| binding.socket.clone())
}
}
#[cfg(test)]
#[path = "parent_socket_tests.rs"]
mod tests;