Files
codex/codex-rs/ext/extension-api/src/contributors/mcp.rs
T
jif c3a479620f Add selected-plugin precedence and attribution to the MCP catalog (#27884)
## Why

**In short:** this PR resolves already-discovered MCP registrations. It
does not read selected plugins or discover their MCP servers.

The resolved MCP catalog currently builds config and auto-discovered
plugin registrations before runtime contributors are applied. A
thread-selected plugin needs a distinct precedence tier in that same
initial resolution pass: otherwise a disabled lower-precedence winner
can leave stale name-level state behind, and the winning MCP tools
cannot be attributed to the selected package reliably.

This PR adds that catalog boundary before executor discovery is
connected.

## What changed

- Added an explicit selected-plugin registration tier between
auto-discovered plugins and explicit config.
- Collected selected-plugin contributions before the initial catalog
build, while leaving compatibility and generic extension overlays in
their existing runtime phase.
- Retained the winning plugin ID and display name directly on
plugin-owned catalog registrations.
- Derived MCP tool provenance from the winning catalog entry instead of
joining against local-only plugin summaries.
- Retained the winning selected server's tool approval policy in the
running connection manager, so a selected registration cannot inherit
approval behavior from a losing local plugin.
- Kept remembered approval session-scoped for selected plugins until
there is an authority-aware persistence contract; Codex will not write
approval back to an unrelated local plugin.
- Preserved existing name-level disabled vetoes for discovered plugins
and config, while keeping a selected package's own disabled registration
scoped to that registration.
- Preserved deterministic selection order and existing config,
compatibility, and extension precedence.

The resulting order is:

```text
auto-discovered plugin
  < selected plugin
  < explicit config
  < compatibility registration
  < extension overlay
```

## Behavior and scope

This is a catalog and provenance change only. No production host
contributes selected-plugin MCP registrations yet, so existing local MCP
behavior remains unchanged.

The stacked follow-up, #27870, installs the executor plugin provider
that produces these registrations. App-server activation remains a
separate final step.

## Verification

Focused tests cover precedence, deterministic selected-plugin conflicts,
disabled-veto behavior across catalog phases, managed requirements
before selected-plugin resolution, winning-server approval policy, and
attribution when local and selected packages share an ID or server name.
CI owns execution of the test suite.
2026-06-15 11:10:51 +02:00

72 lines
2.1 KiB
Rust

use codex_config::McpServerConfig;
use crate::ExtensionDataInit;
/// Input supplied while resolving MCP server contributions.
///
/// Thread-scoped implementations can read the immutable host-seeded inputs
/// through [`Self::thread_init`]. Implementations should not retain borrowed
/// context after contribution completes.
pub struct McpServerContributionContext<'a, C> {
/// Host configuration visible during MCP resolution.
config: &'a C,
/// Initial inputs for the active thread, when resolution is thread-scoped.
thread_init: Option<&'a ExtensionDataInit>,
}
impl<C> Clone for McpServerContributionContext<'_, C> {
fn clone(&self) -> Self {
*self
}
}
impl<C> Copy for McpServerContributionContext<'_, C> {}
impl<'a, C> McpServerContributionContext<'a, C> {
/// Creates context for resolution that is not associated with a running thread.
pub fn global(config: &'a C) -> Self {
Self {
config,
thread_init: None,
}
}
/// Creates context for one active thread runtime.
pub fn for_thread(config: &'a C, thread_init: &'a ExtensionDataInit) -> Self {
Self {
config,
thread_init: Some(thread_init),
}
}
/// Returns the host configuration visible during resolution.
pub fn config(&self) -> &'a C {
self.config
}
/// Returns the frozen initial inputs when resolving for a running thread.
pub fn thread_init(&self) -> Option<&'a ExtensionDataInit> {
self.thread_init
}
}
/// One extension-owned overlay for the runtime MCP server configuration.
#[derive(Clone, Debug)]
pub enum McpServerContribution {
/// Adds or replaces a named MCP server.
Set {
name: String,
config: Box<McpServerConfig>,
},
/// Registers a server declared by a plugin selected for this thread.
SelectedPlugin {
name: String,
plugin_id: String,
plugin_display_name: String,
selection_order: usize,
config: Box<McpServerConfig>,
},
/// Removes a named MCP server.
Remove { name: String },
}