mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
Pin MCP runtimes to model steps (#30101)
## Why
An MCP refresh can replace the session's current manager while a model
step is still running. The step must execute calls through the same
manager whose tools it advertised.
## Boundary
```text
current session MCP runtime
|
| capture once for this model step
v
StepContext.mcp
- exact MCP config
- exact connection manager
- exact runtime environment context
```
```rust
pub struct McpRuntimeSnapshot {
config: Arc<McpConfig>,
manager: Arc<McpConnectionManager>,
runtime_context: McpRuntimeContext,
}
```
## Example
```text
step A captures runtime A and advertises A's tools
refresh publishes runtime B
step A tool call -> runtime A
next step -> runtime B
```
Capturing the snapshot is only an `Arc` clone. It does not restart MCPs
or make an RPC.
## What changes
- Captures one MCP runtime in `StepContext`.
- Uses it for tool planning, tool calls, resources, approvals, connector
attribution, and elicitation.
- Publishes replacement runtimes atomically.
- Lets an old runtime live only while an in-flight step or request still
holds its `Arc`.
Most of this diff is mechanical routing from the session-global manager
to `step_context.mcp`; it does not introduce selected-plugin discovery
yet.
## What does not change
- No plugin or extension migration.
- No new MCP cache policy.
- No environment file watching.
- No client sharing between separate managers.
## Stack
1. Extension-owned World State sections.
2. Project executor skills through World State.
3. **This PR:** pin one MCP runtime to each model step.
4. Project selected MCP/app/connector metadata by environment
availability.
5. One end-to-end integration scenario.
This commit is contained in:
@@ -40,6 +40,7 @@ use crate::guardian::spawn_approval_request_review;
|
||||
use crate::mcp_tool_call::MCP_TOOL_APPROVAL_ACCEPT;
|
||||
use crate::mcp_tool_call::MCP_TOOL_APPROVAL_ACCEPT_FOR_SESSION;
|
||||
use crate::mcp_tool_call::MCP_TOOL_APPROVAL_DECLINE_SYNTHETIC;
|
||||
use crate::mcp_tool_call::McpToolApprovalMetadata;
|
||||
use crate::mcp_tool_call::build_guardian_mcp_tool_review_request;
|
||||
use crate::mcp_tool_call::is_mcp_tool_approval_question_id;
|
||||
use crate::mcp_tool_call::lookup_mcp_tool_metadata;
|
||||
@@ -60,6 +61,12 @@ use codex_protocol::protocol::MultiAgentVersion;
|
||||
#[cfg(test)]
|
||||
use crate::session::completed_session_loop_termination;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct PendingMcpInvocation {
|
||||
invocation: McpInvocation,
|
||||
metadata: Option<McpToolApprovalMetadata>,
|
||||
}
|
||||
|
||||
/// Start an interactive sub-Codex thread and return IO channels.
|
||||
///
|
||||
/// The returned `events_rx` yields non-approval events emitted by the sub-agent.
|
||||
@@ -149,10 +156,10 @@ pub(crate) async fn run_codex_thread_interactive(
|
||||
let parent_session_clone = Arc::clone(&parent_session);
|
||||
let parent_ctx_clone = Arc::clone(&parent_ctx);
|
||||
let codex_for_events = Arc::clone(&codex);
|
||||
// Cache delegated MCP invocations so guardian can recover the full tool call
|
||||
// context when the later legacy RequestUserInput approval event only carries
|
||||
// a call_id plus approval question metadata.
|
||||
let pending_mcp_invocations = Arc::new(Mutex::new(HashMap::<String, McpInvocation>::new()));
|
||||
// Cache the child call's MCP metadata at begin time. The later legacy
|
||||
// RequestUserInput approval event only carries a call_id and question metadata.
|
||||
let pending_mcp_invocations =
|
||||
Arc::new(Mutex::new(HashMap::<String, PendingMcpInvocation>::new()));
|
||||
tokio::spawn(async move {
|
||||
forward_events(
|
||||
codex_for_events,
|
||||
@@ -270,7 +277,7 @@ async fn forward_events(
|
||||
tx_sub: Sender<Event>,
|
||||
parent_session: Arc<Session>,
|
||||
parent_ctx: Arc<TurnContext>,
|
||||
pending_mcp_invocations: Arc<Mutex<HashMap<String, McpInvocation>>>,
|
||||
pending_mcp_invocations: Arc<Mutex<HashMap<String, PendingMcpInvocation>>>,
|
||||
cancel_token: CancellationToken,
|
||||
) {
|
||||
let cancelled = cancel_token.cancelled();
|
||||
@@ -357,10 +364,34 @@ async fn forward_events(
|
||||
id,
|
||||
msg: EventMsg::McpToolCallBegin(event),
|
||||
} => {
|
||||
// Runtime refreshes are published before a request step is captured, so
|
||||
// the child runtime at call begin is the one executing this invocation.
|
||||
// Cache its metadata now; the later approval event has only a call ID.
|
||||
let metadata = if let Some(turn_context) =
|
||||
codex.session.turn_context_for_sub_id(&id).await
|
||||
{
|
||||
let mcp = codex.session.services.latest_mcp_runtime();
|
||||
lookup_mcp_tool_metadata(
|
||||
codex.session.as_ref(),
|
||||
turn_context.as_ref(),
|
||||
mcp.manager(),
|
||||
&event.invocation.server,
|
||||
&event.invocation.tool,
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
None
|
||||
};
|
||||
pending_mcp_invocations
|
||||
.lock()
|
||||
.await
|
||||
.insert(event.call_id.clone(), event.invocation.clone());
|
||||
.insert(
|
||||
event.call_id.clone(),
|
||||
PendingMcpInvocation {
|
||||
invocation: event.invocation.clone(),
|
||||
metadata,
|
||||
},
|
||||
);
|
||||
if !forward_event_or_shutdown(
|
||||
&codex,
|
||||
&tx_sub,
|
||||
@@ -648,7 +679,7 @@ async fn handle_request_user_input(
|
||||
id: String,
|
||||
parent_session: &Arc<Session>,
|
||||
parent_ctx: &Arc<TurnContext>,
|
||||
pending_mcp_invocations: &Arc<Mutex<HashMap<String, McpInvocation>>>,
|
||||
pending_mcp_invocations: &Arc<Mutex<HashMap<String, PendingMcpInvocation>>>,
|
||||
event: RequestUserInputEvent,
|
||||
cancel_token: &CancellationToken,
|
||||
) {
|
||||
@@ -686,12 +717,12 @@ async fn handle_request_user_input(
|
||||
/// programmatically after running the guardian review.
|
||||
///
|
||||
/// The RequestUserInput event only carries `call_id` plus approval question
|
||||
/// metadata, so this helper joins it back to the cached `McpToolCallBegin`
|
||||
/// invocation in order to rebuild the full guardian review request.
|
||||
/// metadata, so this helper joins it back to the child runtime metadata cached at
|
||||
/// `McpToolCallBegin` in order to rebuild the full guardian review request.
|
||||
async fn maybe_auto_review_mcp_request_user_input(
|
||||
parent_session: &Arc<Session>,
|
||||
parent_ctx: &Arc<TurnContext>,
|
||||
pending_mcp_invocations: &Arc<Mutex<HashMap<String, McpInvocation>>>,
|
||||
pending_mcp_invocations: &Arc<Mutex<HashMap<String, PendingMcpInvocation>>>,
|
||||
event: &RequestUserInputEvent,
|
||||
cancel_token: &CancellationToken,
|
||||
) -> Option<RequestUserInputResponse> {
|
||||
@@ -702,18 +733,13 @@ async fn maybe_auto_review_mcp_request_user_input(
|
||||
.questions
|
||||
.iter()
|
||||
.find(|question| is_mcp_tool_approval_question_id(&question.id))?;
|
||||
let invocation = pending_mcp_invocations
|
||||
let pending = pending_mcp_invocations
|
||||
.lock()
|
||||
.await
|
||||
.get(&event.call_id)
|
||||
.cloned()?;
|
||||
let metadata = lookup_mcp_tool_metadata(
|
||||
parent_session.as_ref(),
|
||||
parent_ctx.as_ref(),
|
||||
&invocation.server,
|
||||
&invocation.tool,
|
||||
)
|
||||
.await;
|
||||
let invocation = pending.invocation;
|
||||
let metadata = pending.metadata;
|
||||
let approvals_reviewer =
|
||||
mcp_approvals_reviewer(parent_ctx, &invocation.server, metadata.as_ref());
|
||||
if !routes_approval_to_guardian_with_reviewer(parent_ctx, approvals_reviewer) {
|
||||
|
||||
Reference in New Issue
Block a user