Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/test-suite-registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,7 @@
{
"id": "id.rust.crate-integration",
"paths": [
"pkgs/id/crates/id-caps/tests/mcp_bridge_surface.rs",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Wire the new integration target into its registered venue

The registry now lists this test as covered, but the registered venue does not execute it: pkgs/id/flake.nix:383 delegates to test-crate-integration, and pkgs/id/justfile:300-305 still enumerates only the five existing integration targets without referencing mcp_bridge_surface. Consequently, that check can pass without exercising either new bridge test. Add the new target to the recipe rather than only updating the inventory.

Useful? React with 👍 / 👎.

"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",
Expand All @@ -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",
Expand Down
1 change: 1 addition & 0 deletions pkgs/id/crates/id-caps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
126 changes: 126 additions & 0 deletions pkgs/id/crates/id-caps/src/mcp_bridge.rs
Original file line number Diff line number Diff line change
@@ -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<ToolDefinition> {
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<Self, BridgeError> {
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())
}
40 changes: 40 additions & 0 deletions pkgs/id/crates/id-caps/tests/mcp_bridge_surface.rs
Original file line number Diff line number Diff line change
@@ -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();
Comment on lines +28 to +32

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add property coverage for the definition-pin invariants

This test exercises only one fixed mutation of the first tool's ceiling, leaving the new serializer-backed state machine without the property coverage required by the repository's testing doctrine. Generate definition sets and mutations across names, commands, ceilings, ordering, and membership, then check verification sequences against a live/terminated model: unchanged definitions must succeed before termination, changed definitions must terminate, and termination must remain sticky. The fixed example can remain as acceptance coverage, but does not replace the required invariant tests.

AGENTS.md reference: AGENTS.md:L37-L39

Useful? React with 👍 / 👎.


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());
}