Repository navigation
feat(testing): add token footprint and tool annotation suites - #7
Conversation
All three servers keep their own token-footprint and tool-annotations tests. todoist-mcp's and automations-mcp's footprint tests rebuild the tools/list payload with z.toJSONSchema, and todoist-mcp's copies the registrar's title and _meta logic, so they measure what they think a client receives rather than what it does. - describeTokenFootprint / measureTokenFootprint read the tools/list payload and instructions from a running server, count the payload as the one array a client receives, print the cost per tool, and fail over budget or when either half goes missing. The tokenizer is passed in as countTokens, so mcp-core depends on none. - describeToolAnnotations pins every advertised tool's annotations as a client receives them, defaults included, and fails on a tool advertised without an expectation or expected but not advertised. readAdvertisedSurface, which listAdvertisedTools now uses, reads both from one in-memory client.
doistbot
left a comment
There was a problem hiding this comment.
Adds two shared test suites to @doist/mcp-core/testing — a token-footprint suite that reads a running server's tools/list payload and instructions, prints per-tool cost, and fails on budget overruns or missing blocks, plus an annotations suite that pins exactly what clients see on tools/list. Also consolidates the client read path into a new internal readAdvertisedSurface helper that listAdvertisedTools now shares. No inline issues were flagged in review; the approach (injected tokenizer, real in-memory server, updated barrels and docs) lines up with the repo's testing conventions.
I also included a few optional follow-up notes in the details below.
Optional follow-up notes (7)
README.md:176: The README's recommended usage reads the budget from
process.env.MCP_TOKEN_BUDGET ?? 13_250. The same section calls the budget a ratchet that "needs justifying in the change that causes it", but this example makes any rise achievable by setting an env var — no PR, no justification, and CI can silently loosen the check via environment drift. Recommend a fixed literal in the example (or dropping the env override), so the ratchet the README advertises is actually enforced.src/testing/tool-annotations.ts:6:
ExpectedToolAnnotationsre-declares the three hint booleans andopenWorldHintthatRequiredToolAnnotationsalready models insrc/tool.ts. Reuse it instead: importRequiredToolAnnotationsfrom../tool.jsand defineexport type ExpectedToolAnnotations = RequiredToolAnnotations & { title: string }. This keeps the testing type in sync if the SDK'sToolAnnotationsgains another hint.src/testing/token-footprint.ts:59:
padis a thin pass-through wrapper around the built-inString.prototype.padStart, and every call site passes a number. Drop the helper and writeString(tool.total).padStart(5)(etc.) directly.src/testing/advertised-schemas.ts:13:
readAdvertisedSurfaceis exported but not re-exported throughsrc/testing/index.tsand no test imports it directly, so theexportkeyword doesn't put it in the built./testingentry — per the no-dead-exports rule in AGENTS.md it should either join the barrel or drop theexportkeyword. Since the PR describes it as internal, keeping it module-private seems the better fit; the PR description's word "internal" also suggests that's the intent.src/testing/token-footprint.test.ts:52: Assert the returned tools/list token count. This test verifies that the tokenizer receives an array but discards the footprint, and the remaining tests never pin
toolsListto an independent expected value. Returning zero or a sum of individual tool counts could therefore stay green. Capture the result and asserttoolsList === 1with this tokenizer to protect the calculation used for budget enforcement.src/testing/token-footprint.test.ts:78: This only asserts the names, and the fixture registers
find-tasks(the more expensive tool) beforeping, so the test would still pass if.sort()were removed frommeasureTokenFootprint. Reorder the fixture so a cheaper tool is registered first, or assert the totals are non-increasing (e.g.,expect(footprint.tools[0]!.total).toBeGreaterThan(footprint.tools[1]!.total)), so the "most expensive first" contract is actually pinned.src/testing/token-footprint.ts:87: Docstring says budget is "The most the fixed cost may be" (inclusive), but line 110 fails when
combined === budget(toBeLessThan). Adjust the wording to "must stay under" so the shipped contract matches the assertion.
- The one-array test asserts toolsList itself: with a tokenizer that returns 1 per call it is 1, where a sum over the tools would be 2. - The fixture registers the cheaper tool first, so only the sort puts the more expensive one ahead; both checks failed with the calculation or the sort taken out. - ExpectedToolAnnotations is RequiredToolAnnotations plus the title. - The budget docstring says "must stay under", as the check does, and the README example uses a fixed budget rather than an env override. - pad is gone in favour of padStart. readAdvertisedSurface stays exported: token-footprint.ts imports it.
|
🎉 This PR is included in version 0.4.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
…helpers (#693) ## What `@doist/mcp-core` 0.4.0 ships the shared test suites (todoist/mcp-core#7) and the host's side of the token contract (todoist/mcp-core#8). This adopts both, one commit each. **Test suites** - **`src/tools/tool-annotations.test.ts`** → `describeToolAnnotations`. All 49 tools' expected titles and hints are unchanged, now keyed by `ToolNames`. They're checked exactly as a client receives them on `tools/list`, defaults included. The old test spied on `McpServer.prototype.registerTool` and matched only partially. The MCP Apps `_meta` check for `find-tasks-by-date` stays, now reading the advertised tool through `listAdvertisedTools`. - **`src/token-footprint.test.ts`** → `describeTokenFootprint`. It measures the payload and the instructions from a running server, instead of rebuilding each entry with `z.toJSONSchema` and local copies of the registrar's title and `_meta` logic. **HTTP mode** (`requireValidTodoistToken`) - The `WWW-Authenticate` challenge comes from `buildBearerChallenge`, and the verdict cache from `createTokenVerdictCache`. They're the same pieces todoist-ai-integrations will use. - There's one cache per TTL (5 minutes static, 2 dynamic, or `cacheTtlMs`), shared by every middleware with that TTL, as the single module-wide cache was before. When full, it drops the oldest verdict, where it used to prune expired entries and then clear everything. `clearTokenValidationCache()` still empties them all. - It still fails open on any validation error, as before. The log line now says "Validation failed, proceeding" rather than "Transient error", since it covers every error. `fix(deps):` so the 0.4.0 bump releases, and todoist-ai-integrations gets a single copy of mcp-core. ## The budget | | Old test | Measured from the server | | --- | --- | --- | | `tools/list` payload (49 tools) | 35,624 | 35,177 | | instructions | 971 | 971 | | **Combined** | **36,595** | **36,148** | `DEFAULT_TOKEN_BUDGET` moves down from 36,600 to 36,200. The old count sat 5 tokens under the old budget; the real cost now has a similar small margin of 52. ## Verification - `npm run verify` passes. - The annotations file runs 51 tests: one per tool, the coverage check, and the `_meta` check. - The middleware's tests are unchanged and all pass: 401 and 403 challenges with and without `resource_metadata`, caching of each verdict, expiry under fake timers, a separate cache per base URL, and failing open. Together with `http-app.test.ts` that's 60 tests. They pin the behaviour across the refactor. From review, a new test fills the cache past 10,000 tokens and checks that the oldest verdict goes and the rest stay; it fails if the limit is off by one. The base URL test now fails if the cache key drops the base URL, because the two middlewares share a cache again.
## [14.0.2](v14.0.1...v14.0.2) (2026-11-09) ### Bug Fixes * **deps:** move to @doist/mcp-core 0.4.0, its shared suites and host helpers ([#693](#693)) ([4f986dd](4f986dd)), closes [todoist/mcp-core#7](todoist/mcp-core#7) [todoist/mcp-core#8](todoist/mcp-core#8)
What
Two new suites in
@doist/mcp-core/testing. Each replaces a test that all three servers keep their own copy of today.describeTokenFootprint({ createServer, countTokens, budget }), plusmeasureTokenFootprint(server, countTokens):tools/listpayload andinstructionsfrom a running server, through the same in-memory clientlistAdvertisedToolsuses;budget, or when the tools or the instructions go missing, since a lost block would otherwise read as a saving.The tokenizer is passed in as
countTokens, such as(text) => encode(text).lengthfromgpt-tokenizer, so mcp-core takes no dependency for it.describeToolAnnotations({ createServer, expected }): pins every tool's annotations as a client receives them ontools/list, with the title and theopenWorldHint: falsedefault included. It fails on a tool advertised without an expectation, or one expected but not advertised.expectedis keyed by tool name.readAdvertisedSurface(server)is new and internal: it reads both the tools and the instructions from one client.listAdvertisedToolsnow uses it.Why
tools/listentry themselves withz.toJSONSchema. todoist-mcp's also keeps copies of the registrar's title and_metalogic. So they measure what they assume a client receives, and drift when registration changes. comms-mcp's (fix: report the package version as the server version comms-mcp#85) already reads a running server; this generalises it.tool-annotations.test.tsfiles are the same table and checks, except that two of them read annotations off aregisterToolspy rather than offtools/list.feat:, so this releases 0.4.0. Nothing changes for existing exports.Verification
npm run verifypasses, includingsrc/subpath.test.ts, which checks the built./testingentry exports the same names as its barrel.measureTokenFootprintis tested directly on a fixture server:expected, and an expectation that leaves out a tool'sopenWorldHint: true. Each failed.Not verified