From 9b05dc915849740e43fb134c6b43ff361c0bd23f Mon Sep 17 00:00:00 2001 From: Sayan Sisodiya Date: Fri, 12 Jun 2026 13:40:28 -0700 Subject: [PATCH] Simplify deferred namespace guidance wiring --- .../codex-mcp/src/connection_manager_tests.rs | 28 +++++++++++++++++++ codex-rs/codex-mcp/src/tools.rs | 22 ++++++++++++++- codex-rs/core/src/tools/spec_plan.rs | 28 ++++--------------- codex-rs/core/tests/suite/code_mode.rs | 2 +- 4 files changed, 56 insertions(+), 24 deletions(-) diff --git a/codex-rs/codex-mcp/src/connection_manager_tests.rs b/codex-rs/codex-mcp/src/connection_manager_tests.rs index ea405ced9a..5ac7669e3b 100644 --- a/codex-rs/codex-mcp/src/connection_manager_tests.rs +++ b/codex-rs/codex-mcp/src/connection_manager_tests.rs @@ -13,6 +13,8 @@ use crate::rmcp_client::AsyncManagedClient; use crate::rmcp_client::ManagedClient; use crate::rmcp_client::StartupOutcomeError; use crate::server::McpServerOrigin; +use crate::tools::MAX_MCP_NAMESPACE_DESCRIPTION_BYTES; +use crate::tools::MCP_NAMESPACE_DESCRIPTION_TRUNCATION_SUFFIX; use crate::tools::ToolFilter; use crate::tools::ToolInfo; use crate::tools::filter_tools; @@ -323,6 +325,32 @@ fn test_normalize_tools_short_non_duplicated_names() { ); } +#[test] +fn test_normalize_tools_bounds_namespace_descriptions() { + let mut short = create_test_tool("short", "tool"); + short.namespace_description = Some("brief guidance".to_string()); + let mut long = create_test_tool("long", "tool"); + long.namespace_description = Some("é".repeat(MAX_MCP_NAMESPACE_DESCRIPTION_BYTES)); + + let model_tools = + normalize_tools_for_model_with_prefix([short, long], /*prefix_mcp_tool_names*/ true); + let expected_prefix_bytes = + MAX_MCP_NAMESPACE_DESCRIPTION_BYTES - MCP_NAMESPACE_DESCRIPTION_TRUNCATION_SUFFIX.len(); + let expected_long = format!( + "{}{}", + "é".repeat(expected_prefix_bytes / "é".len()), + MCP_NAMESPACE_DESCRIPTION_TRUNCATION_SUFFIX + ); + + assert_eq!( + model_tools + .iter() + .map(|tool| tool.namespace_description.clone()) + .collect::>(), + vec![Some(expected_long), Some("brief guidance".to_string())] + ); +} + #[test] fn test_normalize_tools_duplicated_names_skipped() { let tools = vec![ diff --git a/codex-rs/codex-mcp/src/tools.rs b/codex-rs/codex-mcp/src/tools.rs index 83cbfd5e74..3c57b9d56c 100644 --- a/codex-rs/codex-mcp/src/tools.rs +++ b/codex-rs/codex-mcp/src/tools.rs @@ -155,7 +155,10 @@ where { let mut seen_raw_names = HashSet::new(); let mut candidates = Vec::new(); - for tool in tools { + for mut tool in tools { + if let Some(description) = tool.namespace_description.take() { + tool.namespace_description = Some(truncate_namespace_description(description)); + } let raw_namespace_identity = format!( "{}\0{}\0{}", tool.server_name, @@ -259,6 +262,8 @@ struct CallableToolCandidate { const MCP_TOOL_NAME_DELIMITER: &str = "__"; const MAX_TOOL_NAME_LENGTH: usize = 64; +pub(crate) const MAX_MCP_NAMESPACE_DESCRIPTION_BYTES: usize = 4 * 1024; +pub(crate) const MCP_NAMESPACE_DESCRIPTION_TRUNCATION_SUFFIX: &str = "\n[truncated]"; const CALLABLE_NAME_HASH_LEN: usize = 12; const META_OPENAI_FILE_PARAMS: &str = "openai/fileParams"; @@ -349,6 +354,21 @@ fn truncate_name(value: &str, max_len: usize) -> String { value.chars().take(max_len).collect() } +fn truncate_namespace_description(mut description: String) -> String { + if description.len() <= MAX_MCP_NAMESPACE_DESCRIPTION_BYTES { + return description; + } + + let mut end = MAX_MCP_NAMESPACE_DESCRIPTION_BYTES + .saturating_sub(MCP_NAMESPACE_DESCRIPTION_TRUNCATION_SUFFIX.len()); + while !description.is_char_boundary(end) { + end = end.saturating_sub(1); + } + description.truncate(end); + description.push_str(MCP_NAMESPACE_DESCRIPTION_TRUNCATION_SUFFIX); + description +} + fn fit_callable_parts_with_hash( namespace: &str, tool_name: &str, diff --git a/codex-rs/core/src/tools/spec_plan.rs b/codex-rs/core/src/tools/spec_plan.rs index e17ede115c..5c995b8a08 100644 --- a/codex-rs/core/src/tools/spec_plan.rs +++ b/codex-rs/core/src/tools/spec_plan.rs @@ -85,7 +85,6 @@ use codex_tools::request_user_input_available_modes; use codex_tools::shell_command_backend_for_features; use codex_tools::shell_type_for_model_and_features; use std::collections::BTreeMap; -use std::collections::HashMap; use std::collections::HashSet; use std::sync::Arc; use tracing::instrument; @@ -427,7 +426,6 @@ fn is_excluded_from_code_mode(turn_context: &TurnContext, tool_name: &ToolName) fn build_code_mode_executors( turn_context: &TurnContext, executors: &[Arc], - deferred_mcp_tools: Option<&[ToolInfo]>, ) -> Vec> { if !matches!( turn_context.tool_mode, @@ -440,17 +438,6 @@ fn build_code_mode_executors( let mut exec_prompt_tool_specs = Vec::new(); let mut deferred_tools_available = false; let deferred_tools_guidance_enabled = search_tool_enabled(turn_context); - let deferred_mcp_instructions = deferred_mcp_tools - .unwrap_or_default() - .iter() - .filter_map(|tool| { - tool.namespace_description - .as_deref() - .map(str::trim) - .filter(|instructions| !instructions.is_empty()) - .map(|instructions| (tool.canonical_tool_name(), instructions)) - }) - .collect::>(); for executor in executors { let exposure = executor.exposure(); if exposure == ToolExposure::DirectModelOnly { @@ -468,16 +455,17 @@ fn build_code_mode_executors( let mut spec = executor.spec(); if exposure == ToolExposure::Deferred { - if let Some(instructions) = deferred_mcp_instructions.get(&executor.tool_name()) - && let ToolSpec::Namespace(namespace) = &mut spec + if let ToolSpec::Namespace(namespace) = &mut spec + && !namespace.description.trim().is_empty() { + let namespace_description = namespace.description.trim(); for tool in &mut namespace.tools { let ResponsesApiNamespaceTool::Function(tool) = tool; if tool.description.trim().is_empty() { - tool.description = (*instructions).to_string(); + tool.description = namespace_description.to_string(); } else { tool.description = - format!("{}\n\n{instructions}", tool.description.trim_end()); + format!("{}\n\n{namespace_description}", tool.description.trim_end()); } } } @@ -890,11 +878,7 @@ fn prepend_code_mode_executors( planned_tools: &mut PlannedTools, ) { let turn_context = context.turn_context; - let code_mode_executors = build_code_mode_executors( - turn_context, - planned_tools.runtimes(), - context.deferred_mcp_tools, - ); + let code_mode_executors = build_code_mode_executors(turn_context, planned_tools.runtimes()); planned_tools.runtimes.splice(0..0, code_mode_executors); } diff --git a/codex-rs/core/tests/suite/code_mode.rs b/codex-rs/core/tests/suite/code_mode.rs index 31686beed9..077d9e37be 100644 --- a/codex-rs/core/tests/suite/code_mode.rs +++ b/codex-rs/core/tests/suite/code_mode.rs @@ -3329,7 +3329,7 @@ text(JSON.stringify(tool)); } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn code_mode_appends_server_instructions_to_deferred_mcp_tool_descriptions() -> Result<()> { +async fn code_mode_appends_namespace_description_to_deferred_tool_descriptions() -> Result<()> { skip_if_no_network!(Ok(())); let server = responses::start_mock_server().await;