From 3bd490390c59b3df93c6b19f6bb99203a3857921 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 22 Nov 2026 23:01:41 -0600 Subject: [PATCH] id: add pinned MCP bridge surface for #7914 --- .github/test-suite-registry.json | 3 +- pkgs/id/crates/id-caps/src/lib.rs | 1 + pkgs/id/crates/id-caps/src/mcp_bridge.rs | 126 ++++++++++++++++++ .../id-caps/tests/mcp_bridge_surface.rs | 40 ++++++ 4 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 pkgs/id/crates/id-caps/src/mcp_bridge.rs create mode 100644 pkgs/id/crates/id-caps/tests/mcp_bridge_surface.rs diff --git a/.github/test-suite-registry.json b/.github/test-suite-registry.json index 64362da9ed..433f61556e 100644 --- a/.github/test-suite-registry.json +++ b/.github/test-suite-registry.json @@ -1497,6 +1497,7 @@ { "id": "id.rust.crate-integration", "paths": [ + "pkgs/id/crates/id-caps/tests/mcp_bridge_surface.rs", "pkgs/id/crates/id-caps/tests/roc_import_policy.rs", "pkgs/id/crates/id-cli/tests/aliases.rs", "pkgs/id/crates/id-core/tests/probe_petname_load.rs", @@ -1511,7 +1512,7 @@ "dispatchable": true }, "status": "mapped", - "notes": "Wired by #10329: test-crate-integration is a named pkgs/id flake check included in the id PR-check list; its just recipe runs all five member-crate targets, including the four named by the issue and id-caps/roc_import_policy." + "notes": "Wired by #10329: test-crate-integration is a named pkgs/id flake check included in the id PR-check list; its just recipe runs all six member-crate targets, including the MCP bridge surface, the four named by the issue and id-caps/roc_import_policy." }, { "id": "id.web.build-manifest", diff --git a/pkgs/id/crates/id-caps/src/lib.rs b/pkgs/id/crates/id-caps/src/lib.rs index 2d64a8ade8..ed7e46a640 100644 --- a/pkgs/id/crates/id-caps/src/lib.rs +++ b/pkgs/id/crates/id-caps/src/lib.rs @@ -16,6 +16,7 @@ pub mod http_fetch_url; pub mod library_gate; pub mod library_manifest; pub mod library_resolution; +pub mod mcp_bridge; pub mod portal; pub mod used_caps; pub mod world_caps; diff --git a/pkgs/id/crates/id-caps/src/mcp_bridge.rs b/pkgs/id/crates/id-caps/src/mcp_bridge.rs new file mode 100644 index 0000000000..889f796449 --- /dev/null +++ b/pkgs/id/crates/id-caps/src/mcp_bridge.rs @@ -0,0 +1,126 @@ +//! The authority-free MCP bridge surface for the agent-in-worlds spike. +//! +//! The bridge is a translator: the world host remains responsible for +//! credentials, authority, quota, and audit. This module only defines the +//! narrow v1 tool surface and the session-start definition pin. + +use serde::{Deserialize, Serialize}; +use sha2::{Digest as _, Sha256}; + +/// Names that are deliberately not tools on the bridge surface. +pub const FORBIDDEN_TOOL_NAMES: [&str; 5] = [ + "capability.mint", + "credential.read", + "audit.mutate", + "world.export", + "host.exec", +]; + +/// A v1 tool definition exposed to an MCP client. +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +pub struct ToolDefinition { + /// Stable MCP tool name. + pub name: String, + /// F13.1 world command the host will receive after authority checks. + pub command: String, + /// Capability ceiling key enforced by the world host. + pub ceiling: String, +} + +impl ToolDefinition { + fn new(name: &str, command: &str, ceiling: &str) -> Self { + Self { + name: name.to_owned(), + command: command.to_owned(), + ceiling: ceiling.to_owned(), + } + } +} + +/// The seven approved v1 MCP tools, in their pinned wire order. +#[must_use] +pub fn v1_tool_definitions() -> Vec { + vec![ + ToolDefinition::new("world.join", "session.presence", "presence"), + ToolDefinition::new("world.leave", "session.presence", "presence"), + ToolDefinition::new("screen.read", "screen.read", "screen.read"), + ToolDefinition::new("screen.subscribe", "screen.subscribe", "screen.read"), + ToolDefinition::new("object.inspect", "object.inspect", "screen.read"), + ToolDefinition::new("chat.post", "chat.post", "chat.post"), + ToolDefinition::new("presence.set", "presence.set", "presence"), + ] +} + +/// Errors that terminate a bridge session before a changed tool definition +/// can be used as a poisoning or shadowing vector. +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum BridgeError { + /// The definition set changed after the session was pinned. + DefinitionChanged { + /// Hash recorded at session start. + expected: [u8; 32], + /// Hash observed during the later check. + actual: [u8; 32], + }, + /// The definition set could not be serialized for hashing. + InvalidDefinitions, +} + +/// A bridge session with its tool-definition hash pinned at start. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct BridgeSession { + pinned_definition_hash: [u8; 32], + terminated: bool, +} + +impl BridgeSession { + /// Start a session and pin the exact ordered definition bytes. + pub fn start(definitions: &[ToolDefinition]) -> Result { + Ok(Self { + pinned_definition_hash: definition_hash(definitions)?, + terminated: false, + }) + } + + /// Check that the server still serves the pinned definitions. + /// + /// Once a mismatch is observed the session stays terminated, even if a + /// later check happens to present the original definitions. + pub fn verify_definitions( + &mut self, + definitions: &[ToolDefinition], + ) -> Result<(), BridgeError> { + if self.terminated { + return Err(BridgeError::DefinitionChanged { + expected: self.pinned_definition_hash, + actual: self.pinned_definition_hash, + }); + } + let actual = definition_hash(definitions)?; + if actual != self.pinned_definition_hash { + self.terminated = true; + return Err(BridgeError::DefinitionChanged { + expected: self.pinned_definition_hash, + actual, + }); + } + Ok(()) + } + + /// Whether a definition mismatch has terminated this session. + #[must_use] + pub const fn is_terminated(&self) -> bool { + self.terminated + } + + /// The pinned hash, for audit/debug output without exposing definitions. + #[must_use] + pub const fn pinned_definition_hash(&self) -> [u8; 32] { + self.pinned_definition_hash + } +} + +fn definition_hash(definitions: &[ToolDefinition]) -> Result<[u8; 32], BridgeError> { + let bytes = serde_json::to_vec(definitions).map_err(|_| BridgeError::InvalidDefinitions)?; + Ok(Sha256::digest(bytes).into()) +} diff --git a/pkgs/id/crates/id-caps/tests/mcp_bridge_surface.rs b/pkgs/id/crates/id-caps/tests/mcp_bridge_surface.rs new file mode 100644 index 0000000000..30d9d982dd --- /dev/null +++ b/pkgs/id/crates/id-caps/tests/mcp_bridge_surface.rs @@ -0,0 +1,40 @@ +//! Surface oracle for #7914: only the seven approved tools are exposed, and +//! a mid-session definition change terminates the bridge. + +use id_caps::mcp_bridge::{BridgeError, BridgeSession, FORBIDDEN_TOOL_NAMES, v1_tool_definitions}; + +#[test] +fn v1_surface_excludes_forbidden_non_tools() { + let definitions = v1_tool_definitions(); + let names: Vec<&str> = definitions + .iter() + .map(|definition| definition.name.as_str()) + .collect(); + + assert_eq!(names.len(), 7); + assert!(names.contains(&"world.join")); + assert!(names.contains(&"world.leave")); + assert!(names.contains(&"screen.read")); + assert!(names.contains(&"screen.subscribe")); + assert!(names.contains(&"object.inspect")); + assert!(names.contains(&"chat.post")); + assert!(names.contains(&"presence.set")); + for forbidden in FORBIDDEN_TOOL_NAMES { + assert!(!names.contains(&forbidden)); + } +} + +#[test] +fn tampered_definition_terminates_session() { + let definitions = v1_tool_definitions(); + let mut session = BridgeSession::start(&definitions).expect("the static surface serializes"); + let mut tampered = definitions.clone(); + tampered[0].ceiling = "credential.read".to_owned(); + + let error = session + .verify_definitions(&tampered) + .expect_err("tampering must terminate"); + assert!(matches!(error, BridgeError::DefinitionChanged { .. })); + assert!(session.is_terminated()); + assert!(session.verify_definitions(&definitions).is_err()); +}