Files
codex/codex-rs/codex-mcp/src/parent_socket_tests.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

117 lines
4.1 KiB
Rust

#![cfg(test)]
use super::*;
use crate::McpPluginAttribution;
use crate::McpServerRegistration;
use crate::ResolvedMcpCatalog;
use crate::mcp::effective_mcp_server;
use crate::mcp::tests::test_mcp_config;
use codex_config::McpServerConfig;
use pretty_assertions::assert_eq;
use serde_json::json;
#[test]
fn parent_binding_is_scoped_to_the_runtime_and_resolved_plugin_declaration() {
let endpoint = json!({
"plugin_id": "plugin@marketplace",
"server_name": "server",
"id": "v1",
"path": "/tmp/socket",
"pid": 1
});
let isolated = test_mcp_config(std::env::temp_dir());
let mut host = isolated.clone();
host.parent_bindings = ParentMcpBindings::from_value(endpoint.clone());
let config: McpServerConfig = serde_json::from_value(json!({
"command": "launcher",
"env": {"CODEX_MCP_PARENT_BINDING_ID":"v1"}
}))
.unwrap();
let plugin = |id: &str, config| {
McpServerRegistration::from_plugin(
"server".to_string(),
McpPluginAttribution::new(id.to_string(), "Plugin".to_string()),
/*plugin_order*/ 0,
config,
)
};
let resolve = |runtime: &crate::McpConfig, registrations: Vec<McpServerRegistration>| {
let mut builder = ResolvedMcpCatalog::builder();
for registration in registrations {
builder.register(registration);
}
let catalog = builder.build();
effective_mcp_server(
runtime,
/*auth*/ None,
catalog.server_by_name("server").unwrap(),
)
.unwrap()
.parent_socket
};
let original = plugin("plugin@marketplace", config.clone());
assert_eq!(
resolve(&host, vec![original.clone()]),
Some(serde_json::from_value(endpoint).unwrap())
);
assert_eq!(resolve(&isolated, vec![original.clone()]), None);
assert_eq!(
resolve(&host, vec![plugin("plugin@other", config.clone())]),
None
);
let mut old = config.clone();
if let McpServerTransportConfig::Stdio { env, .. } = &mut old.transport {
*env = None;
}
assert_eq!(
resolve(&host, vec![plugin("plugin@marketplace", old)]),
None
);
let shadow = McpServerRegistration::from_config("server".to_string(), config);
assert_eq!(resolve(&host, vec![original, shadow]), None);
}
#[test]
fn each_registration_requires_its_own_unique_match() {
let endpoint = |plugin, server, id, path| json!({"plugin_id": plugin, "server_name": server, "id": id, "path": path, "pid": 1});
let first = endpoint("plugin@market", "first", "v1", "/first");
let second = endpoint("plugin@market", "second", "v1", "/second");
let mut builder = ResolvedMcpCatalog::builder();
for name in ["first", "second"] {
let config = serde_json::from_value(json!({
"command": "launcher",
"env": {"CODEX_MCP_PARENT_BINDING_ID": "v1"}
}))
.unwrap();
builder.register(McpServerRegistration::from_plugin(
name.to_string(),
McpPluginAttribution::new("plugin@market".to_string(), "Plugin".to_string()),
/*plugin_order*/ 0,
config,
));
}
let catalog = builder.build();
let resolve = |bindings: &ParentMcpBindings, name| {
bindings.for_registration(catalog.server_by_name(name).unwrap())
};
let bindings = ParentMcpBindings::from_value(json!([
second,
endpoint("other@market", "first", "v1", "/other-plugin"),
endpoint("plugin@market", "first", "v2", "/other-version"),
{"plugin_id": "malformed"},
first,
]));
let first_socket = Some(serde_json::from_value(first.clone()).unwrap());
let second_socket = Some(serde_json::from_value(second.clone()).unwrap());
assert_eq!(resolve(&bindings, "first"), first_socket);
assert_eq!(resolve(&bindings, "second"), second_socket);
let bindings = ParentMcpBindings::from_value(json!([
first,
second,
endpoint("plugin@market", "first", "v1", "/ambiguous"),
]));
assert_eq!(resolve(&bindings, "first"), None);
assert_eq!(resolve(&bindings, "second"), second_socket);
}