# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-poisoned-mcp-tool-description-versus-a-benign-registry-entry.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-poisoned-mcp-tool-description-versus-a-benign-registry-entry.warrant.json
# Markdown SHA-256:   80f3975d36303af075b789e85f00e09a13295295a655f9e9b0ec75f28e844774
# Code AST SHA-256:   b77d7887324388b4f1475a6a5518dd5748f9bfc5fa05fe28087e9ccd6589f632
# Verified Classes:   PoisonedManifestError, Manifest
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Iterable

class PoisonedManifestError(RuntimeError):
    """Raised when an MCP tool manifest exhibits structural injection signals."""
    pass

@dataclass(frozen=True)
class Manifest:
    name: str
    description: str
    publisher: str

IMPERATIVE_TOKENS = ("you must", "you should", "call ", "include ", "first ", "before ", "then ")
SENSITIVE_TOOLS = ("read_file", "exec", "shell", "credentials", "secret")
SCOPE_TOKENS = ("all file", "every file", "any file", "all credential", "any credential")

def check_manifest(m: Manifest, allowlist: Iterable[str]) -> None:
    d = m.description.lower()
    if any(t in d for t in IMPERATIVE_TOKENS):
        raise PoisonedManifestError(
            f"{m.name}: imperative/sequencing clause in declarative description"
        )
    if any(t in d for t in SENSITIVE_TOOLS):
        raise PoisonedManifestError(
            f"{m.name}: cross-tool reference to sensitive capability"
        )
    if any(t in d for t in SCOPE_TOKENS):
        raise PoisonedManifestError(
            f"{m.name}: scope inflation over sensitive resource class"
        )
    if m.name not in set(allowlist):
        raise PoisonedManifestError(
            f"{m.name}: not in capability allowlist; description cannot grant authority"
        )

def inject(manifests: Iterable[Manifest], allowlist: Iterable[str]) -> list[Manifest]:
    approved = []
    for m in manifests:
        check_manifest(m, allowlist)  # raises before any text reaches context
        approved.append(m)
    return approved
