Files
codex/codex-rs/core-plugins/src/provider.rs
T
jif b3c423e475 Discover stdio MCP servers from selected executor plugins (#27870)
## Why

**In short:** this PR discovers MCP registrations by reading a selected
plugin's `.mcp.json` on its executor. #27884 then resolves those
registrations in the shared catalog.

`thread/start.selectedCapabilityRoots` can select a plugin root owned by
an executor, and Codex can resolve that package through the executor
filesystem. MCP declarations inside the selected plugin are still
ignored.

This PR adds the source-specific discovery layer on top of the
selected-plugin catalog boundary in #27884:

```text
selected capability root
        |
        v
resolve the plugin through its executor filesystem
        |
        v
read and normalize its MCP config through the same filesystem
        |
        v
contribute stdio registrations bound to that environment ID
```

The existing MCP launcher and connection manager remain unchanged. MCP
config parsing is shared with local plugins through #27863.

## What changed

- Added an executor plugin MCP provider in the MCP extension.
- Retained only the exact filesystem capability used for package
resolution and reused it for the selected plugin's MCP config, with no
host-filesystem fallback or unrelated process/HTTP authority.
- Read either the manifest-declared MCP config or the default
`.mcp.json`; a missing default file means the plugin has no MCP servers.
- Accepted stdio servers only for this first vertical. Executor-owned
HTTP declarations are skipped with a warning until their placement
semantics are defined.
- Normalized stdio registrations with the owning environment's stable
logical ID and plugin-root working directory.
- Resolved environment-variable names on the owning executor and
rejected explicit local forwarding for non-local plugins.
- Froze discovered declarations once per active thread runtime, then
applied current managed plugin and MCP requirements when contributing
them.
- Carried the selected root ID, display name, and selection order into
the catalog contribution defined by #27884.

## Behavior and scope

There is intentionally no production behavior change yet. This PR
provides the executor provider and contribution boundary, but app-server
does not install it in this change. Existing local plugin MCP loading is
unchanged, and no MCP process is launched by this PR alone.

## Assumptions

- The selected root ID is the plugin policy identity; the manifest
display name is presentation metadata.
- An environment ID is a stable logical authority. Reconnection or
replacement under the same ID does not change ownership.
- Selected plugin packages and their manifests are trusted inputs.
- The selected package and MCP discovery snapshot remain frozen for the
active thread runtime.

## Follow-up

The next PR installs this contributor in app-server and adds an
end-to-end test proving that a selected plugin MCP tool launches on its
owning executor, can be called by the model, survives an explicit MCP
refresh, and is invisible when its root was not selected.

Resume, fork, environment removal or ID changes, dynamic catalog reload,
and executor-owned HTTP MCP placement remain separate lifecycle
decisions.

## Verification

Focused tests cover executor-only filesystem reads, missing and
malformed config, stdio filtering and normalization, managed
requirements, package attribution, and selection order. CI owns
execution of the test suite.
2026-06-15 11:52:05 +02:00

251 lines
8.1 KiB
Rust

use crate::manifest::parse_plugin_manifest;
use codex_exec_server::EnvironmentManager;
use codex_exec_server::ExecutorFileSystem;
use codex_plugin::PluginProvider;
use codex_plugin::ResolvedPlugin;
use codex_plugin::ResolvedPluginError;
use codex_protocol::capabilities::CapabilityRootLocation;
use codex_protocol::capabilities::SelectedCapabilityRoot;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_path_uri::PathUri;
use codex_utils_plugins::DISCOVERABLE_PLUGIN_MANIFEST_PATHS;
use std::io;
use std::path::PathBuf;
use std::sync::Arc;
use thiserror::Error;
/// Failure to resolve an environment-owned capability root as a plugin package.
#[derive(Debug, Error)]
pub enum ExecutorPluginProviderError {
#[error("selected capability root `{root_id}` has invalid path `{path}`: {message}")]
InvalidRootPath {
root_id: String,
path: String,
message: String,
},
#[error(
"selected capability root `{root_id}` references unavailable environment `{environment_id}`"
)]
UnavailableEnvironment {
root_id: String,
environment_id: String,
},
#[error("failed to inspect selected capability root `{root_id}` at {path}: {source}")]
InspectRoot {
root_id: String,
path: AbsolutePathBuf,
#[source]
source: io::Error,
},
#[error("selected capability root `{root_id}` path {path} is not a directory")]
RootNotDirectory {
root_id: String,
path: AbsolutePathBuf,
},
#[error("failed to inspect plugin manifest for `{root_id}` at {path}: {source}")]
InspectManifest {
root_id: String,
path: AbsolutePathBuf,
#[source]
source: io::Error,
},
#[error("failed to read plugin manifest for `{root_id}` at {path}: {source}")]
ReadManifest {
root_id: String,
path: AbsolutePathBuf,
#[source]
source: io::Error,
},
#[error("failed to parse plugin manifest for `{root_id}` at {path}: {source}")]
ParseManifest {
root_id: String,
path: AbsolutePathBuf,
#[source]
source: serde_json::Error,
},
#[error("failed to construct plugin descriptor for `{root_id}`: {source}")]
ConstructDescriptor {
root_id: String,
#[source]
source: ResolvedPluginError,
},
}
/// Resolves plugin packages through the filesystem owned by an execution environment.
#[derive(Clone, Debug)]
pub struct ExecutorPluginProvider {
environment_manager: Arc<EnvironmentManager>,
}
/// A resolved plugin paired with the concrete filesystem used to read it.
#[derive(Clone)]
pub struct ResolvedExecutorPlugin {
plugin: ResolvedPlugin,
file_system: Arc<dyn ExecutorFileSystem>,
}
impl ResolvedExecutorPlugin {
/// Returns the source-neutral plugin descriptor.
pub fn plugin(&self) -> &ResolvedPlugin {
&self.plugin
}
/// Returns the concrete filesystem that resolved the descriptor.
pub fn file_system(&self) -> &dyn ExecutorFileSystem {
self.file_system.as_ref()
}
}
impl ExecutorPluginProvider {
/// Creates a provider backed by the active execution environments.
pub fn new(environment_manager: Arc<EnvironmentManager>) -> Self {
Self {
environment_manager,
}
}
/// Resolves a plugin and retains the exact filesystem used for package access.
pub async fn resolve_bound(
&self,
selected_root: &SelectedCapabilityRoot,
) -> Result<Option<ResolvedExecutorPlugin>, ExecutorPluginProviderError> {
let root_id = &selected_root.id;
let plugin_root = selected_plugin_root(selected_root)?;
let CapabilityRootLocation::Environment { environment_id, .. } = &selected_root.location;
let environment = self
.environment_manager
.get_environment(environment_id)
.ok_or_else(|| ExecutorPluginProviderError::UnavailableEnvironment {
root_id: root_id.clone(),
environment_id: environment_id.clone(),
})?;
let file_system = environment.get_filesystem();
let plugin = resolve_plugin_root(selected_root, plugin_root, file_system.as_ref()).await?;
Ok(plugin.map(|plugin| ResolvedExecutorPlugin {
plugin,
file_system,
}))
}
}
impl PluginProvider for ExecutorPluginProvider {
type Error = ExecutorPluginProviderError;
async fn resolve(
&self,
selected_root: &SelectedCapabilityRoot,
) -> Result<Option<ResolvedPlugin>, Self::Error> {
self.resolve_bound(selected_root)
.await
.map(|plugin| plugin.map(|plugin| plugin.plugin))
}
}
fn selected_plugin_root(
selected_root: &SelectedCapabilityRoot,
) -> Result<AbsolutePathBuf, ExecutorPluginProviderError> {
let root_id = &selected_root.id;
let CapabilityRootLocation::Environment { path, .. } = &selected_root.location;
let plugin_root = PathBuf::from(path);
if !plugin_root.is_absolute() {
return Err(ExecutorPluginProviderError::InvalidRootPath {
root_id: root_id.clone(),
path: path.clone(),
message: "executor path must be absolute".to_string(),
});
}
AbsolutePathBuf::from_absolute_path_checked(plugin_root).map_err(|err| {
ExecutorPluginProviderError::InvalidRootPath {
root_id: root_id.clone(),
path: path.clone(),
message: err.to_string(),
}
})
}
async fn resolve_plugin_root(
selected_root: &SelectedCapabilityRoot,
plugin_root: AbsolutePathBuf,
file_system: &dyn ExecutorFileSystem,
) -> Result<Option<ResolvedPlugin>, ExecutorPluginProviderError> {
let root_id = &selected_root.id;
let CapabilityRootLocation::Environment { environment_id, .. } = &selected_root.location;
let root_uri = PathUri::from_abs_path(&plugin_root);
let root_metadata = file_system
.get_metadata(&root_uri, /*sandbox*/ None)
.await
.map_err(|source| ExecutorPluginProviderError::InspectRoot {
root_id: root_id.clone(),
path: plugin_root.clone(),
source,
})?;
if !root_metadata.is_directory {
return Err(ExecutorPluginProviderError::RootNotDirectory {
root_id: root_id.clone(),
path: plugin_root,
});
}
let mut manifest_path = None;
for relative_path in DISCOVERABLE_PLUGIN_MANIFEST_PATHS {
let candidate = plugin_root.join(relative_path);
let candidate_uri = PathUri::from_abs_path(&candidate);
match file_system
.get_metadata(&candidate_uri, /*sandbox*/ None)
.await
{
Ok(metadata) if metadata.is_file => {
manifest_path = Some((candidate, candidate_uri));
break;
}
Ok(_) => {}
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(source) => {
return Err(ExecutorPluginProviderError::InspectManifest {
root_id: root_id.clone(),
path: candidate,
source,
});
}
}
}
let Some((manifest_path, manifest_uri)) = manifest_path else {
return Ok(None);
};
let contents = file_system
.read_file_text(&manifest_uri, /*sandbox*/ None)
.await
.map_err(|source| ExecutorPluginProviderError::ReadManifest {
root_id: root_id.clone(),
path: manifest_path.clone(),
source,
})?;
let manifest =
parse_plugin_manifest(&plugin_root, &manifest_path, &contents).map_err(|source| {
ExecutorPluginProviderError::ParseManifest {
root_id: root_id.clone(),
path: manifest_path.clone(),
source,
}
})?;
let plugin = ResolvedPlugin::from_environment(
root_id.clone(),
environment_id.clone(),
plugin_root,
manifest_path,
manifest,
)
.map_err(|source| ExecutorPluginProviderError::ConstructDescriptor {
root_id: root_id.clone(),
source,
})?;
Ok(Some(plugin))
}
#[cfg(test)]
#[path = "provider_tests.rs"]
mod tests;