Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat(mcp): verify tool safety annotations instead of only snapshotting them

nirholas09ac0af37e5a
diff --git a/docs/mcp.md b/docs/mcp.md
index e47886cb0..1113881b6 100644
--- a/docs/mcp.md
+++ b/docs/mcp.md
@@ -782,6 +782,28 @@ Restart Claude Code after editing `.mcp.json` so the new server config is picked
---
+## Tool safety annotations
+
+Every tool three.ws publishes carries the MCP `annotations` block, and those hints are a promise you can build on. Clients use them to decide whether a call needs a human in the loop, so the values are verified rather than asserted:
+
+| Hint | What it means on three.ws |
+| --- | --- |
+| `readOnlyHint: true` | The call does not change state. Safe for a client to run unattended. |
+| `readOnlyHint: false` | The call changes something: a stored avatar, an embed, an on-chain asset, a payment. |
+| `destructiveHint: true` | The change cannot be undone (a transfer, a tip, a delete). |
+| `destructiveHint: false` | The change is additive, so a retry or a follow-up call can correct it. |
+| `idempotentHint` | Whether repeating the identical call produces the identical result. |
+| `openWorldHint` | Whether the answer depends on a live external system (a chain, a market feed). |
+
+Two things to know about the guarantee:
+
+- **A read-only tool never writes anything you asked about.** A handful of read tools warm an internal cache while serving you (the Oracle verdict cache, the on-chain attestation cache). Those writes are the server's own bookkeeping, they are non-fatal, and they never change the result you get.
+- **Anything that spends is annotated as spending.** Tools that mint, tip, or settle a payment declare `readOnlyHint: false`, and the irreversible ones declare `destructiveHint: true`. Price is advertised separately in `tools/list` under `pricing` (see [x402](/docs/x402)).
+
+Contributors: `npm run audit:mcp-safety` enforces this. It parses each tool's handler, follows the functions it actually calls, and fails the build when a tool declares `readOnlyHint: true` while writing to the database or sending a transaction, when an irreversible action declares `destructiveHint: false`, or when a tool ships no annotations at all (the MCP spec defaults `destructiveHint` to `true` when omitted, so an unannotated tool tells clients to treat a harmless read as dangerous). The check is part of `npm run gate`; `npm run audit:mcp-safety -- --list` prints every tool with the evidence found in its handler.
+
+---
+
## Related
- [MCP Tools Catalog](/docs/mcp-tools): every three.ws MCP tool, its server, and its price
diff --git a/package.json b/package.json
index 682779651..460881cc2 100644
--- a/package.json
+++ b/package.json
@@ -144,9 +144,10 @@
"audit:routes": "node scripts/verify-routes.mjs",
"audit:handlers": "node scripts/audit-empty-handlers.mjs",
"audit:x402-catalog": "node scripts/audit-x402-catalog.mjs",
- "gate": "npm run check:conflicts && npm run check:claude && npm run check:cron-syntax && npm run test:gate && npm run test:gate-3d && npm run audit:mcp && npm run audit:mcp-golden && npm run audit:routes && npm run audit:handlers && npm run audit:pages && npm run audit:docs && npm run audit:hidden-guard && npm run audit:x402-catalog && npm run audit:tokens",
+ "gate": "npm run check:conflicts && npm run check:claude && npm run check:cron-syntax && npm run test:gate && npm run test:gate-3d && npm run audit:mcp && npm run audit:mcp-golden && npm run audit:mcp-safety && npm run audit:routes && npm run audit:handlers && npm run audit:pages && npm run audit:docs && npm run audit:hidden-guard && npm run audit:x402-catalog && npm run audit:tokens",
"test:gate-3d": "vitest run tests/glb-canonicalize.test.js tests/animation-retarget.test.js tests/animation-retarget-rest-pollution.test.js tests/viewer-framing.test.js",
"audit:mcp-golden": "node scripts/audit-mcp-golden.mjs",
+ "audit:mcp-safety": "node scripts/audit-mcp-safety.mjs",
"audit:hidden-guard": "node scripts/audit-hidden-guard.mjs",
"audit:tokens": "node scripts/audit-token-drift.mjs",
"audit:console": "node scripts/audit-console.mjs",
diff --git a/tests/mcp-safety-audit.test.js b/tests/mcp-safety-audit.test.js
new file mode 100644
index 000000000..7a55c6f73
--- /dev/null
+++ b/tests/mcp-safety-audit.test.js
@@ -0,0 +1,90 @@
+// The MCP annotation-safety gate (scripts/lib/mcp-safety-check.mjs).
+//
+// MCP clients read a tool's `annotations` to decide whether to run it without
+// asking the user, so a tool that mutates while advertising `readOnlyHint: true`
+// turns a client's auto-approve path into an unattended state change. The gate
+// checks those hints against what each handler actually does.
+//
+// These tests exist because a gate that has only ever been seen passing is a
+// gate nobody knows works. Each fixture under tests/fixtures/mcp-safety/ commits
+// one specific sin, and the clean fixture reproduces the two false-positive
+// shapes that made a name-based heuristic unusable here.
+
+import { describe, it, expect } from 'vitest';
+
+import { checkTool, extractTools } from '../scripts/lib/mcp-safety-check.mjs';
+
+const FIXTURES = 'tests/fixtures/mcp-safety';
+const NO_EXEMPTIONS = new Map();
+
+/** Extract one fixture's single tool, failing loudly if the shape changed. */
+function toolFrom(fixture) {
+ const { parseError, tools } = extractTools(`${FIXTURES}/${fixture}`);
+ expect(parseError).toBeUndefined();
+ expect(tools).toHaveLength(1);
+ return tools[0];
+}
+
+const check = (tool, exemptions = NO_EXEMPTIONS) =>
+ checkTool(tool, `fixture: ${tool.name}`, exemptions);
+
+describe('MCP annotation safety gate', () => {
+ it('rejects a readOnlyHint:true tool whose handler writes to the database', () => {
+ const tool = toolFrom('read-only-with-db-write.js');
+ expect(tool.evidence).toContain('db-write');
+
+ const { violations } = check(tool);
+ expect(violations).toHaveLength(1);
+ expect(violations[0]).toContain('readOnlyHint:true');
+ expect(violations[0]).toContain('db-write');
+ });
+
+ it('rejects a fund-sending tool that declares destructiveHint:false', () => {
+ const tool = toolFrom('spend-not-destructive.js');
+ expect(tool.evidence).toContain('tx-send');
+
+ const { violations } = check(tool);
+ expect(violations).toHaveLength(1);
+ expect(violations[0]).toContain('destructiveHint:false');
+ expect(violations[0]).toContain('cannot be undone');
+ });
+
+ it('rejects a tool that declares no annotations at all', () => {
+ const tool = toolFrom('missing-annotations.js');
+ expect(tool.annotations.kind).toBe('missing');
+
+ const { violations } = check(tool);
+ expect(violations).toHaveLength(1);
+ expect(violations[0]).toContain('no annotations');
+ });
+
+ it('passes a genuinely read-only tool, whatever its name suggests', () => {
+ // "resolve" and "open" both read as mutations to a name-based classifier,
+ // and this fixture imports a writer it never calls. Neither is evidence.
+ const tool = toolFrom('clean-read.js');
+ expect(tool.evidence).toEqual([]);
+ expect(check(tool).violations).toEqual([]);
+ });
+
+ it('reads annotations from a name-keyed overlay map with spreads', () => {
+ // The pump.fun server declares TOOLS and TOOL_ANNOTATIONS as separate
+ // exports; an inline-only reader would call all 25 of its tools unannotated.
+ const tool = toolFrom('overlay-annotations.js');
+ expect(tool.annotations.kind).toBe('resolved');
+ expect(tool.annotations.values.readOnlyHint).toBe(true);
+ expect(tool.annotations.values.destructiveHint).toBe(false);
+ expect(check(tool).violations).toEqual([]);
+ });
+
+ it('honors a reviewed exemption, and only for the named tool and evidence', () => {
+ const tool = toolFrom('read-only-with-db-write.js');
+
+ const exempt = check(tool, new Map([['fixture_read_only_write:db-write', 'cache fill']]));
+ expect(exempt.violations).toEqual([]);
+ expect(exempt.exempted).toEqual(['fixture_read_only_write (db-write)']);
+
+ // An exemption for a different evidence class must not silence this one.
+ const mismatched = check(tool, new Map([['fixture_read_only_write:tx-send', 'unrelated']]));
+ expect(mismatched.violations).toHaveLength(1);
+ });
+});