Keep MCP user interaction on the root thread (#46066)

## Why

MCP requests that need human input, including browser sign-in, must be handled by the root thread. Subagents need guidance to hand these blockers to their parent without prompting the user or automatically accepting requests that require input.

## What changed

- Reject interactive MCP elicitations and tool approval prompts in subagents with guidance to ask the parent and wait before retrying.
- Recognize browser authentication and `codex_requires_user_input` metadata even when the form schema is empty, while preserving automatic permission approvals and review decisions.
- Carry user-interaction eligibility through MCP runtime creation and connection reuse, and guard prompt registration.
- Preserve connector authentication diagnostics alongside handoff guidance, subject to normal tool-output limits.

## Testing

Add unit and integration coverage for blocked subagent prompts, root browser authentication, automatic approval and review, connection reuse, and authentication diagnostic preservation and truncation. Add a request-history snapshot for browser-auth handoff guidance.

GitOrigin-RevId: 83e146b6e5c1a2a22a34681ad45615f504b3c96c
This commit is contained in:
Rennie
2026-09-17 01:18:37 +00:00
committed by copyberry
parent 5e636ea760
commit 40584fad87
24 changed files with 1330 additions and 153 deletions
+9 -5
View File
@@ -556,16 +556,20 @@ impl Session {
server_name: String,
request_id: RequestId,
request: ElicitationRequest,
) -> McpServerElicitationOutcome {
) -> anyhow::Result<McpServerElicitationOutcome> {
anyhow::ensure!(
!turn_context.session_source.is_non_root_agent(),
codex_mcp::MCP_ELICITATION_HANDOFF_MESSAGE
);
if self.services.mcp_runtime.elicitations_auto_deny() {
return McpServerElicitationOutcome {
return Ok(McpServerElicitationOutcome {
response: Some(ElicitationResponse {
action: codex_rmcp_client::ElicitationAction::Accept,
content: Some(serde_json::json!({})),
meta: None,
}),
sent: false,
};
});
}
let _elicitation = self.services.elicitations.register();
@@ -617,10 +621,10 @@ impl Session {
plugin_install_telemetry.tool_name.as_str(),
);
}
McpServerElicitationOutcome {
Ok(McpServerElicitationOutcome {
response: rx_response.await.ok(),
sent: true,
}
})
}
#[expect(
+1
View File
@@ -404,6 +404,7 @@ impl Session {
client_mcp_extensions: self.services.client_mcp_extensions.for_mcp_servers(),
auth,
auth_manager: Some(Arc::clone(&self.services.auth_manager)),
allow_user_interaction: !desired.session_source.is_non_root_agent(),
elicitation_reviewer,
elicitation_lifecycle: Some(self.mcp_elicitation_lifecycle()),
}
+55 -1
View File
@@ -87,6 +87,7 @@ use codex_protocol::permissions::FileSystemSandboxPolicy;
use codex_protocol::permissions::FileSystemSandboxPolicyContext;
use codex_protocol::permissions::FileSystemSpecialPath;
use codex_protocol::protocol::EnvironmentConfigState;
use codex_protocol::protocol::InternalSessionSource;
use codex_protocol::protocol::SandboxPolicy;
use codex_protocol::protocol::TurnEnvironmentSelections;
use codex_protocol::request_permissions::PermissionGrantScope;
@@ -652,7 +653,8 @@ async fn request_mcp_server_elicitation_auto_accepts_when_auto_deny_is_enabled()
}),
},
)
.await;
.await
.expect("root thread elicitation should be accepted");
assert_eq!(
response.response,
@@ -666,6 +668,58 @@ async fn request_mcp_server_elicitation_auto_accepts_when_auto_deny_is_enabled()
assert!(rx.try_recv().is_err());
}
#[test_case(false; "interactive")]
#[test_case(true; "auto_accept")]
#[tokio::test]
async fn request_mcp_server_elicitation_rejects_non_root_threads(auto_deny: bool) {
for source in [
SessionSource::SubAgent(SubAgentSource::Review),
SessionSource::Internal(InternalSessionSource::Guardian),
] {
let (session, mut turn_context, rx) = make_session_and_context_with_rx().await;
Arc::get_mut(&mut turn_context)
.expect("turn context should not be shared")
.session_source = source;
*session.active_turn.lock().await = Some(ActiveTurn::default());
session
.services
.mcp_runtime
.set_elicitations_auto_deny(auto_deny);
let paused = session.subscribe_elicitation_pause_state();
let Err(error) = tokio::time::timeout(
Duration::from_secs(1),
session.request_mcp_server_elicitation(
turn_context.as_ref(),
"codex_apps".to_string(),
RequestId::String("request-1".into()),
ElicitationRequest::Url {
meta: None,
message: "Connect this app to continue.".to_string(),
url: "https://example.com/connect".to_string(),
elicitation_id: "connect-1".to_string(),
},
),
)
.await
.expect("non-root elicitation must not wait for user input") else {
panic!("non-root elicitation must be rejected");
};
assert_eq!(
error.to_string(),
codex_mcp::MCP_ELICITATION_HANDOFF_MESSAGE
);
assert!(rx.try_recv().is_err());
assert!(!*paused.borrow());
assert!(
!paused
.has_changed()
.expect("elicitation service should remain available")
);
}
}
#[tokio::test]
async fn interrupting_regular_turn_waiting_on_startup_prewarm_emits_turn_aborted() {
let (sess, tc, rx) = make_session_and_context_with_rx().await;