mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
3095ea9c3d
## Why
Selected plugin metadata is stable, but MCP processes are live runtime
state. They need different lifetimes:
- the MCP extension caches manifest, MCP, and connector declarations for
each stable selected root;
- each model step projects that cached metadata through the roots that
resolved as ready for that exact step;
- the MCP manager is rebuilt only when that availability projection
changes.
This matches executor skills: both features consume the same resolved
step roots instead of inferring readiness from the turn's selected
environments.
## Behavior
```text
E1 not ready for this step
-> no E1 MCP servers or connectors
-> cached plugin metadata stays in ext/mcp
E1 becomes ready
-> reuse cached metadata
-> publish one MCP runtime containing E1 capabilities
same ready roots on the next step
-> reuse the exact runtime; no rediscovery and no MCP restart
resume
-> create new extension thread state and a new MCP runtime
```
All model-facing consumers use the same step snapshot:
```text
resolved selected roots
|
v
extension MCP/connector projection
|
v
{ MCP config, connector snapshot, MCP manager }
|
+-> advertise model tools
+-> build app/connector tools
+-> execute MCP calls
```
## Cache contract
The existing MCP extension owns a cache keyed by the full
`SelectedCapabilityRoot`:
```rust
let state = thread_store.get_or_init(SelectedExecutorPluginMcpState::default);
```
The cache lives with extension thread state. Environment availability
filters projection but does not invalidate metadata. Resume creates new
thread state. There is no file watcher or executor generation because
contents behind a stable environment/root are assumed stable.
## What changes
- Keeps executor plugin discovery and cached metadata in `ext/mcp`.
- Caches MCP and connector declarations together per selected root.
- Uses the step's already-resolved capability roots, including lazy
environments that are not turn environments.
- Reuses the current MCP runtime when the ready-root projection is
unchanged.
- Uses the same step MCP manager and connector snapshot for
model-visible tools and execution.
- Resolves direct thread-scoped MCP requests from the current
selected-root projection.
## Deliberately out of scope
- `app/list` remains based on the latest global host-plugin state; this
PR does not make its response or notifications thread-specific.
- `required = true` startup semantics do not apply to delayed executor
MCP activation.
- No filesystem/content invalidation.
- No transport-disconnect watcher.
- No executor generations or environment replacement semantics.
- No client sharing across complete manager replacements.
## Stack
1. Extension-owned World State sections.
2. Project executor skills through World State.
3. Pin one MCP runtime to each model step.
4. **This PR:** project selected MCP and connector state from
extension-owned metadata.
5. Integration coverage for selected capability availability and resume.
## Verification
-
`selected_plugin_servers_use_managed_requirements_for_the_selected_root_id`
- The stacked integration PR covers unavailable to ready activation,
unchanged-runtime reuse, skills, MCP tools, connector attribution, and
cold resume.
147 lines
5.0 KiB
Rust
147 lines
5.0 KiB
Rust
use codex_config::test_support::CloudConfigBundleFixture;
|
|
use codex_core::config::Config;
|
|
use codex_core::config::ConfigBuilder;
|
|
use codex_exec_server::EnvironmentManager;
|
|
use codex_exec_server::LOCAL_ENVIRONMENT_ID;
|
|
use codex_extension_api::ExtensionData;
|
|
use codex_extension_api::ExtensionDataInit;
|
|
use codex_extension_api::ExtensionRegistryBuilder;
|
|
use codex_extension_api::McpServerContribution;
|
|
use codex_extension_api::McpServerContributionContext;
|
|
use codex_protocol::capabilities::CapabilityRootLocation;
|
|
use codex_protocol::capabilities::SelectedCapabilityRoot;
|
|
use codex_utils_path_uri::PathUri;
|
|
use pretty_assertions::assert_eq;
|
|
use std::sync::Arc;
|
|
|
|
type TestResult = Result<(), Box<dyn std::error::Error>>;
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
struct ContributionSummary {
|
|
name: String,
|
|
plugin_id: String,
|
|
plugin_display_name: String,
|
|
selection_order: usize,
|
|
enabled: bool,
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn selected_plugin_servers_use_managed_requirements_for_the_selected_root_id() -> TestResult {
|
|
let codex_home = tempfile::tempdir()?;
|
|
let plugin_root = tempfile::tempdir()?;
|
|
std::fs::create_dir_all(plugin_root.path().join(".codex-plugin"))?;
|
|
std::fs::write(
|
|
plugin_root.path().join(".codex-plugin/plugin.json"),
|
|
r#"{"name":"different-manifest-name","interface":{"displayName":"Selected Demo"}}"#,
|
|
)?;
|
|
std::fs::write(
|
|
plugin_root.path().join(".mcp.json"),
|
|
r#"{
|
|
"mcpServers": {
|
|
"allowed": {"command":"allowed-command"},
|
|
"mismatched": {"command":"wrong-command"},
|
|
"unlisted": {"command":"unlisted-command"}
|
|
}
|
|
}"#,
|
|
)?;
|
|
let config = ConfigBuilder::default()
|
|
.codex_home(codex_home.path().to_path_buf())
|
|
.fallback_cwd(Some(codex_home.path().to_path_buf()))
|
|
.cloud_config_bundle(
|
|
CloudConfigBundleFixture::loader_with_enterprise_requirement(
|
|
r#"
|
|
[plugins."selected-root".mcp_servers.allowed.identity]
|
|
command = "allowed-command"
|
|
|
|
[plugins."selected-root".mcp_servers.mismatched.identity]
|
|
command = "expected-command"
|
|
"#,
|
|
),
|
|
)
|
|
.build()
|
|
.await?;
|
|
|
|
let contributions = selected_plugin_contributions(&config, plugin_root.path()).await?;
|
|
|
|
assert_eq!(
|
|
contributions,
|
|
vec![
|
|
ContributionSummary {
|
|
name: "allowed".to_string(),
|
|
plugin_id: "selected-root".to_string(),
|
|
plugin_display_name: "Selected Demo".to_string(),
|
|
selection_order: 0,
|
|
enabled: true,
|
|
},
|
|
ContributionSummary {
|
|
name: "mismatched".to_string(),
|
|
plugin_id: "selected-root".to_string(),
|
|
plugin_display_name: "Selected Demo".to_string(),
|
|
selection_order: 0,
|
|
enabled: false,
|
|
},
|
|
ContributionSummary {
|
|
name: "unlisted".to_string(),
|
|
plugin_id: "selected-root".to_string(),
|
|
plugin_display_name: "Selected Demo".to_string(),
|
|
selection_order: 0,
|
|
enabled: false,
|
|
},
|
|
]
|
|
);
|
|
Ok(())
|
|
}
|
|
|
|
async fn selected_plugin_contributions(
|
|
config: &Config,
|
|
plugin_root: &std::path::Path,
|
|
) -> Result<Vec<ContributionSummary>, Box<dyn std::error::Error>> {
|
|
let mut builder = ExtensionRegistryBuilder::new();
|
|
codex_mcp_extension::install_executor_plugins(
|
|
&mut builder,
|
|
Arc::new(EnvironmentManager::default_for_tests()),
|
|
);
|
|
let registry = builder.build();
|
|
let mut thread_init = ExtensionDataInit::new();
|
|
thread_init.insert(vec![SelectedCapabilityRoot {
|
|
id: "selected-root".to_string(),
|
|
location: CapabilityRootLocation::Environment {
|
|
environment_id: LOCAL_ENVIRONMENT_ID.to_string(),
|
|
path: PathUri::from_host_native_path(plugin_root)?,
|
|
},
|
|
}]);
|
|
let thread_store = ExtensionData::new_with_init("test-thread", thread_init.clone());
|
|
let available_environment_ids = vec![LOCAL_ENVIRONMENT_ID.to_string()];
|
|
|
|
Ok(registry.mcp_server_contributors()[0]
|
|
.contribute(McpServerContributionContext::for_step(
|
|
config,
|
|
&thread_init,
|
|
&thread_store,
|
|
&available_environment_ids,
|
|
))
|
|
.await
|
|
.into_iter()
|
|
.map(|contribution| match contribution {
|
|
McpServerContribution::SelectedPlugin {
|
|
name,
|
|
plugin_id,
|
|
plugin_display_name,
|
|
selection_order,
|
|
config,
|
|
} => ContributionSummary {
|
|
name,
|
|
plugin_id,
|
|
plugin_display_name,
|
|
selection_order,
|
|
enabled: config.enabled,
|
|
},
|
|
McpServerContribution::Set { .. }
|
|
| McpServerContribution::SelectedPluginConnectors { .. }
|
|
| McpServerContribution::Remove { .. } => {
|
|
panic!("expected selected plugin contribution")
|
|
}
|
|
})
|
|
.collect())
|
|
}
|