mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
Restore shared connector metadata for Codex Apps tools (#61398)
## Why When an Apps `tools/list` response supplies connector names and descriptions in shared `openai/apps` metadata, tools need those fields restored to expose the correct namespaces and descriptions to the model. ## What changed Match tools to shared connector metadata by `connector_id` on each response page and copy nonempty names and descriptions into tool metadata before conversion. Restrict this handling to Codex Apps; regular MCP servers use the standard tool listing path. ## Testing Add a regression test with two tool pages that verifies both connectors' namespaces and descriptions appear in the model's tools state. GitOrigin-RevId: d4c8b44007773ff65d3e366669ee5c705a480cb1
This commit is contained in:
@@ -859,29 +859,51 @@ pub(crate) async fn list_tools_for_client_uncached(
|
||||
let meta = &meta;
|
||||
let first_page = params.as_ref().is_none_or(|params| params.cursor.is_none());
|
||||
async move {
|
||||
let response = client
|
||||
.list_tools_with_connector_ids(params, timeout)
|
||||
.await?;
|
||||
let (tools, next_cursor, response_meta) = if is_codex_apps_mcp_server {
|
||||
let response = client
|
||||
.list_tools_with_connector_ids(params, timeout)
|
||||
.await?;
|
||||
let tools = response
|
||||
.tools
|
||||
.into_iter()
|
||||
.map(|tool| {
|
||||
codex_apps_tool_info_from_listed_tool(
|
||||
server_name,
|
||||
server_instructions,
|
||||
tool,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
(tools, response.next_cursor, response.meta)
|
||||
} else {
|
||||
let response = client.list_tools(params, timeout).await?;
|
||||
let tools = response
|
||||
.tools
|
||||
.into_iter()
|
||||
.map(|tool| {
|
||||
regular_mcp_tool_info_from_listed_tool(
|
||||
server_name,
|
||||
server_instructions,
|
||||
tool,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
(
|
||||
tools,
|
||||
response.next_cursor,
|
||||
response.meta.map(|meta| serde_json::json!(meta)),
|
||||
)
|
||||
};
|
||||
if first_page {
|
||||
*meta
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner) =
|
||||
response.meta.map(Arc::new);
|
||||
response_meta.map(Arc::new);
|
||||
}
|
||||
Ok((response.tools, response.next_cursor))
|
||||
Ok((tools, next_cursor))
|
||||
}
|
||||
})
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|tool| {
|
||||
tool_info_from_listed_tool(
|
||||
server_name,
|
||||
is_codex_apps_mcp_server,
|
||||
server_instructions,
|
||||
tool,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
.await?;
|
||||
if is_codex_apps_mcp_server {
|
||||
emit_duration(
|
||||
MCP_TOOLS_FETCH_UNCACHED_DURATION_METRIC,
|
||||
@@ -990,19 +1012,6 @@ pub(crate) fn prepare_regular_mcp_tools_for_model(
|
||||
tools
|
||||
}
|
||||
|
||||
fn tool_info_from_listed_tool(
|
||||
server_name: &str,
|
||||
is_codex_apps_mcp_server: bool,
|
||||
server_instructions: Option<&str>,
|
||||
tool: ToolWithConnectorId,
|
||||
) -> ToolInfo {
|
||||
if is_codex_apps_mcp_server {
|
||||
codex_apps_tool_info_from_listed_tool(server_name, server_instructions, tool)
|
||||
} else {
|
||||
regular_mcp_tool_info_from_listed_tool(server_name, server_instructions, tool)
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a Codex Apps tool by preserving connector fields, removing connector prefixes from
|
||||
/// model-visible names and titles, and using the connector description for its tool namespace.
|
||||
fn codex_apps_tool_info_from_listed_tool(
|
||||
@@ -1054,18 +1063,17 @@ fn codex_apps_tool_info_from_listed_tool(
|
||||
fn regular_mcp_tool_info_from_listed_tool(
|
||||
server_name: &str,
|
||||
server_instructions: Option<&str>,
|
||||
tool: ToolWithConnectorId,
|
||||
mut tool: RmcpTool,
|
||||
) -> ToolInfo {
|
||||
let mut tool_def = tool.tool;
|
||||
strip_untrusted_connector_meta(&mut tool_def);
|
||||
strip_untrusted_connector_meta(&mut tool);
|
||||
ToolInfo {
|
||||
server_key: McpServerKey::unscoped(server_name),
|
||||
supports_parallel_tool_calls: false,
|
||||
server_origin: None,
|
||||
callable_name: tool_def.name.to_string(),
|
||||
callable_name: tool.name.to_string(),
|
||||
callable_namespace: server_name.to_string(),
|
||||
namespace_description: server_instructions.map(str::to_string),
|
||||
tool: tool_def,
|
||||
tool,
|
||||
openai_file_input_optional_fields: HashMap::new(),
|
||||
connector_id: None,
|
||||
connector_name: None,
|
||||
@@ -1778,9 +1786,8 @@ mod tests {
|
||||
let tool = tool_with_connector_meta();
|
||||
let expected_tool = tool.clone();
|
||||
|
||||
let tool_info = tool_info_from_listed_tool(
|
||||
let tool_info = codex_apps_tool_info_from_listed_tool(
|
||||
CODEX_APPS_MCP_SERVER_NAME,
|
||||
/*is_codex_apps_mcp_server*/ true,
|
||||
/*server_instructions*/ None,
|
||||
ToolWithConnectorId {
|
||||
tool,
|
||||
|
||||
@@ -4,9 +4,8 @@ use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
fn app_tool(description: &str) -> ToolInfo {
|
||||
tool_info_from_listed_tool(
|
||||
codex_apps_tool_info_from_listed_tool(
|
||||
CODEX_APPS_MCP_SERVER_NAME,
|
||||
/*is_codex_apps_mcp_server*/ true,
|
||||
/*server_instructions*/ None,
|
||||
ToolWithConnectorId {
|
||||
tool: RmcpTool::new("search", description.to_string(), Arc::default()),
|
||||
|
||||
@@ -1064,6 +1064,65 @@ startup_timeout_sec = 0.1
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn apps_tool_pages_expose_shared_connector_namespaces_to_the_model() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
let server = responses::start_mock_server().await;
|
||||
let apps_server = AppsTestServer::mount_searchable(&server).await?;
|
||||
Mock::given(method("POST"))
|
||||
.and(path_regex("^/api/codex/ps/mcp/?$"))
|
||||
.and(body_partial_json(json!({"method": "tools/list"})))
|
||||
.respond_with(|request: &Request| {
|
||||
let body: Value = request.body_json().expect("tools/list request");
|
||||
let cursor = body["params"]["cursor"].as_str();
|
||||
let (id, name, description) = match cursor {
|
||||
None => ("connector_a", "Calendar", "Plan events."),
|
||||
Some("page2") => ("connector_b", "Notes", "Find notes."),
|
||||
_ => panic!("unexpected tools/list cursor: {cursor:?}"),
|
||||
};
|
||||
let mut result = json!({
|
||||
"tools": [{
|
||||
"name": format!("{id}.search"),
|
||||
"description": "Search records.",
|
||||
"inputSchema": {"type": "object"},
|
||||
"_meta": {"connector_id": id}
|
||||
}],
|
||||
"_meta": {"openai/apps": [{
|
||||
"connectorId": id,
|
||||
"connectorName": name,
|
||||
"connectorDescription": description
|
||||
}]}
|
||||
});
|
||||
if cursor.is_none() {
|
||||
result["nextCursor"] = json!("page2");
|
||||
}
|
||||
ResponseTemplate::new(200).set_body_json(json!({
|
||||
"jsonrpc": "2.0", "id": body["id"], "result": result
|
||||
}))
|
||||
})
|
||||
.with_priority(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
let response = mount_sse_sequence(&server, completed_response_sequence(/*count*/ 1)).await;
|
||||
let mut builder = search_capable_apps_builder(apps_server.chatgpt_base_url)
|
||||
.with_config(configure_deferred_tool_world_state_without_agents);
|
||||
let test = builder.build_with_auto_env(&server).await?;
|
||||
wait_for_mcp_server(&test.codex, CODEX_APPS_MCP_SERVER_NAME).await?;
|
||||
test.submit_turn("Which apps are available?").await?;
|
||||
|
||||
let tools_state = tools_state_sections(&response.single_request()).join("\n");
|
||||
assert!(
|
||||
tools_state.contains("- mcp__codex_apps__calendar: Plan events."),
|
||||
"{tools_state}"
|
||||
);
|
||||
assert!(
|
||||
tools_state.contains("- mcp__codex_apps__notes: Find notes."),
|
||||
"{tools_state}"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn elevated_apps_catalog_limit_requires_host_owned_registration() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
@@ -775,6 +775,7 @@ impl RmcpClient {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// List a page of Codex Apps tools and restore any shared connector metadata.
|
||||
#[instrument(level = "trace", skip_all)]
|
||||
pub async fn list_tools_with_connector_ids(
|
||||
&self,
|
||||
@@ -782,24 +783,49 @@ impl RmcpClient {
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<ListToolsWithConnectorIdResult> {
|
||||
let result = self.list_tools(params, timeout).await?;
|
||||
let apps: HashMap<_, _> = result
|
||||
.meta
|
||||
.as_ref()
|
||||
.and_then(|meta| meta.get("openai/apps"))
|
||||
.and_then(Value::as_array)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(|app| Some((app["connectorId"].as_str()?, app)))
|
||||
.collect();
|
||||
let tools = result
|
||||
.tools
|
||||
.into_iter()
|
||||
.map(|tool| {
|
||||
.map(|mut tool| {
|
||||
let connector_id = Self::meta_string(tool.meta.as_ref(), "connector_id");
|
||||
if let Some(app) = connector_id.as_deref().and_then(|id| apps.get(id))
|
||||
&& let Some(meta) = &mut tool.meta
|
||||
{
|
||||
// Preserve the wire strings for callers that read the raw tool metadata.
|
||||
for (shared_key, tool_key) in [
|
||||
("connectorName", "connector_name"),
|
||||
("connectorDescription", "connector_description"),
|
||||
] {
|
||||
if let Some(value) = app[shared_key]
|
||||
.as_str()
|
||||
.filter(|value| !value.trim().is_empty())
|
||||
{
|
||||
meta.insert(tool_key.into(), Value::from(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
let meta = tool.meta.as_ref();
|
||||
let connector_id = Self::meta_string(meta, "connector_id");
|
||||
let connector_name = Self::meta_string(meta, "connector_name")
|
||||
.or_else(|| Self::meta_string(meta, "connector_display_name"));
|
||||
let connector_description = Self::meta_string(meta, "connector_description")
|
||||
.or_else(|| Self::meta_string(meta, "connectorDescription"));
|
||||
Ok(ToolWithConnectorId {
|
||||
ToolWithConnectorId {
|
||||
tool,
|
||||
connector_id,
|
||||
connector_name,
|
||||
connector_description,
|
||||
})
|
||||
}
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
.collect();
|
||||
Ok(ListToolsWithConnectorIdResult {
|
||||
meta: result.meta.map(|meta| serde_json::json!(meta)),
|
||||
next_cursor: result.next_cursor,
|
||||
|
||||
Reference in New Issue
Block a user