Distinguish MCP approval cancellation reasons (#54679)

## Why

MCP tool calls reported every aborted approval as "user cancelled MCP tool call", including interrupted requests and automatic cancellations in `exec` mode.

## What changed

Carry `user`, `interrupted`, or `non_interactive` cancellation reasons through MCP approval metadata and resolution. Mark explicit TUI cancellations as user actions, interrupted requests as interruptions, and automatic `exec` cancellations as non-interactive.

Use the reason to report when a call was cancelled before approval or because interactive approval is unavailable in `exec` mode. Preserve the existing cancellation message for clients that omit the reason.

## Testing

Update tests to verify that a closed approval response carries the interruption reason and that `exec` cancellation responses include the non-interactive reason.

GitOrigin-RevId: be2a5f7fef539b5db2add53b46aea27e2b8a66fc
This commit is contained in:
vkg-oai
2026-10-16 19:08:19 +00:00
committed by copyberry
parent e7de1f4440
commit a083a1e49e
10 changed files with 235 additions and 61 deletions
+17 -4
View File
@@ -27,6 +27,7 @@ use codex_app_server_protocol::ClientRequest;
use codex_app_server_protocol::ConfigWarningNotification;
use codex_app_server_protocol::JSONRPCErrorError;
use codex_app_server_protocol::McpServerElicitationAction;
use codex_app_server_protocol::McpServerElicitationRequest;
use codex_app_server_protocol::McpServerElicitationRequestResponse;
use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::ReviewStartParams;
@@ -96,6 +97,10 @@ use codex_protocol::SessionId;
use codex_protocol::ThreadId;
use codex_protocol::config_types::ApprovalsReviewer;
use codex_protocol::config_types::SandboxMode;
use codex_protocol::mcp_approval_meta::APPROVAL_KIND_KEY;
use codex_protocol::mcp_approval_meta::APPROVAL_KIND_MCP_TOOL_CALL;
use codex_protocol::mcp_approval_meta::CANCELLATION_REASON_KEY;
use codex_protocol::mcp_approval_meta::McpApprovalCancellationReason;
use codex_protocol::models::ActivePermissionProfile;
use codex_protocol::models::PermissionProfile;
use codex_protocol::protocol::AskForApproval;
@@ -2002,11 +2007,19 @@ fn resume_lookup_model_providers(
}
}
fn canceled_mcp_server_elicitation_response() -> Result<Value, String> {
fn canceled_mcp_server_elicitation_response(
request: &McpServerElicitationRequest,
) -> Result<Value, String> {
let tool_approval = matches!(request, McpServerElicitationRequest::Form { meta: Some(meta), .. }
if meta.get(APPROVAL_KIND_KEY).and_then(Value::as_str) == Some(APPROVAL_KIND_MCP_TOOL_CALL));
serde_json::to_value(McpServerElicitationRequestResponse {
action: McpServerElicitationAction::Cancel,
content: None,
meta: None,
meta: tool_approval.then(|| {
serde_json::json!({
CANCELLATION_REASON_KEY: McpApprovalCancellationReason::NonInteractive,
})
}),
})
.map_err(|err| format!("failed to encode mcp elicitation response: {err}"))
}
@@ -2077,11 +2090,11 @@ async fn handle_server_request(
) {
let method = server_request_method_name(&request);
let handle_result = match request {
ServerRequest::McpServerElicitationRequest { request_id, .. } => {
ServerRequest::McpServerElicitationRequest { request_id, params } => {
// Exec auto-cancels elicitation instead of surfacing it
// interactively. Preserve that behavior for attached subagent
// threads too so we do not turn a cancel into a decline/error.
match canceled_mcp_server_elicitation_response() {
match canceled_mcp_server_elicitation_response(&params.request) {
Ok(value) => {
resolve_server_request(
client,
+11 -2
View File
@@ -462,7 +462,14 @@ fn should_backfill_turn_completed_items_backfills_persisted_summaries_only() {
#[test]
fn canceled_mcp_server_elicitation_response_uses_cancel_action() {
let value = canceled_mcp_server_elicitation_response()
let request = serde_json::from_value(serde_json::json!({
"mode": "form",
"message": "Allow the tool call?",
"requestedSchema": { "type": "object", "properties": {} },
"_meta": { APPROVAL_KIND_KEY: APPROVAL_KIND_MCP_TOOL_CALL },
}))
.expect("valid MCP tool approval request");
let value = canceled_mcp_server_elicitation_response(&request)
.expect("mcp elicitation cancel response should serialize");
let response: McpServerElicitationRequestResponse =
serde_json::from_value(value).expect("cancel response should deserialize");
@@ -472,7 +479,9 @@ fn canceled_mcp_server_elicitation_response_uses_cancel_action() {
McpServerElicitationRequestResponse {
action: McpServerElicitationAction::Cancel,
content: None,
meta: None,
meta: Some(serde_json::json!({
CANCELLATION_REASON_KEY: McpApprovalCancellationReason::NonInteractive,
})),
}
);
}