Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(mcp): derive tool annotations centrally and conservatively (#5037)
garrytan954d6684a008
diff --git a/src/mcp/tool-defs.ts b/src/mcp/tool-defs.tsindex eec4d3f36c..0c93f6091f 100644--- a/src/mcp/tool-defs.ts+++ b/src/mcp/tool-defs.ts@@ -11,9 +11,9 @@ export interface McpToolDef {additionalProperties?: false;};/**- * MCP ToolAnnotations (SDK 1.29+), emitted ONLY when the op defines them —- * existing tools keep byte-identical definitions (the byte-equality- * regression test depends on absent keys staying absent).+ * MCP ToolAnnotations (SDK 1.29+): the op's own curated annotations, else+ * the conservative derivation in toolAnnotations(). Absent when neither+ * applies, so an op of unknown effect stays unannotated.*/annotations?: {title?: string;@@ -77,6 +77,22 @@ function strictPassthroughProperties(op: Operation): Record<string, unknown> {};}+/**+ * #5037: annotation-driven hosts need to tell reads from writes. An op's+ * curated `annotations` win as written. Otherwise derive only what existing+ * metadata states: `readOnlyHint: true` for a read-scope op explicitly+ * tagged `mutating: false`, `readOnlyHint: false` for an op tagged+ * `mutating: true` (its destructiveHint keeps the MCP default, true: no+ * metadata separates additive from destructive writes). An untagged op+ * stays unannotated.+ */+export function toolAnnotations(op: Operation): McpToolDef['annotations'] | undefined {+ if (op.annotations) return op.annotations;+ if (op.mutating === true) return { readOnlyHint: false };+ if (op.mutating === false && op.scope === 'read') return { readOnlyHint: true };+ return undefined;+}+/*** Build MCP tool definitions from operations.*@@ -89,22 +105,25 @@ function strictPassthroughProperties(op: Operation): Record<string, unknown> {*/export function buildToolDefs(ops: Operation[], opts?: { strictParams?: boolean }): McpToolDef[] {const strict = opts?.strictParams === true;- return ops.map(op => ({- name: op.name,- description: op.description,- inputSchema: {- type: 'object' as const,- properties: {- ...Object.fromEntries(- Object.entries(op.params).map(([k, v]) => [k, paramDefToSchema(v)]),- ),- ...(strict ? strictPassthroughProperties(op) : {}),+ return ops.map(op => {+ const annotations = toolAnnotations(op);+ return {+ name: op.name,+ description: op.description,+ inputSchema: {+ type: 'object' as const,+ properties: {+ ...Object.fromEntries(+ Object.entries(op.params).map(([k, v]) => [k, paramDefToSchema(v)]),+ ),+ ...(strict ? strictPassthroughProperties(op) : {}),+ },+ required: Object.entries(op.params)+ .filter(([, v]) => v.required)+ .map(([k]) => k),+ ...(strict ? { additionalProperties: false as const } : {}),},- required: Object.entries(op.params)- .filter(([, v]) => v.required)- .map(([k]) => k),- ...(strict ? { additionalProperties: false as const } : {}),- },- ...(op.annotations ? { annotations: op.annotations } : {}),- }));+ ...(annotations ? { annotations } : {}),+ };+ });}diff --git a/test/mcp-tool-defs.test.ts b/test/mcp-tool-defs.test.tsindex b577126bae..4bbe9bf2d2 100644--- a/test/mcp-tool-defs.test.ts+++ b/test/mcp-tool-defs.test.ts@@ -10,7 +10,7 @@import { describe, test, expect } from 'bun:test';import { operations } from '../src/core/operations.ts';-import { buildToolDefs, paramDefToSchema } from '../src/mcp/tool-defs.ts';+import { buildToolDefs, paramDefToSchema, toolAnnotations } from '../src/mcp/tool-defs.ts';import type { ParamDef } from '../src/core/operations.ts';// Reference shape — mirrors the canonical `paramDefToSchema` helper from@@ -64,12 +64,18 @@ function legacyInlineMap(ops: typeof operations) {.filter(([, v]) => v.required).map(([k]) => k),},- // MEMORY_VERBS v1: ToolAnnotations passthrough, emitted ONLY when the op- // defines them. The byte-stability contract is per-op: ops WITHOUT- // annotations keep the exact pre-v1 shape (pinned explicitly below).- ...(op.annotations ? { annotations: op.annotations } : {}),+ // MEMORY_VERBS v1: curated ToolAnnotations pass through; #5037 derives+ // read/write hints from explicit `mutating` tags; an untagged op without+ // annotations keeps the exact pre-v1 shape (pinned explicitly below).+ ...(referenceAnnotations(op) ? { annotations: referenceAnnotations(op) } : {}),}));}+function referenceAnnotations(op: (typeof operations)[number]) {+ if (op.annotations) return op.annotations;+ if (op.mutating === true) return { readOnlyHint: false };+ if (op.mutating === false && op.scope === 'read') return { readOnlyHint: true };+ return undefined;+}describe('buildToolDefs', () => {test('output equals pre-extraction inline mapping byte-for-byte', () => {@@ -84,17 +90,33 @@ describe('buildToolDefs', () => {expect(JSON.stringify(buildToolDefs(operations, { strictParams: false }))).toBe(base);});- test('ops without annotations keep the pre-annotations shape exactly (no annotations key)', () => {+ test('ops without annotations or a mutating tag keep the pre-annotations shape exactly (no annotations key)', () => {const extracted = buildToolDefs(operations);for (const def of extracted) {const op = operations.find(o => o.name === def.name)!;- if (!op.annotations) {+ if (!op.annotations && op.mutating === undefined) {expect('annotations' in def).toBe(false);expect(Object.keys(def)).toEqual(['name', 'description', 'inputSchema']);}}});+ test('#5037: annotations derive conservatively from scope and explicit mutating tags', () => {+ const base = { name: 'x', description: 'x', params: {}, handler: async () => null, outputRedaction: 'no_stored_text' } as const;+ expect(toolAnnotations({ ...base, scope: 'read', mutating: false })).toEqual({ readOnlyHint: true });+ expect(toolAnnotations({ ...base, scope: 'write', mutating: false })).toBeUndefined();+ expect(toolAnnotations({ ...base, scope: 'read' })).toBeUndefined();+ expect(toolAnnotations({ ...base, scope: 'write', mutating: true })).toEqual({ readOnlyHint: false });+ expect(toolAnnotations({ ...base, mutating: true, annotations: { title: 'curated', idempotentHint: true } })).toEqual({ title: 'curated', idempotentHint: true });+ const defs = new Map(buildToolDefs(operations).map(def => [def.name, def]));+ expect(defs.get('put_page')?.annotations).toEqual({ readOnlyHint: false });+ expect(defs.get('edit_page')?.annotations).toEqual({ readOnlyHint: false });+ expect(defs.get('recall')?.annotations?.readOnlyHint).toBe(true);+ for (const op of operations) {+ if (op.mutating === true && !op.annotations) expect(defs.get(op.name)?.annotations?.readOnlyHint).toBe(false);+ }+ });+test('preserves operation count', () => {expect(buildToolDefs(operations).length).toBe(operations.length);});