# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-mcp-oauth-2-1-resource-indicators-as-per-tool-consent-boundaries-rather-than-per-server-grants.md # Remnant Warrant: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-mcp-oauth-2-1-resource-indicators-as-per-tool-consent-boundaries-rather-than-per-server-grants.warrant.json # Markdown SHA-256: b5874e1b844ac8c2f14c3fbb483a0452cdb5755126b0214e4cee57015039e8a5 # Code AST SHA-256: e8875749ad197dd558f6075d5f926e03aa81361b09d312ddba920df0e5370cda # Verified Classes: ToolAudienceMismatch, HomogeneityViolation, ToolPrivilege # Defensive Guards: 4 fail-closed invariants (zero external deps) # Governance: Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com) import hashlib from dataclasses import dataclass from typing import Mapping class ToolAudienceMismatch(RuntimeError): """Raised when a token's resource indicator does not cover the invoked tool.""" class HomogeneityViolation(RuntimeError): """Raised when a server configures per-server resource but hosts heterogeneous tools.""" @dataclass(frozen=True) class ToolPrivilege: name: str resource_uri: str # the audience the token MUST carry reversible: bool egress: bool blast_radius: int # 1 = single record, 10 = org-wide def build_tool_resource_map(tools: list[ToolPrivilege]) -> Mapping[str, str]: """Static map from tool name to the resource indicator that authorizes it. The map's cardinality of distinct resource_uri values must equal the cardinality of privilege classes, or the per-server model is unsafe. """ classes = { (t.reversible, t.egress, t.blast_radius) for t in tools } resources = {t.resource_uri for t in tools} if len(classes) > 1 and len(resources) == 1: raise HomogeneityViolation( f"{len(classes)} privilege classes collapse into a single resource " f"indicator {next(iter(resources))!r}; per-tool audiences required" ) return {t.name: t.resource_uri for t in tools} def authorize_tool_call( token_aud: str, token_scope: set[str], tool: ToolPrivilege, resource_map: Mapping[str, str], ) -> None: """Enforce the per-tool audience invariant before dispatch. This runs BEFORE the tool executes. A failure here must not fall through to a retry loop; the agent must surface it as a consent event. """ expected = resource_map.get(tool.name) if expected is None: raise ToolAudienceMismatch( f"tool {tool.name!r} has no registered resource indicator; " f"refusing to dispatch" ) if token_aud != expected: raise ToolAudienceMismatch( f"token aud={token_aud!r} does not authorize tool {tool.name!r} " f"(requires aud={expected!r}); agent must re-enter consent flow, " f"not retry" ) # Scope check is necessary but not sufficient; audience is the primary gate. required_scope = f"tool:{tool.name}" if required_scope not in token_scope: raise ToolAudienceMismatch( f"token missing tool-specific scope {required_scope!r}" ) def fingerprint_consent(tool: ToolPrivilege, user_id: str) -> str: """Bind the consent record to the exact tool, not the scope string.""" payload = f"{user_id}|{tool.name}|{tool.resource_uri}|{tool.blast_radius}" return hashlib.sha256(payload.encode()).hexdigest()[:16] # Example: the observed failure, now bounded. TOOLS = [ ToolPrivilege("read_file", "https://mcp.internal/vcs/read_file", True, False, 1), ToolPrivilege("push", "https://mcp.internal/vcs/push", False, True, 3), ToolPrivilege("force_push","https://mcp.internal/vcs/force_push",False, True, 8), ] RESOURCE_MAP = build_tool_resource_map(TOOLS) # Token minted for the server (the failure mode): aud is the server URI. compromised_token_aud = "https://mcp.internal/vcs" try: authorize_tool_call( token_aud=compromised_token_aud, token_scope={"repo:write", "tool:force_push"}, tool=TOOLS[2], resource_map=RESOURCE_MAP, ) except ToolAudienceMismatch as e: # Correct behavior: reject, escalate, do not retry. print(f"BLOCKED: {e}")