mirror of
https://github.com/openai/codex.git
synced 2026-11-22 23:00:42 +00:00
Use model MCP metadata path
This commit is contained in:
@@ -343,7 +343,6 @@ use codex_login::complete_device_code_login;
|
||||
use codex_login::login_with_api_key;
|
||||
use codex_login::request_device_code;
|
||||
use codex_login::run_login_server;
|
||||
use codex_mcp::CODEX_APPS_MCP_SERVER_NAME;
|
||||
use codex_mcp::McpRuntimeContext;
|
||||
use codex_mcp::McpServerStatusSnapshot;
|
||||
use codex_mcp::McpSnapshotDetail;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use super::*;
|
||||
|
||||
const MCP_TOOL_THREAD_ID_META_KEY: &str = "threadId";
|
||||
const SEARCH_SERVICE_TOOL_NAME_PREFIX: &str = "search_service.";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct McpRequestProcessor {
|
||||
@@ -403,14 +402,6 @@ impl McpRequestProcessor {
|
||||
let thread_id = params.thread_id.clone();
|
||||
let (_, thread) = self.load_thread(&thread_id).await?;
|
||||
let meta = with_mcp_tool_call_thread_id_meta(params.meta, &thread_id);
|
||||
let meta = if should_forward_turn_metadata_to_mcp_tool(¶ms.server, ¶ms.tool) {
|
||||
with_mcp_tool_call_turn_metadata_meta(
|
||||
meta,
|
||||
thread.current_mcp_request_turn_metadata().await,
|
||||
)
|
||||
} else {
|
||||
meta
|
||||
};
|
||||
let request_id = request_id.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
@@ -425,38 +416,6 @@ impl McpRequestProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
fn should_forward_turn_metadata_to_mcp_tool(server: &str, tool: &str) -> bool {
|
||||
server == CODEX_APPS_MCP_SERVER_NAME && tool.starts_with(SEARCH_SERVICE_TOOL_NAME_PREFIX)
|
||||
}
|
||||
|
||||
fn with_mcp_tool_call_turn_metadata_meta(
|
||||
meta: Option<serde_json::Value>,
|
||||
turn_metadata: Option<serde_json::Value>,
|
||||
) -> Option<serde_json::Value> {
|
||||
let Some(turn_metadata) = turn_metadata else {
|
||||
return meta;
|
||||
};
|
||||
|
||||
match meta {
|
||||
Some(serde_json::Value::Object(mut map)) => {
|
||||
map.insert(
|
||||
codex_core::X_CODEX_TURN_METADATA_HEADER.to_string(),
|
||||
turn_metadata,
|
||||
);
|
||||
Some(serde_json::Value::Object(map))
|
||||
}
|
||||
None => {
|
||||
let mut map = serde_json::Map::new();
|
||||
map.insert(
|
||||
codex_core::X_CODEX_TURN_METADATA_HEADER.to_string(),
|
||||
turn_metadata,
|
||||
);
|
||||
Some(serde_json::Value::Object(map))
|
||||
}
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
fn with_mcp_tool_call_thread_id_meta(
|
||||
meta: Option<serde_json::Value>,
|
||||
thread_id: &str,
|
||||
@@ -480,28 +439,3 @@ fn with_mcp_tool_call_thread_id_meta(
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn turn_metadata_is_forwarded_only_to_codex_apps_search_service_tools() {
|
||||
assert!(should_forward_turn_metadata_to_mcp_tool(
|
||||
CODEX_APPS_MCP_SERVER_NAME,
|
||||
"search_service.web_run",
|
||||
));
|
||||
assert!(should_forward_turn_metadata_to_mcp_tool(
|
||||
CODEX_APPS_MCP_SERVER_NAME,
|
||||
"search_service._web_run",
|
||||
));
|
||||
assert!(!should_forward_turn_metadata_to_mcp_tool(
|
||||
CODEX_APPS_MCP_SERVER_NAME,
|
||||
"gmail.send_email",
|
||||
));
|
||||
assert!(!should_forward_turn_metadata_to_mcp_tool(
|
||||
"custom_mcp_server",
|
||||
"search_service.web_run",
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ use crate::goals::GoalRuntimeEvent;
|
||||
use crate::session::Codex;
|
||||
use crate::session::SessionSettingsUpdate;
|
||||
use crate::session::SteerInputError;
|
||||
use crate::turn_metadata::McpTurnMetadataContext;
|
||||
use codex_features::Feature;
|
||||
use codex_otel::SessionTelemetry;
|
||||
use codex_protocol::config_types::ApprovalsReviewer;
|
||||
@@ -503,18 +502,6 @@ impl CodexThread {
|
||||
self.codex.session.get_config().await
|
||||
}
|
||||
|
||||
pub async fn current_mcp_request_turn_metadata(&self) -> Option<serde_json::Value> {
|
||||
let active = self.codex.session.active_turn.lock().await;
|
||||
let (_, active_task) = active.as_ref()?.tasks.first()?;
|
||||
let turn_context = active_task.turn_context.as_ref();
|
||||
turn_context
|
||||
.turn_metadata_state
|
||||
.current_meta_value_for_mcp_request(McpTurnMetadataContext {
|
||||
model: turn_context.model_info.slug.as_str(),
|
||||
reasoning_effort: turn_context.effective_reasoning_effort(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Refresh the thread's layer-backed user config state from a caller-supplied
|
||||
/// config snapshot. Thread-scoped layers and session-static settings remain
|
||||
/// unchanged.
|
||||
|
||||
@@ -1089,6 +1089,43 @@ async fn mcp_tool_call_request_meta_includes_turn_started_at_unix_ms() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn mcp_tool_call_request_meta_includes_responsesapi_client_metadata() {
|
||||
let (_, turn_context) = make_session_and_context().await;
|
||||
let mcp_request_meta = serde_json::json!({
|
||||
"user_location": {
|
||||
"country": "US",
|
||||
"region": "CA",
|
||||
"city": "San Francisco",
|
||||
},
|
||||
})
|
||||
.to_string();
|
||||
turn_context
|
||||
.turn_metadata_state
|
||||
.set_responsesapi_client_metadata(HashMap::from([(
|
||||
"mcp_request_meta".to_string(),
|
||||
mcp_request_meta.clone(),
|
||||
)]));
|
||||
|
||||
let meta = build_mcp_tool_call_request_meta(
|
||||
&turn_context,
|
||||
CODEX_APPS_MCP_SERVER_NAME,
|
||||
"call-custom",
|
||||
/*metadata*/ None,
|
||||
)
|
||||
.expect("Codex Apps MCP tools should receive turn metadata");
|
||||
let turn_metadata = meta
|
||||
.get(crate::X_CODEX_TURN_METADATA_HEADER)
|
||||
.expect("turn metadata should be present");
|
||||
|
||||
assert_eq!(
|
||||
turn_metadata
|
||||
.get("mcp_request_meta")
|
||||
.and_then(serde_json::Value::as_str),
|
||||
Some(mcp_request_meta.as_str())
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn plugin_mcp_tool_call_request_meta_includes_plugin_id() {
|
||||
let (_, turn_context) = make_session_and_context().await;
|
||||
|
||||
@@ -443,6 +443,14 @@ async fn tool_search_returns_deferred_tools_without_follow_up_tool_injection() -
|
||||
|
||||
let mut builder = configured_builder(apps_server.chatgpt_base_url.clone());
|
||||
let test = builder.build(&server).await?;
|
||||
let mcp_request_meta = json!({
|
||||
"user_location": {
|
||||
"country": "US",
|
||||
"region": "CA",
|
||||
"city": "San Francisco",
|
||||
},
|
||||
})
|
||||
.to_string();
|
||||
test.codex
|
||||
.submit(Op::UserInput {
|
||||
environments: None,
|
||||
@@ -451,7 +459,10 @@ async fn tool_search_returns_deferred_tools_without_follow_up_tool_injection() -
|
||||
text_elements: Vec::new(),
|
||||
}],
|
||||
final_output_json_schema: None,
|
||||
responsesapi_client_metadata: None,
|
||||
responsesapi_client_metadata: Some(HashMap::from([(
|
||||
"mcp_request_meta".to_string(),
|
||||
mcp_request_meta.clone(),
|
||||
)])),
|
||||
thread_settings: Default::default(),
|
||||
})
|
||||
.await?;
|
||||
@@ -566,6 +577,10 @@ async fn tool_search_returns_deferred_tools_without_follow_up_tool_injection() -
|
||||
mcp_turn_started_at_unix_ms > 0,
|
||||
"apps tools/call should include a positive turn_started_at_unix_ms: {apps_tool_call:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
apps_tool_call.pointer("/params/_meta/x-codex-turn-metadata/mcp_request_meta"),
|
||||
Some(&json!(mcp_request_meta))
|
||||
);
|
||||
|
||||
let first_request_turn_metadata: Value = serde_json::from_str(
|
||||
&requests[0]
|
||||
|
||||
Reference in New Issue
Block a user