code-mode: retain shared MCP types for deferred tools (#31745)

## Why

When MCP tools are deferred behind `tool_search`, Code mode keeps them
callable but omits their individual declarations from the initial `exec`
description. The shared MCP `CallToolResult` types were derived only
from directly rendered tools, so deferring every MCP tool also removed
the common response contract that models need to interpret MCP results.

This restores that contract without undoing the context savings from
deferred tool definitions. This is a follow-up to #29486.

## What changed

- Track deferred Code-mode tool definitions separately from directly
rendered definitions.
- Render the shared MCP type preamble when either direct or deferred MCP
tools are available.
- Keep deferred tool declarations out of the initial prompt.
- Add unit and integration coverage for deferred MCP tools.

## Testing

- `just test -p codex-code-mode-protocol`
- `just test -p codex-core
code_mode_only_guides_all_tools_search_and_calls_deferred_app_tools`
This commit is contained in:
sayan-oai
2026-07-09 08:17:13 -07:00
committed by GitHub
parent 23a09eb3c3
commit a6b99ee5c4
4 changed files with 69 additions and 31 deletions
@@ -6,9 +6,9 @@ use std::collections::BTreeMap;
pub(crate) fn create_code_mode_tool(
enabled_tools: &[CodeModeToolDefinition],
deferred_tools: &[CodeModeToolDefinition],
namespace_descriptions: &BTreeMap<String, codex_code_mode::ToolNamespaceDescription>,
code_mode_only: bool,
deferred_tools_available: bool,
) -> ToolSpec {
const CODE_MODE_FREEFORM_GRAMMAR: &str = r#"
start: pragma_source | plain_source
@@ -24,9 +24,9 @@ SOURCE: /[\s\S]+/
name: codex_code_mode::PUBLIC_TOOL_NAME.to_string(),
description: codex_code_mode::build_exec_tool_description(
enabled_tools,
deferred_tools,
namespace_descriptions,
code_mode_only,
deferred_tools_available,
),
format: FreeformToolFormat {
r#type: "grammar".to_string(),
@@ -56,17 +56,17 @@ mod tests {
assert_eq!(
create_code_mode_tool(
&enabled_tools,
&[],
&BTreeMap::new(),
/*code_mode_only*/ true,
/*deferred_tools_available*/ false,
),
ToolSpec::Freeform(FreeformTool {
name: codex_code_mode::PUBLIC_TOOL_NAME.to_string(),
description: codex_code_mode::build_exec_tool_description(
&enabled_tools,
&[],
&BTreeMap::new(),
/*code_mode_only*/ true,
/*deferred_tools_available*/ false
),
format: FreeformToolFormat {
r#type: "grammar".to_string(),
+7 -6
View File
@@ -461,7 +461,7 @@ fn build_code_mode_executors(
let mut code_mode_nested_tool_specs = Vec::new();
let mut exec_prompt_tool_specs = Vec::new();
let mut deferred_tools_available = false;
let mut deferred_exec_prompt_tool_specs = Vec::new();
let deferred_tools_guidance_enabled = search_tool_enabled(turn_context);
for executor in executors {
let exposure = executor.exposure();
@@ -480,10 +480,9 @@ fn build_code_mode_executors(
let spec = executor.spec();
if exposure == ToolExposure::Deferred {
// Only show deferred-tool guidance when supported and an included spec is usable by code mode.
deferred_tools_available |= deferred_tools_guidance_enabled
&& !collect_code_mode_exec_prompt_tool_definitions(std::iter::once(&spec))
.is_empty();
if deferred_tools_guidance_enabled {
deferred_exec_prompt_tool_specs.push(spec.clone());
}
} else {
exec_prompt_tool_specs.push(spec.clone());
}
@@ -495,14 +494,16 @@ fn build_code_mode_executors(
collect_code_mode_exec_prompt_tool_definitions(exec_prompt_tool_specs.iter());
enabled_tools
.sort_by(|left, right| compare_code_mode_tools(left, right, &namespace_descriptions));
let deferred_tools =
collect_code_mode_exec_prompt_tool_definitions(deferred_exec_prompt_tool_specs.iter());
vec![
Arc::new(CodeModeExecuteHandler::new(
create_code_mode_tool(
&enabled_tools,
&deferred_tools,
&namespace_descriptions,
tool_mode == ToolMode::CodeModeOnly,
deferred_tools_available,
),
code_mode_nested_tool_specs,
)),