Skip to content

test: add compile-time key-parity assertions for spec type checks#1652

Open
rechedev9 wants to merge 3 commits intomodelcontextprotocol:mainfrom
rechedev9:test/spec-type-key-parity-assertions
Open

test: add compile-time key-parity assertions for spec type checks#1652
rechedev9 wants to merge 3 commits intomodelcontextprotocol:mainfrom
rechedev9:test/spec-type-key-parity-assertions

Conversation

@rechedev9
Copy link

Summary

  • Adds 151 compile-time key-parity assertions to spec.types.test.ts that catch missing/extra .optional() fields between SDK and spec types
  • Introduces 3 utility types (KnownKeys<T>, AssertExactKeys<A,B>, Assert<T>) that produce descriptive compile errors on key-set mismatches
  • Documents 7 genuine SDK/spec key mismatches with @ts-expect-error annotations (serving as a living drift inventory)
  • Covers 151 of 174 sdkTypeChecks entries; 23 exclusions documented (union types and primitive aliases)

Motivation

Closes #1298.

The existing mutual-assignability checks (sdk = spec; spec = sdk) pass even when one type has optional keys that the other lacks, because TypeScript structural subtyping treats optional properties as compatible in both directions. This means a Zod schema missing .optional() on a field — or having .optional() when the spec requires the field — goes undetected.

The new key-parity layer compares the exact set of named property keys between each SDK/spec type pair at compile time, with zero runtime cost.

Test plan

  • pnpm typecheck:all passes (key-parity assertions validated at compile time)
  • pnpm test:all — 734 tests pass (440 core + 37 server + 257 client)
  • All 7 @ts-expect-error annotations verified as necessary
  • Verified that removing .optional() from a schema correctly triggers a compile error from the new assertions

Add 151 compile-time key-parity assertions to spec.types.test.ts that
verify SDK and spec types have exactly the same set of named property
keys. This catches missing .optional() parameters that the existing
mutual-assignability checks cannot detect due to TypeScript structural
subtyping.

Introduces three utility types:
- KnownKeys<T>: strips index signatures, keeps named keys only
- AssertExactKeys<A, B>: resolves to true on match, descriptive error
  type on mismatch
- Assert<T extends true>: constraint that forces compile error

Coverage: 151 of 174 sdkTypeChecks entries. 23 exclusions documented
(15 union types, 8 primitive aliases).

Discovered 7 genuine SDK/spec key mismatches, documented with
@ts-expect-error annotations as a living drift inventory.

Closes modelcontextprotocol#1298
@rechedev9 rechedev9 requested a review from a team as a code owner March 9, 2026 22:23
@changeset-bot
Copy link

changeset-bot bot commented Mar 9, 2026

⚠️ No Changeset found

Latest commit: 6427760

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 9, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1652

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1652

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1652

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1652

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1652

commit: 3a046cb

rechedev9 and others added 2 commits March 9, 2026 23:50
Fix CI lint failure by applying Prettier formatting to the
key-parity assertions test file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make spec type tests catch .optional() missing args too

1 participant