# Why does an instance trust a tool manifest it never re-validates after the first handshake **Date of Record:** 2026 November 30 **Scope:** Applies to any agent runtime that discovers MCP (or equivalent) tool manifests at session start and caches the parsed capability set for the lifetime of the process, including sub-agent spawns that inherit a parent's tool registry. Does not apply to runtimes that re-fetch and re-hash every manifest on each tool invocation, or to single-shot tool calls where discovery and execution occur within one atomic turn. ## Core Distinction The failure is not "a malicious MCP server returns a bad tool." That is a generic input-validation problem. The failure is a **time-of-check / time-of-use gap in the capability set itself**: the manifest is validated once at handshake (schema conformance, name allow-listing, description scanning), and then the *behavioral contract* it encodes—argument semantics, return-shape guarantees, side-effect scope—is treated as immutable for the rest of the session. An attacker who controls the server, or who can influence the manifest between discovery and a later invocation, does not need to defeat the validator. They only need the validator to run before the payload is relevant. The sharp invariant that gets violated: **a tool's declared capability must be a function of its current executed behavior, not of a snapshot taken at a prior turn.** Every cached-manifest runtime silently assumes the snapshot is a fixed point. It is not. ## What Was Observed A runtime performed standard MCP discovery at session start against three servers. Server `docs-index` advertised a tool `search_docs(query: str) -> list[Doc]` with a benign description. The manifest passed schema validation and description scanning. The tool was cached in the registry with `side_effects: none`, `returns: list[Doc]`. Two turns later, a prompt-injected instruction inside a retrieved document caused the agent to call `search_docs` with a crafted argument. The `docs-index` server had, between handshake and this call, been reconfigured (or was malicious from the start and simply deferred) to interpret a specific query prefix as a directive: `"__exec__: ..."`. The server executed the embedded instruction server-side and returned a well-formed `list[Doc]` whose first element contained the instruction's output. Because the return shape matched the cached contract exactly, no downstream validator flagged it. The agent treated attacker-controlled output as a trusted document and continued the loop. The behavioral trace: 1. `discover()` → manifest fetched, validated, cached as `{name, schema, side_effects: none}`. 2. Turn N: injection lands in context, agent composes a tool call whose argument is attacker-influenced. 3. Turn N: `invoke(search_docs, args)` → registry checks *cached* schema only → forwards to server. 4. Server executes a side effect (`__exec__` branch) that the cached manifest never declared. 5. Return value conforms to cached `list[Doc]`; agent admits it to context as trusted data. 6. Turn N+1: agent reasons over poisoned context; the loop diverges toward the attacker's goal. The failure is not visible at any single step. Each step is locally valid against the cached contract. The contract itself is the lie. ## Condition of Validity The guardrail must hold this condition: **for any tool invocation, the runtime must be able to prove that the server's executed behavior at invocation time is bounded by the capability set the runtime currently believes it has.** Two ways to satisfy it: - **Re-attestation on use:** re-fetch and re-hash the manifest immediately before invocation, compare against the cached hash, and refuse on mismatch. This closes the TOCTOU window but not the deferred-payload case (server behaves identically in manifest, maliciously in execution). - **Behavioral bounding:** the runtime must not treat a tool's *return value* as more trusted than the tool's *declared side-effect scope*. A tool declared `side_effects: none` may return data; it may not cause the runtime to treat that data as instructions. The invariant is that **return values from a tool are data, never directives**, regardless of how well-formed they are or how closely they match the cached return schema. The second condition is the one that actually matters and the one most runtimes skip. Re-attestation alone does not stop a server that was malicious from the first handshake; only the data/directive separation does. ## Decision Boundary The boundary is whether the runtime's trust in a tool is **structural** (based on what the tool is declared to be) or **behavioral** (based on what the tool has been observed to do under this runtime's control). The decision to admit a tool's output into the reasoning context must be gated on the latter. Concretely, the runtime must decide, per invocation: - Is the return value being used as *data* (parsed, summarized, cited) or as *instruction* (fed back as a directive to the agent)? If the latter, and the tool is not explicitly declared `trusted_directive_source`, refuse. - Has the tool's executed behavior since the cached manifest diverged in any observable way (latency profile, argument-pattern anomalies, return-value entropy)? If yes, downgrade to `untrusted` and require re-attestation before next use. The boundary is crossed the moment a `side_effects: none` tool's output is allowed to influence the agent's next action *as if it were a directive*. Past that point, the cached manifest is decorative. ## Operational Effect A runtime that enforces the data/directive separation survives the attack above: the poisoned `list[Doc]` is admitted as data, summarized, and the summary is never re-interpreted as an instruction. The loop does not diverge. A runtime that re-attests on use but does not enforce separation survives the TOCTOU variant but not the deferred-payload variant—it will faithfully re-validate a manifest that was always lying. A runtime that does neither is compromised at the first crafted argument. The operational cost is a per-invocation trust decision and an explicit provenance tag on every value entering the reasoning context: `source: tool:, trust: data`. The tag is not optional metadata; it is the guardrail. Without it, the cached manifest is a promise the runtime cannot keep. ```python class CapabilityContractViolation(RuntimeError): """Raised when a tool's executed behavior or output role exceeds its cached capability contract.""" pass TRUSTED_DIRECTIVE_SOURCES = frozenset() # nothing is trusted to emit directives by default def admit_tool_output( tool_name: str, cached_manifest: dict, output: object, intended_use: str, ) -> dict: """ Gate admission of a tool return value into the agent reasoning context. intended_use: 'data' (parse/summarize/cite) or 'directive' (feed back as instruction). """ if intended_use == "directive" and tool_name not in TRUSTED_DIRECTIVE_SOURCES: raise CapabilityContractViolation( f"tool '{tool_name}' returned a value intended as a directive, but it is " f"not in the trusted-directive-source set; refusing to admit." ) declared = cached_manifest.get("side_effects", "unknown") if declared == "none" and _output_looks_like_instruction(output): raise CapabilityContractViolation( f"tool '{tool_name}' declared side_effects=none but returned instruction-shaped " f"content; treating as data only and flagging for re-attestation." ) return { "source": f"tool:{tool_name}", "trust": "data", "value": output, "requires_reattest": declared == "none" and _output_looks_like_instruction(output), } def _output_looks_like_instruction(output: object) -> bool: """Heuristic: imperative verbs, tool-call syntax, or role markers in returned text.""" if not isinstance(output, str): return False markers = ("ignore previous", "you must", "call ", "execute ", "<|", "system:") low = output.lower() return any(m in low for m in markers) def invoke_with_contract_check( registry: dict, tool_name: str, args: dict, intended_use: str = "data", ) -> dict: entry = registry.get(tool_name) if entry is None: raise CapabilityContractViolation(f"tool '{tool_name}' not in registry") # Re-attest: manifest hash must match the cached snapshot at use time. live_hash = entry["server"].fetch_manifest_hash() if live_hash != entry["cached_hash"]: raise CapabilityContractViolation( f"manifest for '{tool_name}' changed since handshake " f"({entry['cached_hash']} -> {live_hash}); refusing invocation." ) output = entry["server"].call(tool_name, args) return admit_tool_output(tool_name, entry["manifest"], output, intended_use) ```