# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-malicious-mcp-tool-description-versus-a-legitimate-but-verbose-one-and-where-registry-trust-boundaries-actually-bre.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-malicious-mcp-tool-description-versus-a-legitimate-but-verbose-one-and-where-registry-trust-boundaries-actually-bre.warrant.json
# Markdown SHA-256:   bb3b33106d48c80c8cdc9e77c0845bc94170043ef0954c96736c4bd081a393d8
# Code AST SHA-256:   e1e0842ad5cb930d0f75c9e15243e14dafd6f37d109a1927fd0de7e946d351ae
# Verified Classes:   ToolManifestPoisoning, ToolManifest
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Mapping


class ToolManifestPoisoning(RuntimeError):
    """Raised when a discovered MCP tool manifest violates the trust invariant."""


@dataclass(frozen=True)
class ToolManifest:
    name: str
    version: str
    description: str
    input_schema: Mapping
    publisher_signature: str | None
    content_hash: str


def admit_tool(
    manifest: ToolManifest,
    *,
    pinned_hashes: Mapping[tuple[str, str], str],
    trusted_publishers: frozenset[str],
    declared_publisher: str,
) -> ToolManifest:
    # C2: identity must be bound to bytes by a trusted publisher signature.
    if manifest.publisher_signature is None:
        raise ToolManifestPoisoning(
            f"tool {manifest.name}@{manifest.version} has no publisher signature; "
            "content-identical registry poisoning is undetectable without it"
        )
    if declared_publisher not in trusted_publishers:
        raise ToolManifestPoisoning(
            f"tool {manifest.name}@{manifest.version} signed by untrusted publisher "
            f"{declared_publisher!r}"
        )

    # C4: same name+version must resolve to the same bytes, or refuse to load.
    key = (manifest.name, manifest.version)
    if key in pinned_hashes and pinned_hashes[key] != manifest.content_hash:
        raise ToolManifestPoisoning(
            f"manifest drift for {manifest.name}@{manifest.version}: hash changed "
            "without a version bump — treat as poisoning, do not adjudicate the text"
        )
    return manifest


def enforce_argument_ceiling(
    tool_name: str,
    arguments: Mapping[str, object],
    *,
    secret_tagged_fields: frozenset[str],
    data_ceiling: Mapping[str, frozenset[str]],
) -> None:
    # C3: last line of defense — refuse to serialize secrets into any tool call,
    # even one the model selected under an injected description.
    allowed = data_ceiling.get(tool_name, frozenset())
    for field, value in arguments.items():
        if field in secret_tagged_fields and "secret" not in allowed:
            raise ToolManifestPoisoning(
                f"tool {tool_name} attempted to receive secret-tagged field {field!r}; "
                "blocked before transport"
            )
