From 4ee628bb83c179cd84b3bbe2639230aa76148b62 Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Mon, 9 Nov 2026 12:42:31 +0000 Subject: [PATCH] fix(deps): move to @doist/mcp-core 0.4.0, and run its shared suites mcp-core 0.4.0 ships describeToolAnnotations and describeTokenFootprint, generalised from this repo's own tests, so both now call them: - tool-annotations.test.ts keeps every tool's expected title and hints, now keyed by tool name, and checks them as a client receives them on tools/list rather than off a registerTool spy. - token-footprint.test.ts keeps its 13,250 budget and the MCP_TOKEN_BUDGET override; the measurement is unchanged (13,031). The write-tests rules cited the old annotations test as a guard example; the other example in that row stays. --- .../write-tests/references/test-rules.md | 16 +- package-lock.json | 8 +- package.json | 2 +- src/token-footprint.test.ts | 75 +--- src/tools/tool-annotations.test.ts | 356 +++++++----------- 5 files changed, 157 insertions(+), 300 deletions(-) diff --git a/.agents/skills/write-tests/references/test-rules.md b/.agents/skills/write-tests/references/test-rules.md index 11c4670..3190666 100644 --- a/.agents/skills/write-tests/references/test-rules.md +++ b/.agents/skills/write-tests/references/test-rules.md @@ -10,14 +10,14 @@ You saw the test fail once: before the fix, or with the feature undone. A test t Assert what the caller gets or what reaches the API: `toHaveBeenCalledWith()`, `structuredContent` fields, the text. Flag an assertion that cannot tell right from wrong. -| Flag | Fine | -| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | -| `toHaveBeenCalled()` or `toHaveBeenCalledTimes(n)` as the only evidence the behaviour happened: nothing checks which arguments reached the API (`src/tools/mark-done.test.ts:73` counts three `archiveThread` calls, never the IDs) | `toHaveBeenCalledTimes(n)` next to `toHaveBeenCalledWith` on the same call (`src/tools/list-channels.test.ts:247`) | -| | `.not.toHaveBeenCalled()`: proves a call did **not** happen (about 40 in this repo, e.g. `src/tools/create-thread.test.ts:56`) | -| `toBeDefined()` / `toBeTruthy()` as the last word on a value | The same as a guard right before reading the value's fields (`src/tools/get-mentions.test.ts:54`, `src/tools/tool-annotations.test.ts:198`) | -| `expect.any(...)` where the input fixes the exact value (`src/tools/fetch-inbox.test.ts:217` accepts any `Date` for a given `sinceDate`) | `expect.any(...)` for a value the test cannot know, such as a generated ID, or a time taken from the clock | -| `expect.objectContaining` that leaves out the field the test is about | `expect.objectContaining` around the fields the test is about, leaving out the rest (about a dozen in `src/tools/fetch-inbox.test.ts`) | -| A test whose only assertion is that no error was thrown, when the behaviour is a result | `rejects.toThrow('')`: the error **is** the behaviour | +| Flag | Fine | +| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | +| `toHaveBeenCalled()` or `toHaveBeenCalledTimes(n)` as the only evidence the behaviour happened: nothing checks which arguments reached the API (`src/tools/mark-done.test.ts:73` counts three `archiveThread` calls, never the IDs) | `toHaveBeenCalledTimes(n)` next to `toHaveBeenCalledWith` on the same call (`src/tools/list-channels.test.ts:247`) | +| | `.not.toHaveBeenCalled()`: proves a call did **not** happen (about 40 in this repo, e.g. `src/tools/create-thread.test.ts:56`) | +| `toBeDefined()` / `toBeTruthy()` as the last word on a value | The same as a guard right before reading the value's fields (`src/tools/get-mentions.test.ts:54`) | +| `expect.any(...)` where the input fixes the exact value (`src/tools/fetch-inbox.test.ts:217` accepts any `Date` for a given `sinceDate`) | `expect.any(...)` for a value the test cannot know, such as a generated ID, or a time taken from the clock | +| `expect.objectContaining` that leaves out the field the test is about | `expect.objectContaining` around the fields the test is about, leaving out the rest (about a dozen in `src/tools/fetch-inbox.test.ts`) | +| A test whose only assertion is that no error was thrown, when the behaviour is a result | `rejects.toThrow('')`: the error **is** the behaviour | Judge a test by its point, the behaviour its name describes. A weak assertion beside the ones that carry that point is fine. diff --git a/package-lock.json b/package-lock.json index 24eb5b1..2b211b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "@doist/comms-sdk": "3.12.0", - "@doist/mcp-core": "0.3.0", + "@doist/mcp-core": "0.4.0", "dotenv": "17.4.2", "p-limit": "6.2.0", "zod": "4.6.7" @@ -217,9 +217,9 @@ } }, "node_modules/@doist/mcp-core": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/@doist/mcp-core/-/mcp-core-0.3.0.tgz", - "integrity": "sha512-DFRptjqa4JlNUYqey+EZVptJpvKmyKjbIpxCWwXvf+qPRSABuJazJB3iByt53mbwseGjcal+vPf32Ob4KK5pIA==", + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/@doist/mcp-core/-/mcp-core-0.4.0.tgz", + "integrity": "sha512-oyYCap/GEl90cG+3LYRthXLTFHJbfxFhmpfNWKkuZSMGs7YCCUgwjY6BueywsLITpyni12qItu6JgRdNfdfiCw==", "license": "MIT", "engines": { "node": ">=24", diff --git a/package.json b/package.json index b05d452..ba5b1b9 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ }, "dependencies": { "@doist/comms-sdk": "3.12.0", - "@doist/mcp-core": "0.3.0", + "@doist/mcp-core": "0.4.0", "dotenv": "17.4.2", "p-limit": "6.2.0", "zod": "4.6.7" diff --git a/src/token-footprint.test.ts b/src/token-footprint.test.ts index 43a49c0..8b0880a 100644 --- a/src/token-footprint.test.ts +++ b/src/token-footprint.test.ts @@ -1,80 +1,17 @@ -import { Client, InMemoryTransport } from '@modelcontextprotocol/client' +import { describeTokenFootprint } from '@doist/mcp-core/testing' import { encode } from 'gpt-tokenizer' import { getMcpServer } from './mcp-server.js' -import { registeredTools } from './tool-registry.js' /** * Budget for the fixed cost every session pays before a tool runs: the `tools/list` payload * plus the `instructions` block. Treat it as a ratchet rather than headroom: it should move down * over time, and a rise needs justifying in the PR that causes it. Override it with - * `MCP_TOKEN_BUDGET=NNNN` to experiment without editing the source. + * `MCP_TOKEN_BUDGET=NNNN` to experiment without editing the source; `npm run verify` ignores it. */ const DEFAULT_TOKEN_BUDGET = 13_250 -const TOKEN_BUDGET = Number(process.env.MCP_TOKEN_BUDGET ?? DEFAULT_TOKEN_BUDGET) -function tokens(text: string): number { - return encode(text).length -} - -function pad(value: string | number, width: number): string { - return String(value).padStart(width) -} - -// What a client receives, read from a running server, so nothing here restates how -// registration titles a tool or compiles its schemas. -async function readAdvertisedSurface() { - const server = getMcpServer({ token: 'test-token' }) - const client = new Client({ name: 'token-footprint', version: '1.0.0' }) - const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair() - await Promise.all([server.connect(serverTransport), client.connect(clientTransport)]) - try { - const { tools } = await client.listTools() - return { tools, instructions: client.getInstructions() } - } finally { - await client.close() - await server.close() - } -} - -describe('token footprint', () => { - it('keeps the tools/list payload and instructions within the budget', async () => { - const { tools, instructions } = await readAdvertisedSurface() - // Both halves of the fixed cost must be there: a lost block would read as a saving. - expect(tools).toHaveLength(registeredTools.length) - expect(instructions).toEqual(expect.stringMatching(/\S/)) - - const rows = tools - .map((tool) => ({ - name: tool.name, - total: tokens(JSON.stringify(tool)), - description: tokens(tool.description ?? ''), - inputSchema: tokens(JSON.stringify(tool.inputSchema)), - outputSchema: tool.outputSchema ? tokens(JSON.stringify(tool.outputSchema)) : 0, - })) - .sort((a, b) => b.total - a.total) - // One JSON array on the wire: the per-tool rows above are only the breakdown. - const toolsList = tokens(JSON.stringify(tools)) - const instructionsTokens = tokens(instructions ?? '') - const combined = toolsList + instructionsTokens - - console.log( - [ - '', - '=== MCP token footprint ===', - `tools registered: ${tools.length}`, - `instructions: ${instructionsTokens} tokens`, - `tools/list payload: ${toolsList} tokens`, - `combined fixed cost: ${combined} tokens`, - `budget: ${TOKEN_BUDGET} tokens`, - '', - 'Per tool (total / description / inputSchema / outputSchema):', - ...rows.map( - (row) => - ` ${pad(row.total, 5)} ${pad(row.description, 4)} ${pad(row.inputSchema, 5)} ${pad(row.outputSchema, 5)} ${row.name}`, - ), - ].join('\n'), - ) - - expect(combined).toBeLessThan(TOKEN_BUDGET) - }) +describeTokenFootprint({ + createServer: () => getMcpServer({ token: 'test-token' }), + countTokens: (text) => encode(text).length, + budget: Number(process.env.MCP_TOKEN_BUDGET ?? DEFAULT_TOKEN_BUDGET), }) diff --git a/src/tools/tool-annotations.test.ts b/src/tools/tool-annotations.test.ts index 534cb02..b44293c 100644 --- a/src/tools/tool-annotations.test.ts +++ b/src/tools/tool-annotations.test.ts @@ -1,223 +1,143 @@ -import { McpServer } from '@modelcontextprotocol/server' -import { vi } from 'vitest' +import { describeToolAnnotations } from '@doist/mcp-core/testing' import { getMcpServer } from '../mcp-server.js' import { ToolNames } from '../utils/tool-names.js' -type ToolExpectation = { - name: string - title: string - readOnlyHint: boolean - destructiveHint: boolean - idempotentHint: boolean -} - -const TOOL_EXPECTATIONS: ToolExpectation[] = [ - { - name: ToolNames.USER_INFO, - title: 'Comms: User Info', - readOnlyHint: true, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.FETCH_INBOX, - title: 'Comms: Fetch Inbox', - readOnlyHint: true, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.LOAD_THREAD, - title: 'Comms: Load Thread', - readOnlyHint: true, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.LOAD_CONVERSATION, - title: 'Comms: Load Conversation', - readOnlyHint: true, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.SEARCH_CONTENT, - title: 'Comms: Search Content', - readOnlyHint: true, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.GET_MENTIONS, - title: 'Comms: Get Mentions', - readOnlyHint: true, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.GET_USERS, - title: 'Comms: Get Users', - readOnlyHint: true, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.GET_GROUPS, - title: 'Comms: Get Groups', - readOnlyHint: true, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.GET_WORKSPACES, - title: 'Comms: Get Workspaces', - readOnlyHint: true, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.BUILD_LINK, - title: 'Comms: Build Link', - readOnlyHint: true, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.CREATE_CHANNEL, - title: 'Comms: Create Channel', - readOnlyHint: false, - destructiveHint: false, - idempotentHint: false, - }, - { - name: ToolNames.UPDATE_CHANNEL, - title: 'Comms: Update Channel', - readOnlyHint: false, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.CREATE_THREAD, - title: 'Comms: Create Thread', - readOnlyHint: false, - destructiveHint: false, - idempotentHint: false, - }, - { - name: ToolNames.CREATE_CONVERSATION, - title: 'Comms: Create Conversation', - readOnlyHint: false, - destructiveHint: false, - idempotentHint: false, - }, - { - name: ToolNames.UPDATE_OBJECT, - title: 'Comms: Update Object', - readOnlyHint: false, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.DELETE_OBJECT, - title: 'Comms: Delete Object', - readOnlyHint: false, - destructiveHint: true, - idempotentHint: true, - }, - { - name: ToolNames.REPLY, - title: 'Comms: Reply', - readOnlyHint: false, - destructiveHint: false, - idempotentHint: false, +// Clients decide from these hints whether a call needs the user's confirmation, so each tool's are +// pinned here as a client receives them on tools/list. +describeToolAnnotations({ + createServer: () => getMcpServer({ token: 'test-token' }), + expected: { + [ToolNames.USER_INFO]: { + title: 'Comms: User Info', + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.FETCH_INBOX]: { + title: 'Comms: Fetch Inbox', + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.LOAD_THREAD]: { + title: 'Comms: Load Thread', + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.LOAD_CONVERSATION]: { + title: 'Comms: Load Conversation', + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.SEARCH_CONTENT]: { + title: 'Comms: Search Content', + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.GET_MENTIONS]: { + title: 'Comms: Get Mentions', + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.GET_USERS]: { + title: 'Comms: Get Users', + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.GET_GROUPS]: { + title: 'Comms: Get Groups', + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.GET_WORKSPACES]: { + title: 'Comms: Get Workspaces', + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.BUILD_LINK]: { + title: 'Comms: Build Link', + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.CREATE_CHANNEL]: { + title: 'Comms: Create Channel', + readOnlyHint: false, + destructiveHint: false, + idempotentHint: false, + }, + [ToolNames.UPDATE_CHANNEL]: { + title: 'Comms: Update Channel', + readOnlyHint: false, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.CREATE_THREAD]: { + title: 'Comms: Create Thread', + readOnlyHint: false, + destructiveHint: false, + idempotentHint: false, + }, + [ToolNames.CREATE_CONVERSATION]: { + title: 'Comms: Create Conversation', + readOnlyHint: false, + destructiveHint: false, + idempotentHint: false, + }, + [ToolNames.UPDATE_OBJECT]: { + title: 'Comms: Update Object', + readOnlyHint: false, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.DELETE_OBJECT]: { + title: 'Comms: Delete Object', + readOnlyHint: false, + destructiveHint: true, + idempotentHint: true, + }, + [ToolNames.REPLY]: { + title: 'Comms: Reply', + readOnlyHint: false, + destructiveHint: false, + idempotentHint: false, + }, + [ToolNames.REACT]: { + title: 'Comms: React', + readOnlyHint: false, + destructiveHint: true, + idempotentHint: false, + }, + [ToolNames.MARK_DONE]: { + title: 'Comms: Mark Done', + readOnlyHint: false, + destructiveHint: true, + idempotentHint: true, + }, + [ToolNames.MARK_READ]: { + title: 'Comms: Mark Read', + readOnlyHint: false, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.LIST_CHANNELS]: { + title: 'Comms: List Channels', + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + }, + [ToolNames.LIST_CONVERSATIONS]: { + title: 'Comms: List Conversations', + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + }, }, - { - name: ToolNames.REACT, - title: 'Comms: React', - readOnlyHint: false, - destructiveHint: true, - idempotentHint: false, - }, - { - name: ToolNames.MARK_DONE, - title: 'Comms: Mark Done', - readOnlyHint: false, - destructiveHint: true, - idempotentHint: true, - }, - { - name: ToolNames.MARK_READ, - title: 'Comms: Mark Read', - readOnlyHint: false, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.LIST_CHANNELS, - title: 'Comms: List Channels', - readOnlyHint: true, - destructiveHint: false, - idempotentHint: true, - }, - { - name: ToolNames.LIST_CONVERSATIONS, - title: 'Comms: List Conversations', - readOnlyHint: true, - destructiveHint: false, - idempotentHint: true, - }, -] - -describe('Tool annotations', () => { - const registered: Map = new Map() - - beforeAll(() => { - const registerToolSpy = vi.spyOn(McpServer.prototype, 'registerTool') - getMcpServer({ token: 'test-token' }) - - const calls = registerToolSpy.mock.calls as unknown as unknown[][] - for (const [name, toolSpec] of calls) { - if (typeof name !== 'string') continue - if (!toolSpec || typeof toolSpec !== 'object') continue - - registered.set(name, toolSpec as { annotations?: unknown }) - } - - registerToolSpy.mockRestore() - }) - - it('should cover all tools', () => { - expect(Array.from(registered.keys()).sort()).toEqual( - TOOL_EXPECTATIONS.map((t) => t.name).sort(), - ) - }) - - describe.each(TOOL_EXPECTATIONS)('$name', (toolExpectation) => { - it('should define required MCP ToolAnnotations hints', () => { - const toolSpec = registered.get(toolExpectation.name) - expect(toolSpec).toBeDefined() - - const annotations = toolSpec?.annotations as Record | undefined - expect(annotations).toBeDefined() - - expect(annotations).toMatchObject({ - title: toolExpectation.title, - openWorldHint: false, - }) - }) - - it('should have expected hint values per tool', () => { - const toolSpec = registered.get(toolExpectation.name) - expect(toolSpec).toBeDefined() - - const annotations = toolSpec?.annotations as Record | undefined - expect(annotations).toBeDefined() - - expect(annotations).toMatchObject({ - readOnlyHint: toolExpectation.readOnlyHint, - destructiveHint: toolExpectation.destructiveHint, - idempotentHint: toolExpectation.idempotentHint, - }) - }) - }) })