Skip to content

feat(mcp): pin MCP tool-set digests and quarantine drifted servers (SK2) - #171

Closed
devin-ai-integration[bot] wants to merge 1 commit into
devin/1790292004-audit-chainfrom
devin/1790293341-mcp-toolset-pin
Closed

devin-ai-integration[bot] wants to merge 1 commit into
devin/1790292004-audit-chainfrom
devin/1790293341-mcp-toolset-pin

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

What changes for users

An MCP server declaration accepts an optional pin:

"linear": Http(
    url: "https://mcp.linear.app/mcp",
    bearer: Env("LINEAR_TOKEN"),
    pin: "5a1f…e9c0",   // 64 lowercase hex digits
),

QQ digests every server's tool listing (SHA-256 over each tool's namespaced name, description, compact sorted-key input schema, and hints, in name order — listing order is irrelevant). A pinned server whose live listing digests differently is quarantined: its tools leave the catalog, every call to it — including a tool whose own schema did not change — is refused with McpCallFailure::Quarantined, and the readiness message reads

quarantined MCP servers: linear (tool set digests to 9c2e… but the configured pin is 5a1f…)

A list_changed that drifts a pinned server quarantines it before the next call executes even if no catalog is fetched in between; a server that reverts to the pinned listing leaves quarantine on its next notification. A malformed pin (wrong length, uppercase, non-hex) fails config load: mcp server "x" pin must be the 64 lowercase hex digits of a tool-set digest. Unpinned servers behave exactly as before.

Why / design

Slice SK2 (A2) of ADR-0042: Axiom's tool-set digest pinning and drift quarantine, as library code in qq-mcp rather than an axiom-mcp dependency or a service.

  • McpToolSetDigest([u8; 32]) in qq-mcp with Display/FromStr as 64 lowercase hex and a versioned domain separator (qq-mcp-tool-set-v1\0). The schema is hashed as ToolSpec::input_schema().get() — the compact sorted-key text serde_json::Value already produces, which is what ToolSpec equality relies on too — so no extra canonicalization pass exists.
  • ServerState caches Listing { tools, digest } instead of bare tools; the digest is computed once per fetch. ServerHandle::listing() compares against settings.pin on every use (a 32-byte compare) and returns ListingFault::Quarantined { expected, actual } without dropping the cache, so a quarantined server is reported, not refetched, per catalog/call.
  • ServerHandle::execute runs listing() for pinned servers before connecting for the call. This is what makes list_changed fail closed: the notification only marks the cache dirty; the next call refetches and re-verifies. Unpinned servers skip the check, so their call path is unchanged.
  • McpCatalog gains servers: Vec<McpServerListing> (digest of every server that answered, quarantined included — that is the value an operator copies into pin) and quarantined: Vec<McpQuarantine>. Quarantined tools never reach tools. unavailable semantics are unchanged.
  • qq-config validates the pin text at load (valid_mcp_tool_set_pin) so a typo is a config error, not a silent quarantine; the composition root (src/mcp.rs) parses it into McpServerSettings::pin and maps Quarantined to HostCallError::Refused (the server is reachable; the kernel declines to use it).

Deliberately not ported: Axiom's separate ServerIdentityChanged reason — qq names servers in configuration and the name is inside the digest, so a rename is a digest change; BLAKE3 (the workspace already carries sha2); per-tool pins; a CLI that prints a server's digest (the quarantine message carries the actual digest; the guide documents pinning via a placeholder pin — a qq mcp digest <server> command is a possible follow-up).

Stacked on #165 (SK1) so the security-kernel ledger edits do not conflict; no code dependency on SK1.

Linear: ENG-
Slice: docs/plans/security-kernel.md#sk2

How verified

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --all-features -- -D warnings
  • cargo test --workspace — all green; qq-mcp 18 passed (13 existing + 5 new), qq-config 98 passed, qq-core 727 passed
  • cargo test -p qq-provider --no-default-features --features test-support — not run; qq-provider unchanged
  • Manual check: none beyond the in-process fixture tests below (no live MCP servers used)

New tests, crates/qq-mcp/src/tests.rs:

  • tool_set_digest_ignores_listing_order_and_tracks_every_field — same digest for reordered listings and reordered schema keys; different digest for a renamed, re-described, reshaped, or re-hinted tool, pairwise distinct; hex round-trip, uppercase/short text rejected.
  • unpinned_servers_list_tools_and_expose_their_digest
  • a_matching_pin_leaves_the_server_usable — pin taken from one fixture, applied to a fixture listing the same tools in the opposite order.
  • a_drifted_pin_quarantines_the_catalog_and_every_call — catalog quarantined/servers/tools shape, unchanged tool refused too, listing cached (one tools/list), sibling server untouched, quarantine clears when the server reverts and notifies.
  • a_list_changed_drift_quarantines_before_the_next_call — description change + notification with no catalog fetch in between; the next call is Quarantined.

Extended, crates/qq-config/src/tests.rs: mcp_servers_parse_layer_by_name_and_apply_defaults (pin surfaced, unpinned is None), rejects_invalid_mcp_declarations (short / uppercase / non-hex pins rejected, well-formed pin accepted).

Impact

  • Performance: one SHA-256 over the listing per tools/list fetch (already off the call path — fetches happen on first use and after list_changed). Pinned calls add a mutex lock and a 32-byte compare before the existing connect step; unpinned calls are unchanged.
  • Compatibility: PROTOCOL / DESCRIPTOR / store schema unchanged. McpCatalog gains two public fields (servers, quarantined) and McpCallFailure one variant (Quarantined); McpServerSettings gains pin (defaults to None via new). Existing configs without pin are unaffected.
  • Docs: docs/design/tools.md § MCP (digest + quarantine) and § MCP Configuration (pin); docs/guide/mcp.md (pin key, "Pinning a server's tools"); docs/plans/security-kernel.md and docs/plans/progress/security-kernel.md (SK2 → In review, implementation entry).

Link to Devin session: https://app.devin.ai/sessions/4a83e789a14d4b4190950fb8678d668e
Open in Devin Desktop: https://app.devin.ai/desktop/session/4a83e789a14d4b4190950fb8678d668e?variant=devin
Requested by: @integrate-your-mind


Devin Review

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 2 potential issues.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment thread src/mcp.rs
Comment on lines +382 to +384
settings.pin = server
.pin()
.map(|pin| pin.parse().expect("the validated pin is a tool-set digest"));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Pinned and unpinned plans share an identity

Adding a matching pin changes server behavior without changing the plan digest. describe_server omits the pin, while both configurations produce identical catalogs until drift occurs.

Learn more

The plan descriptor is the durable identity of a compiled plan, and its digest represents behavior-affecting settings. A matching pin changes how future listing changes affect tool calls, but the MCP server descriptor omits it. The plan cache creates a new live generation when the registry changes, yet the persisted plan digest remains identical if both catalogs are currently healthy. This makes pinned and unpinned executions indistinguishable by plan digest.

Example: An unpinned linear server lists tools with digest abc…; adding pin: "abc…" keeps its catalog and readiness unchanged. Both plans get the same digest, although the pinned plan refuses calls after a later listing change.

Recommended fix: Include the configured pin in McpServerDescriptor and populate it in describe_server. Follow the descriptor version and fixture requirements for changing its canonical encoding.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment thread crates/qq-mcp/src/lib.rs
Comment on lines +578 to +582
let tools = self.namespaced_tools(tools);
let listing = Arc::new(Listing {
digest: McpToolSetDigest::of_tools(&tools),
tools,
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟥 Skipped tools escape the server pin

When namespaced_tools skips a declared tool, of_tools leaves it out of the pin digest. McpManager::call still dispatches its nonempty name, so a matching pin does not prevent an explicitly granted call to a changed, skipped tool.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@lg2m

lg2m commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this contribution — the tool-set pinning idea is one we want in QQ, and the reviewed design has now landed as a single integration PR: #194 (feat/eng-939-mcp-pinning-stack, tracking ENG-939). Your authorship is preserved via Co-Authored-By on the commit.

#194 ports the MCP pinning delta from this stack and repairs the issues found in review (details and reproducing probes in the PR description and docs/adr/0046-mcp-tool-set-pinning.md):

This PR will be closed as superseded once #194 merges.

lg2m added a commit that referenced this pull request Sep 26, 2026
… dispatch; add qq mcp inspect (#194)

* feat(mcp)!: pin MCP tool sets in config and plan identity; enforce at dispatch; add qq mcp inspect

An MCP server's tool descriptors are untrusted text the model reads as
instructions, and the server may change them at any time. A server
declaration may now carry `pin`: the SHA-256 tool-set digest (64 hex
digits, validated at load) of the listing an operator reviewed. A pinned
server whose listing digests differently is quarantined: it contributes no
tools, the readiness message names both digests, and every call to it is
refused (`McpCallFailure::Quarantined` -> `HostCallError::Refused`).

This takes over the contributed stack #163 / #165 / #171 and ships only
the MCP pinning delta, repaired against QQ's own contracts (ADR-0046):

- Dispatch-time enforcement. A pinned call resolves the cached listing
  before waiting for a permit, re-checks the listing generation and the
  `list_changed` flag after it, requires the tool to be in that listing,
  and sends exactly one `tools/call` whose enqueue poll runs under a
  synchronous dispatch gate shared with `on_tool_list_changed`,
  `bump_generation`, and `shutdown`. Previously a queued call that passed
  the pin check executed after the server was quarantined.
- Durable identity. `McpServerDescriptor.pin` is recorded and
  `DESCRIPTOR_VERSION` moves 9 -> 10; pinned and unpinned plans no longer
  share a digest.
- Injective digest. Domain `qq-mcp-tool-set-v2` with length-prefixed
  fields replaces the `\0`-separated v1 encoding.
- Whole listings only. Malformed or duplicate tool names fail the listing
  instead of being skipped before hashing; fetches are bounded to 32
  pages, 512 tools, and 1 MiB; a `list_changed` during a fetch discards it.
- Inspection. `McpManager::inspect` and `qq mcp inspect NAME` print the
  live digest and every descriptor as JSON without calling a tool, so the
  digest is reviewed before it is pinned rather than copied out of a
  quarantine error.

The per-event audit hash chain from #165 is not ported: it has no
consumer, checkpoint, or retention contract yet, and its verifier
accepted a cursor rewrite. Approval/argument binding is deferred.

Breaking: `DESCRIPTOR_VERSION` 9 -> 10 invalidates cached plan digests
(recompiled on next load; no store migration). No protocol change.

Verification: fmt, clippy (-D warnings), workspace build and tests, the
minimal qq-provider profile, and `plan_compile` bench within noise of the
baseline (ledger `docs/plans/progress/mcp-pinning.md`).

Closes ENG-939. Supersedes #163, #165, #171.

Co-Authored-By: romy <[email protected]>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(mcp): count listing descriptor bytes without buffering an encoded copy

Codex review on #194: `serde_json::to_vec(tool)` allocated the whole
encoded descriptor before the 1 MiB check, doubling the resident bytes for
a page `rmcp` had already decoded. Measure through a counting `ByteBudget`
writer instead; the write that crosses the budget is rejected and nothing
is allocated. The check now bounds only what it claims to bound; the
transport read limit remains an `rmcp` concern (SSE events are capped at
16 MiB there, stdio lines are unbounded) and is noted in the ledger.

Co-Authored-By: romy <[email protected]>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: romy <[email protected]>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@lg2m lg2m closed this Sep 26, 2026
@lg2m
lg2m deleted the devin/1790293341-mcp-toolset-pin branch September 26, 2026 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants