Skip to content

refactor(specTypeSchema): drop as unknown as casts; add allowlist drift guard#1993

Merged
KKonstantinov merged 2 commits intomainfrom
fweinberger/spec-type-schema-followups
Apr 30, 2026
Merged

refactor(specTypeSchema): drop as unknown as casts; add allowlist drift guard#1993
KKonstantinov merged 2 commits intomainfrom
fweinberger/spec-type-schema-followups

Conversation

@felixweinberger
Copy link
Copy Markdown
Contributor

Follow-ups from #1887 review (#1887 (review)):

  • Retype _specTypeSchemas as Record<string, StandardSchemaV1> so the freeze-site cast is a single as SchemaRecord instead of as unknown as. Same for _isSpecType/GuardRecord.
  • Add a runtime drift-guard test: Object.keys(isSpecType) (minus auth schemas) must equal every PascalCase *Schema export from schemas.ts minus the 5 documented internal helpers. A new schema added to schemas.ts will fail this test until it is allowlisted.

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature
  • Breaking change
  • Documentation update

…dRecord exports

Retyping the staging record from `Record<string, z.ZodTypeAny>` to
`Record<string, StandardSchemaV1>` gives TS overlap with `SchemaRecord`
(every `SchemaRecord[K]` is a `StandardSchemaV1`, so the cast direction
is a permitted narrowing). The guard record already overlapped because
`(v) => v is T` is a subtype of `(v) => boolean`. Both exports are now
single `as` casts inside `Object.freeze()` instead of double-casts.
…exports

Compares `Object.keys(isSpecType)` (protocol entries only) against the
PascalCase `*Schema` exports of schemas.ts minus the documented internal
helpers. A new schema added to schemas.ts now fails this test until it
is either added to SPEC_SCHEMA_KEYS or to INTERNAL_HELPER_SCHEMAS.
@felixweinberger felixweinberger requested a review from a team as a code owner April 30, 2026 12:28
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 30, 2026

⚠️ No Changeset found

Latest commit: 65ab998

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

@felixweinberger felixweinberger added the v2-bc v2 backwards-compatibility series label Apr 30, 2026
@felixweinberger felixweinberger added this to the v2.0.0-bc milestone Apr 30, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 30, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@1993

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@1993

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@1993

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@1993

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@1993

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@1993

commit: 65ab998

Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

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

LGTM — type-annotation cleanup with no runtime change, plus a drift-guard test that passes against current schemas.ts.

Extended reasoning...

Overview

Two-part follow-up to #1887 review feedback:

  1. specTypeSchema.ts (4 lines): retypes _specTypeSchemas as Record<string, StandardSchemaV1> and register's param as z.ZodType, so the freeze-site casts become single-step as SchemaRecord / as GuardRecord instead of as unknown as. The intermediate record's value type is now a supertype of the target mapped type's values, which is what makes the direct cast legal.
  2. specTypeSchema.test.ts: adds a drift-guard test asserting Object.keys(isSpecType) (minus auth entries) equals every PascalCase *Schema export from schemas.ts minus the 5 documented internal helpers.

Security risks

None. No I/O, no auth, no transport code — pure type annotations and a test. No new exports or public-API surface change.

Level of scrutiny

Low. The src change is type-annotation-only; register() still calls schema.safeParse(v) on a z.ZodType, and Zod v4 types implement StandardSchemaV1, so the assignment into the record is sound. The exported specTypeSchemas / isSpecType values and their public types (SchemaRecord / GuardRecord) are unchanged — only the path the compiler takes to accept the cast differs. Runtime behavior is byte-identical.

Other factors

  • Ran vitest on the test file: 18/18 pass including the new drift guard, confirming SPEC_SCHEMA_KEYS is currently in sync with schemas.ts.
  • Verified schemas.ts exports no OAuth*/OpenId* names, so the actual-side filter (!k.startsWith('OAuth') && !k.startsWith('OpenId')) cannot accidentally drop a protocol schema and the two sides of the comparison are drawn from disjoint sources as intended.
  • The INTERNAL_HELPER_SCHEMAS list in the test mirrors the exclusion comment in specTypeSchema.ts exactly (5 entries).
  • No changeset is appropriate: internal refactor + test only, no consumer-visible change.

Copy link
Copy Markdown
Contributor

@KKonstantinov KKonstantinov left a comment

Choose a reason for hiding this comment

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

LGTM

@KKonstantinov KKonstantinov merged commit 4fbcfcd into main Apr 30, 2026
22 of 23 checks passed
@KKonstantinov KKonstantinov deleted the fweinberger/spec-type-schema-followups branch April 30, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2-bc v2 backwards-compatibility series

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants