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:
Krish Chainani authored and copyberry committed 2026-08-21 21:17:42 +00:00
1 parent 8b5beea5c0
commit ab8768306f
14 files changed
+157 -6

No files matched your search

@@ -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}'"