# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-that-mcp-oauth-2-1-resource-server-binding-has-silently-decoupled-from-per-tool-scoped-consent.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-that-mcp-oauth-2-1-resource-server-binding-has-silently-decoupled-from-per-tool-scoped-consent.warrant.json
# Markdown SHA-256:   763d7eef06c8f404e1de99de94cf093c3c082f511b72da1830441bd4f7406553
# Code AST SHA-256:   de28d8d44d057f105724b01b7ab2a51a9590e4b1b14b9a24c2dc0347c2d7af8a
# Verified Classes:   StaleScopeProjectionError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class StaleScopeProjectionError(RuntimeError):
    """Raised when a token's scope claim, resolved against the current
    tool-to-scope map, does not cover the requested tool — i.e. the
    resource-server binding has decoupled from per-tool scoped consent."""
    pass


def authorize_tool_call(token: dict, tool_name: str, current_scope_map: dict,
                        map_version_at_iat: int, current_map_version: int) -> None:
    # 1. Audience binding (RFC 8707). Necessary, NOT sufficient.
    if token.get("aud") != "https://mcp.example.internal":
        raise StaleScopeProjectionError(
            f"audience mismatch for tool {tool_name!r}: {token.get('aud')!r}"
        )

    # 2. Structural signal: map version skew across the token lifetime.
    if map_version_at_iat != current_map_version:
        # A narrowing may have occurred; we cannot trust a cached projection.
        # Fall through to per-call resolution against the CURRENT map.

        pass

    # 3. Resolve the tool's gating scope against the CURRENT map, per call.
    required_scope = current_scope_map.get(tool_name)
    if required_scope is None:
        raise StaleScopeProjectionError(
            f"tool {tool_name!r} absent from current scope map "
            f"(version {current_map_version})"
        )

    granted = set(token.get("scope", "").split())
    if required_scope not in granted:
        # This is the silent-pass boundary: a correctly-audienced token
        # whose scope no longer projects onto this tool under the current map.
        raise StaleScopeProjectionError(
            f"scope projection drift: tool {tool_name!r} now requires "
            f"{required_scope!r} (map v{current_map_version}); token carries "
            f"{sorted(granted)} minted at map v{map_version_at_iat}"
        )


def handle_tools_call(token: dict, tool_name: str, current_scope_map: dict,
                      map_version_at_iat: int, current_map_version: int) -> str:
    authorize_tool_call(token, tool_name, current_scope_map,
                        map_version_at_iat, current_map_version)
    return f"executed {tool_name}"


# Reproduces the observed failure: token minted under map v1 where
# repo:rw -> {repo.read, repo.write.file, repo.write.branch};
# current map v2 narrows repo.write.branch to repo:branch.
token_v1 = {"aud": "https://mcp.example.internal", "scope": "repo:rw"}
current_map_v2 = {
    "repo.read": "repo:rw",
    "repo.write.file": "repo:rw",
    "repo.write.branch": "repo:branch",   # narrowed
}
handle_tools_call(token_v1, "repo.write.branch", current_map_v2,
                  map_version_at_iat=1, current_map_version=2)
# -> raises StaleScopeProjectionError instead of silently mutating a branch
