Skip to content

feat(testing): add token footprint and tool annotation suites - #7

Merged
scottlovegrove merged 2 commits into
mainfrom
feat/testing-suites
Nov 9, 2026
Merged

scottlovegrove merged 2 commits into
mainfrom
feat/testing-suites

Conversation

@scottlovegrove

Copy link
Copy Markdown
Collaborator

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 }), plus measureTokenFootprint(server, countTokens):

    • reads the tools/list payload and instructions from a running server, through the same in-memory client listAdvertisedTools uses;
    • counts the payload as the one array a client receives;
    • prints the cost per tool, most expensive first;
    • fails when the total reaches 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).length from gpt-tokenizer, so mcp-core takes no dependency for it.

  • describeToolAnnotations({ createServer, expected }): pins every tool's annotations as a client receives them on tools/list, with the title and the openWorldHint: false default included. It fails on a tool advertised without an expectation, or one expected but not advertised. expected is keyed by tool name.

readAdvertisedSurface(server) is new and internal: it reads both the tools and the instructions from one client. listAdvertisedTools now uses it.

Why

  • Token footprint: todoist-mcp's and automations-mcp's tests rebuild each tools/list entry themselves with z.toJSONSchema. todoist-mcp's also keeps copies of the registrar's title and _meta logic. 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.
  • Annotations: the three tool-annotations.test.ts files are the same table and checks, except that two of them read annotations off a registerTool spy rather than off tools/list.

feat:, so this releases 0.4.0. Nothing changes for existing exports.

Verification

  • npm run verify passes, including src/subpath.test.ts, which checks the built ./testing entry exports the same names as its barrel.
  • measureTokenFootprint is tested directly on a fixture server:
    • the payload is measured as one array of the advertised tools, titles included;
    • the instructions are added to the total;
    • the per-tool breakdown is sorted, most expensive first, with 0 for a tool that has no output schema;
    • missing instructions count as 0.
  • Both suites run against fixture servers. I checked they can fail with throwaway copies of the test files: a budget below the cost, a server with no instructions, a wrong hint, a wrong title, a tool missing from expected, and an expectation that leaves out a tool's openWorldHint: true. Each failed.

Not verified

  • The suites haven't run against the three servers yet. Their adoption PRs follow once 0.4.0 is out. The todoist-mcp and automations-mcp budgets will need re-basing then, because they'll measure the real payload.

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.
@scottlovegrove scottlovegrove self-assigned this Nov 9, 2026

@doistbot doistbot left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)
  • P3 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.
  • P3 src/testing/tool-annotations.ts:6: ExpectedToolAnnotations re-declares the three hint booleans and openWorldHint that RequiredToolAnnotations already models in src/tool.ts. Reuse it instead: import RequiredToolAnnotations from ../tool.js and define export type ExpectedToolAnnotations = RequiredToolAnnotations & { title: string }. This keeps the testing type in sync if the SDK's ToolAnnotations gains another hint.
  • P3 src/testing/token-footprint.ts:59: pad is a thin pass-through wrapper around the built-in String.prototype.padStart, and every call site passes a number. Drop the helper and write String(tool.total).padStart(5) (etc.) directly.
  • P3 src/testing/advertised-schemas.ts:13: readAdvertisedSurface is exported but not re-exported through src/testing/index.ts and no test imports it directly, so the export keyword doesn't put it in the built ./testing entry — per the no-dead-exports rule in AGENTS.md it should either join the barrel or drop the export keyword. 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.
  • P3 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 toolsList to an independent expected value. Returning zero or a sum of individual tool counts could therefore stay green. Capture the result and assert toolsList === 1 with this tokenizer to protect the calculation used for budget enforcement.
  • P3 src/testing/token-footprint.test.ts:78: This only asserts the names, and the fixture registers find-tasks (the more expensive tool) before ping, so the test would still pass if .sort() were removed from measureTokenFootprint. 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.
  • P3 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.

Share Feedback • Review Logs

- 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.
@scottlovegrove scottlovegrove added the 🚢 Ship PR requires passing checks before merge, but no independent human review. label Nov 9, 2026
@scottlovegrove
scottlovegrove merged commit be51330 into main Nov 9, 2026
5 checks passed
@scottlovegrove
scottlovegrove deleted the feat/testing-suites branch November 9, 2026 12:32
@doist-release-bot

Copy link
Copy Markdown

🎉 This PR is included in version 0.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

scottlovegrove added a commit to todoist/todoist-mcp that referenced this pull request Nov 9, 2026
…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.
doist-release-bot Bot added a commit to todoist/todoist-mcp that referenced this pull request Nov 9, 2026
## [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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released 🚢 Ship PR requires passing checks before merge, but no independent human review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants