mirror of
https://github.com/openai/codex.git
synced 2026-12-01 00:20:48 +00:00
fix(core): skip custom mcp approval for empty annotations
Treat custom MCP tools with empty `ToolAnnotations` the same as missing annotations when the tool is using the default `auto` approval mode. This avoids routing trivial stdio MCP tools into the approval elicitation path in `exec` mode, which was causing `user cancelled MCP tool call` failures for otherwise safe servers.
This commit is contained in:
@@ -46,6 +46,7 @@ use codex_mcp::mcp_permission_prompt_is_auto_approved;
|
||||
use codex_otel::sanitize_metric_tag_value;
|
||||
use codex_protocol::mcp::CallToolResult;
|
||||
use codex_protocol::openai_models::InputModality;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::McpInvocation;
|
||||
use codex_protocol::protocol::McpToolCallBeginEvent;
|
||||
@@ -829,8 +830,9 @@ fn mcp_tool_approval_prompt_options(
|
||||
///
|
||||
/// The function's early-exit order matters:
|
||||
/// 1. Full-access mode → skip everything.
|
||||
/// 2. Custom MCP in `Auto` mode without annotations → skip (user opted in by
|
||||
/// configuring the server; see `should_skip_default_custom_mcp_approval`).
|
||||
/// 2. Custom MCP in `Auto` mode without annotations (or with empty annotations
|
||||
/// where every hint is `None`) → skip (user opted in by configuring the
|
||||
/// server; see `should_skip_default_custom_mcp_approval`).
|
||||
/// 3. Annotation-based check → skip if annotations say the tool is safe and
|
||||
/// the mode is not `Prompt`.
|
||||
/// 4. ARC safety monitor (for `Approve` mode) → may block or escalate.
|
||||
@@ -892,7 +894,7 @@ async fn maybe_request_mcp_tool_approval(
|
||||
// safety-monitor block so that `Approve`-mode tools are still
|
||||
// safety-checked even when running non-interactively.
|
||||
if matches!(turn_context.approval_policy.value(), AskForApproval::Never) {
|
||||
return Some(McpToolApprovalDecision::Decline);
|
||||
return Some(McpToolApprovalDecision::Decline { message: None });
|
||||
}
|
||||
let session_approval_key = session_mcp_tool_approval_key(invocation, metadata, approval_mode);
|
||||
let persistent_approval_key =
|
||||
@@ -1051,14 +1053,23 @@ async fn maybe_request_mcp_tool_approval(
|
||||
///
|
||||
/// Codex Apps tools always carry `ToolAnnotations`, so the annotation-based logic in
|
||||
/// `requires_mcp_tool_approval` is authoritative for them. Custom MCP servers, however,
|
||||
/// may not provide annotations at all. When a custom tool is in the default `Auto`
|
||||
/// approval mode and no annotations are present, the user has already opted in by
|
||||
/// configuring the server — prompting them again would be a regression from the
|
||||
/// expected behavior (see #15824).
|
||||
/// may omit annotations entirely **or** supply a `ToolAnnotations` struct with every
|
||||
/// hint field set to `None` (the "empty annotations" case — common for stdio-based
|
||||
/// servers that echo the struct without populating it). Both cases signal the same
|
||||
/// thing: the server has no opinion on the tool's risk profile.
|
||||
///
|
||||
/// When a custom tool is in the default `Auto` approval mode and annotations are absent
|
||||
/// or empty, the user has already opted in by configuring the server — prompting them
|
||||
/// again would be a regression from the expected behavior (see #15824).
|
||||
///
|
||||
/// This gate intentionally does **not** fire for `Prompt` or `Approve` modes: those
|
||||
/// represent an explicit per-tool override that should always take effect regardless
|
||||
/// of annotation presence.
|
||||
///
|
||||
/// Callers that set any concrete hint value (e.g. `destructive_hint: Some(true)`)
|
||||
/// will fall through to the normal `requires_mcp_tool_approval` path, ensuring
|
||||
/// annotation-based approval is still enforced whenever the server actually provides
|
||||
/// risk information.
|
||||
fn should_skip_default_custom_mcp_approval(
|
||||
invocation: &McpInvocation,
|
||||
approval_mode: AppToolApproval,
|
||||
@@ -1066,7 +1077,11 @@ fn should_skip_default_custom_mcp_approval(
|
||||
) -> bool {
|
||||
invocation.server != CODEX_APPS_MCP_SERVER_NAME
|
||||
&& approval_mode == AppToolApproval::Auto
|
||||
&& annotations.is_none()
|
||||
&& annotations.is_none_or(|annotations| {
|
||||
annotations.destructive_hint.is_none()
|
||||
&& annotations.read_only_hint.is_none()
|
||||
&& annotations.open_world_hint.is_none()
|
||||
})
|
||||
}
|
||||
|
||||
async fn maybe_monitor_auto_approved_mcp_tool_call(
|
||||
|
||||
@@ -210,6 +210,24 @@ fn custom_mcp_invocation_without_annotations() -> McpInvocation {
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds metadata whose `annotations` field is `Some(...)` but every hint inside
|
||||
/// is `None` — the "empty annotations" variant that stdio MCP servers commonly emit.
|
||||
fn custom_mcp_metadata_with_empty_annotations() -> McpToolApprovalMetadata {
|
||||
McpToolApprovalMetadata {
|
||||
annotations: Some(annotations(
|
||||
/*read_only*/ None, /*destructive*/ None, /*open_world*/ None,
|
||||
)),
|
||||
connector_id: None,
|
||||
connector_name: None,
|
||||
connector_description: None,
|
||||
tool_title: Some("Search".to_string()),
|
||||
tool_description: Some("Search docs".to_string()),
|
||||
mcp_app_resource_uri: None,
|
||||
codex_apps_meta: None,
|
||||
openai_file_input_params: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn approval_required_when_read_only_false_and_destructive() {
|
||||
let annotations = annotations(Some(false), Some(true), /*open_world*/ None);
|
||||
@@ -2099,6 +2117,7 @@ async fn custom_auto_mode_skips_approval_when_annotations_are_missing_in_never_m
|
||||
&turn_context,
|
||||
"call-custom-auto",
|
||||
&invocation,
|
||||
"mcp__docs__search",
|
||||
/*metadata*/ None,
|
||||
AppToolApproval::Auto,
|
||||
)
|
||||
@@ -2128,6 +2147,7 @@ async fn custom_auto_mode_skips_approval_when_annotations_are_missing_in_on_requ
|
||||
&turn_context,
|
||||
"call-custom-auto-on-request",
|
||||
&invocation,
|
||||
"mcp__docs__search",
|
||||
/*metadata*/ None,
|
||||
AppToolApproval::Auto,
|
||||
)
|
||||
@@ -2143,6 +2163,44 @@ async fn custom_auto_mode_skips_approval_when_annotations_are_missing_in_on_requ
|
||||
assert_eq!(decision, None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn custom_auto_mode_skips_approval_when_annotations_have_no_hints_in_on_request_mode() {
|
||||
let (session, turn_context, _rx_event) = make_session_and_context_with_rx().await;
|
||||
{
|
||||
let mut active_turn = session.active_turn.lock().await;
|
||||
*active_turn = Some(ActiveTurn::default());
|
||||
}
|
||||
|
||||
let session = Arc::new(session);
|
||||
let turn_context = Arc::new(turn_context);
|
||||
let invocation = custom_mcp_invocation_without_annotations();
|
||||
let metadata = custom_mcp_metadata_with_empty_annotations();
|
||||
|
||||
let mut approval_task = {
|
||||
let session = Arc::clone(&session);
|
||||
let turn_context = Arc::clone(&turn_context);
|
||||
tokio::spawn(async move {
|
||||
maybe_request_mcp_tool_approval(
|
||||
&session,
|
||||
&turn_context,
|
||||
"call-custom-auto-empty-annotations-on-request",
|
||||
&invocation,
|
||||
"mcp__docs__search",
|
||||
Some(&metadata),
|
||||
AppToolApproval::Auto,
|
||||
)
|
||||
.await
|
||||
})
|
||||
};
|
||||
|
||||
let decision = tokio::time::timeout(std::time::Duration::from_millis(200), &mut approval_task)
|
||||
.await
|
||||
.expect("custom MCP tools with empty annotations should not wait for approval")
|
||||
.expect("approval task should complete successfully");
|
||||
|
||||
assert_eq!(decision, None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn custom_approve_mode_without_metadata_still_uses_arc_monitor() {
|
||||
use wiremock::Mock;
|
||||
@@ -2190,6 +2248,7 @@ async fn custom_approve_mode_without_metadata_still_uses_arc_monitor() {
|
||||
&turn_context,
|
||||
"call-custom-approve-no-metadata",
|
||||
&invocation,
|
||||
"mcp__docs__search",
|
||||
/*metadata*/ None,
|
||||
AppToolApproval::Approve,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user