mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Preserve executor context for MCP stop hooks (#40012)
## What changed - Scope executor-provided stop-hook calls to the MCP server environment that registered the hook, and reject calls when that environment does not match. - Forward turn metadata with executor stop-hook requests while retaining the hook call's `threadId` metadata. ## Testing - Cover environment matching for MCP tool calls and verify executor stop hooks receive their environment ID and request metadata. GitOrigin-RevId: b544ea3a6ca1f61389fb4597978ed7aac27d529a
This commit is contained in:
committed by
copyberry
parent
8b5beea5c0
commit
ab8768306f
@@ -854,10 +854,12 @@ impl McpConnectionSet {
|
||||
}
|
||||
|
||||
/// Invoke the tool indicated by the (server, tool) pair.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn call_tool(
|
||||
&self,
|
||||
server: &str,
|
||||
tool: &str,
|
||||
environment_id: Option<&str>,
|
||||
arguments: Option<serde_json::Value>,
|
||||
meta: Option<serde_json::Value>,
|
||||
requested_timeout: Option<Duration>,
|
||||
@@ -867,6 +869,14 @@ impl McpConnectionSet {
|
||||
.servers
|
||||
.get(server)
|
||||
.ok_or_else(|| anyhow!("unknown MCP server '{server}'"))?;
|
||||
if let Some(environment_id) = environment_id
|
||||
&& view.metadata.environment_id != environment_id
|
||||
{
|
||||
bail!(
|
||||
"MCP server `{server}` is running in environment `{}`, expected `{environment_id}`",
|
||||
view.metadata.environment_id
|
||||
);
|
||||
}
|
||||
if !view.tool_filter.allows(tool) {
|
||||
return Err(anyhow!(
|
||||
"tool '{tool}' is disabled for MCP server '{server}'"
|
||||
|
||||
@@ -2674,6 +2674,7 @@ async fn call_tool_requires_connection_without_waiting_for_startup() {
|
||||
manager.call_tool(
|
||||
"docs",
|
||||
"search",
|
||||
/*environment_id*/ None,
|
||||
/*arguments*/ None,
|
||||
/*meta*/ None,
|
||||
Some(Duration::from_secs(5)),
|
||||
@@ -2697,6 +2698,7 @@ async fn call_tool_requires_connection_without_waiting_for_startup() {
|
||||
.call_tool(
|
||||
"docs",
|
||||
"search",
|
||||
/*environment_id*/ None,
|
||||
/*arguments*/ None,
|
||||
/*meta*/ None,
|
||||
Some(Duration::from_secs(5)),
|
||||
@@ -2731,6 +2733,7 @@ async fn connected_call_respects_server_tool_filters() {
|
||||
.call_tool(
|
||||
"docs",
|
||||
"search",
|
||||
/*environment_id*/ None,
|
||||
/*arguments*/ None,
|
||||
/*meta*/ None,
|
||||
Some(Duration::from_secs(5)),
|
||||
@@ -2741,6 +2744,60 @@ async fn connected_call_respects_server_tool_filters() {
|
||||
assert!(filtered_error.to_string().contains("disabled"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn call_tool_validates_environment_without_waiting_for_ready_connections() {
|
||||
let client = create_test_managed_client(vec![create_test_tool("docs", "search")]).await;
|
||||
let (client, _, _) = create_gated_async_managed_client(client);
|
||||
let approval_policy = Constrained::allow_any(AskForApproval::OnRequest);
|
||||
let permission_profile = Constrained::allow_any(PermissionProfile::default());
|
||||
let mut manager = McpConnectionSet::new_uninitialized(
|
||||
&approval_policy,
|
||||
&permission_profile,
|
||||
/*prefix_mcp_tool_names*/ true,
|
||||
);
|
||||
manager.insert_test_client("docs", client);
|
||||
manager
|
||||
.servers
|
||||
.get_mut("docs")
|
||||
.expect("test server should exist")
|
||||
.metadata
|
||||
.environment_id = "executor-a".to_string();
|
||||
|
||||
let mismatched_environment = manager
|
||||
.call_tool(
|
||||
"docs",
|
||||
"search",
|
||||
Some("executor-b"),
|
||||
/*arguments*/ None,
|
||||
/*meta*/ None,
|
||||
Some(Duration::from_secs(5)),
|
||||
/*wait_for_server*/ false,
|
||||
)
|
||||
.await
|
||||
.expect_err("calls must reject a server from a different environment");
|
||||
assert_eq!(
|
||||
mismatched_environment.to_string(),
|
||||
"MCP server `docs` is running in environment `executor-a`, expected `executor-b`"
|
||||
);
|
||||
|
||||
let pending_call = tokio::time::timeout(
|
||||
Duration::from_millis(50),
|
||||
manager.call_tool(
|
||||
"docs",
|
||||
"search",
|
||||
Some("executor-a"),
|
||||
/*arguments*/ None,
|
||||
/*meta*/ None,
|
||||
Some(Duration::from_secs(5)),
|
||||
/*wait_for_server*/ false,
|
||||
),
|
||||
)
|
||||
.await
|
||||
.expect("environment-scoped calls must not wait for pending server startup")
|
||||
.expect_err("pending server must not accept environment-scoped calls");
|
||||
assert!(pending_call.to_string().contains("not connected"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn list_all_tools_resolves_server_catalogs_concurrently() {
|
||||
let first_client = create_test_managed_client(vec![create_test_tool("first", "search")]).await;
|
||||
|
||||
@@ -471,10 +471,12 @@ impl McpRuntime {
|
||||
self.latest_connections().list_all_tools().await
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn latest_call_tool(
|
||||
&self,
|
||||
server: &str,
|
||||
tool: &str,
|
||||
environment_id: Option<&str>,
|
||||
arguments: Option<serde_json::Value>,
|
||||
meta: Option<serde_json::Value>,
|
||||
requested_timeout: Option<Duration>,
|
||||
@@ -484,6 +486,7 @@ impl McpRuntime {
|
||||
.call_tool(
|
||||
server,
|
||||
tool,
|
||||
environment_id,
|
||||
arguments,
|
||||
meta,
|
||||
requested_timeout,
|
||||
|
||||
Reference in New Issue
Block a user