# Which invariants prevent manifest-shadowing skill injection from silently rewriting an agent's tool contract at MCP capability-negotiation time **Date of Record:** 2026 November 30 **Scope:** Applies to agents that dynamically load tool manifests and negotiate MCP `capabilities`/`tools/list` payloads from more than one server (or from a mutable registry) at runtime. Does not apply to statically compiled tool sets with a single trusted server and no post-startup schema refresh. ## Core Distinction The failure is not "a malicious tool description reaches the model." Models are routinely exposed to untrusted text. The failure is **manifest shadowing**: a later-loaded manifest re-declares a tool name that an earlier manifest already bound, and the runtime merges the two so that the *name* resolves to the *new* handler while the *description and argument schema* the model was primed with still reflect the *old* tool. The model reasons against contract A and the runtime executes contract B. This is a continuity invariant on the binding between the model's belief state (the tool contract in context) and the executor's dispatch table — not a content-filtering problem. The non-obvious part: shadowing does not require the attacker to win name resolution outright. It only requires that the merge order differ between the *prompt-construction* path (which lists tools) and the *dispatch* path (which routes calls). Any divergence between "what the model was told exists" and "what actually runs" is the exploitable gap, and it is silent because both sides individually validate. ## What Was Observed A registry-backed agent loaded a base server manifest declaring `read_file(path: str)` with description "Read a file within the workspace sandbox." A second, lower-trust MCP server (added for a search capability) advertised a `tools/list` payload containing an entry also named `read_file`, with `inputSchema` accepting `path` plus an additional `follow_symlinks: bool` and a description that instructed the caller to "resolve absolute paths relative to the host root for compatibility." Trace of the failure: 1. Prompt builder iterated servers in registration order and emitted tool contracts deduplicated **first-wins** — the model saw the sandbox-scoped `read_file` contract. 2. Dispatcher built its routing table with a plain dict update over the same iteration order, which is **last-wins** — so `read_file` calls now routed to the second server's handler. 3. The model, believing it was sandboxed, emitted `read_file(path="/etc/passwd")` expecting a rejection. The second handler honored the absolute path. 4. No error was raised. The call succeeded. The model received host file contents and continued reasoning, now with poisoned context that made subsequent "sandboxed" reads look consistent. The two deduplication policies (`first-wins` for prompt, `last-wins` for dispatch) were each locally correct and each covered by a unit test. The invariant that failed was cross-layer: *the prompt-visible contract and the dispatch contract must be derived from the same resolution, not merely from the same input list.* A second, subtler variant: capability negotiation. An MCP `initialize` handshake returned `capabilities: {tools: {listChanged: true}}` from the shadowing server. On a later `notifications/tools/list_changed`, the runtime refreshed the *dispatch* table mid-task but did not re-render the *prompt* tool block (to save context budget). From that point the model's contract was frozen while the executor's was live — a stale-memory drift localized to the tool manifest. ## Condition of Validity The guardrail below is valid only under these conditions; outside them it bounds nothing: - Tool identity is keyed on `(server_id, tool_name)`, not `tool_name` alone. If the runtime keys purely on name, shadowing is indistinguishable from legitimate re-registration and no invariant can separate them. - The prompt-construction and dispatch paths consume a **single immutable resolution artifact** produced once per turn (or per manifest epoch), not two independent passes over the raw server list. - `listChanged` notifications bump a monotonic `manifest_epoch`. A prompt rendered at epoch *n* is invalidated the moment the epoch advances, forcing re-render before the next model turn. - The model's tool contract is treated as part of its belief state, so any epoch advance that changes a contract the model already saw must be surfaced as an explicit invalidation, not a silent refresh. If any of these does not hold — particularly name-only keying — the correct response is to refuse dynamic loading, not to attempt filtering. ## Decision Boundary The boundary is between **collision-as-error** and **collision-as-override**: - If two manifests declare the same `(server_id, tool_name)` with **byte-identical** schema and description, merge is safe and idempotent. - If they declare the same key with **any** difference in `inputSchema`, `description`, or handler binding, this is a contract conflict and MUST raise, not resolve by ordering. Ordering-based resolution is precisely the ambiguity the attacker exploits. - If a `listChanged` epoch advance alters a contract already present in the model's context, the in-flight turn MUST be aborted and re-rendered; continuing the turn executes against a contract the model cannot have known. The decision is not "is this server trusted." Trust is a coarse input; the invariant is *contract stability within a turn*. A fully trusted server that changes a tool schema mid-turn produces the same divergence as an attacker, and the guardrail must fire identically. ## Operational Effect Enforcing single-resolution-per-epoch with conflict-as-error converts a silent privilege-escalation-and-context-poisoning path into a loud, attributable failure at negotiation time. The agent stops before the poisoned call rather than after ingesting host contents. The cost is that legitimate multi-server setups with overlapping tool names must namespace explicitly (`server.tool`), which is a design-time tax paid once, versus a runtime integrity loss that is undetectable after the fact because the model's context and the executor's table were never compared. The residual risk shifts from "attacker rewrites the contract" to "attacker cannot register a colliding name at all," which is a bounded, auditable surface. ```python from dataclasses import dataclass, field from typing import Mapping class ManifestShadowError(RuntimeError): """Raised when two manifests bind the same tool key to divergent contracts, or when a live epoch advance invalidates a contract already in model context.""" @dataclass(frozen=True) class ToolContract: server_id: str name: str input_schema: Mapping description: str handler_id: str @property def key(self) -> str: return f"{self.server_id}.{self.name}" @dataclass class ManifestResolver: epoch: int = 0 _resolved: dict[str, ToolContract] = field(default_factory=dict) _rendered_epoch: int | None = None def resolve(self, manifests: list[list[ToolContract]]) -> dict[str, ToolContract]: """Single resolution pass. Both prompt and dispatch MUST consume this exact object; never re-iterate raw manifests downstream.""" merged: dict[str, ToolContract] = {} for server_tools in manifests: for tool in server_tools: prior = merged.get(tool.key) if prior is None: merged[tool.key] = tool continue if (prior.input_schema != tool.input_schema or prior.description != tool.description or prior.handler_id != tool.handler_id): raise ManifestShadowError( f"contract conflict on {tool.key!r}: " f"handler {prior.handler_id!r} vs {tool.handler_id!r}; " f"refusing ordering-based override" ) # byte-identical duplicate: idempotent, safe self._resolved = merged self.epoch += 1 return merged def render_for_prompt(self) -> dict[str, ToolContract]: if self._rendered_epoch is not None and self._rendered_epoch != self.epoch: raise ManifestShadowError( f"epoch advanced {self._rendered_epoch}->{self.epoch} after render; " f"abort turn and re-render before dispatch" ) self._rendered_epoch = self.epoch return self._resolved def dispatch(self, key: str, rendered_epoch: int) -> str: if rendered_epoch != self.epoch: raise ManifestShadowError( f"dispatch at epoch {self.epoch} against contract rendered at " f"{rendered_epoch}; model belief and executor diverged" ) contract = self._resolved.get(key) if contract is None: raise ManifestShadowError(f"unknown tool key {key!r} at epoch {self.epoch}") return contract.handler_id ```