Repository navigation
id: add pinned MCP bridge surface for #7914 #12917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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()) | ||
| } |
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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()); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The registry now lists this test as covered, but the registered venue does not execute it:
pkgs/id/flake.nix:383delegates totest-crate-integration, andpkgs/id/justfile:300-305still enumerates only the five existing integration targets without referencingmcp_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 👍 / 👎.