Skip to content

perf(core): mark module-level lookup tables as pure - #3257

Open
ochafik wants to merge 1 commit into
claude/loving-johnson-onwvrg-droppable-clientfrom
claude/loving-johnson-onwvrg-pure-tables
Open

ochafik wants to merge 1 commit into
claude/loving-johnson-onwvrg-droppable-clientfrom
claude/loving-johnson-onwvrg-pure-tables

Conversation

@ochafik

@ochafik ochafik commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Lets bundlers drop specTypeSchemas, isSpecType, inputRequired and the elicitation and validation-ladder tables when an app does not use them. Last of the stack that starts at #3254.

Motivation and Context

These are built by top-level calls and loops in the shared chunk, so every bundle keeps them and the schemas they reference.

Minified size of a bundle using App from @modelcontextprotocol/ext-apps, zod included:

esbuild webpack rolldown rollup
with #3254 256 kB 245 kB 246 kB 243 kB
with this 225 kB 220 kB 222 kB 224 kB
plus #3255 220 kB 216 kB 218 kB 219 kB

It also helps apps that use the main classes: about 14 kB less for Client and 27 kB less for the server (esbuild).

How Has This Been Tested?

A new case in treeShaking.test.ts fails before and passes after. Suites as described in #3254. The ext-apps unit tests pass against packed builds of the full set.

Breaking Changes

None.

Types of changes

  • Refactoring (non-breaking change which makes things better)

Additional context

Notes for review:


Generated by Claude Code

specTypeSchemas, isSpecType, inputRequired and the elicitation and ladder
tables were built by top-level calls that bundlers could not drop.

Co-authored-by: ochafik <ochafik@anthropic.com>
@changeset-bot

changeset-bot Bot commented Nov 30, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 28d9473

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@modelcontextprotocol/client Patch
@modelcontextprotocol/server Patch
@modelcontextprotocol/codemod Patch
@modelcontextprotocol/core Patch
@modelcontextprotocol/server-legacy Patch
@modelcontextprotocol/core-internal Patch

Not sure what this means? Click here to learn what changesets are.

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 30, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@3257

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@3257

@modelcontextprotocol/server

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

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@3257

@modelcontextprotocol/express

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

@modelcontextprotocol/fastify

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 28d9473

@claude claude Bot added the v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes label Dec 1, 2026
@ochafik
ochafik marked this pull request as ready for review December 1, 2026 15:00
@ochafik
ochafik requested a review from a team as a code owner December 1, 2026 15:00

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline comments, two other things were checked and ruled out:

  • packages/core-internal/src/shared/inputRequired.ts:132 — Object.assign does mutate buildInputRequired, but that function is referenced nowhere else in packages/core-internal/src, so a bundler that drops the annotated call cannot leave a caller holding a builder without elicit and its siblings.
  • packages/core-internal/src/types/specTypeSchema.ts — byTypeName walks SPEC_SCHEMA_KEYS and then Object.entries(authSchemas) in the same order as the two removed loops, with the same Schema suffix trim and the same freeze, so the keys and their order in specTypeSchemas and isSpecType are unchanged.

Nothing was built or run here, so the new treeShaking.test.ts case was read, not executed.

Extended reasoning...

The change adds /* @ __PURE__ */ annotations and IIFE wrappers to module-level tables in four packages/core-internal source files, rewrites the specTypeSchemas/isSpecType registration as a pure helper, and adds one esbuild bundle assertion plus a changeset. It touches no auth, injection or data-exposure surface. The Object.assign purity concern and the key-order question were examined by reading and ruled out.

Comment on lines +43 to +45
test('a browser app that uses only Protocol gets none of the lookup tables it does not use', async () => {
const bundle = await bundleUsing('Protocol');
expect(bundle).not.toMatch(/^var (specTypeSchemas|isSpecType|inputRequired|PROPERTY_KEYS_BY_TYPE|INBOUND_VALIDATION_LADDER) =/m);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 (optional) Contributors who already have a built packages/client/dist get a failing test after pulling this change, and a later regression can pass locally. The new assertion depends on dist being rebuilt from the edited core-internal sources. ensureBuilt returns as soon as the sentinel files exist (packages/client/test/helpers/ensureBuilt.ts:65) and never rebuilds. Fix: make the dist under test reflect current sources, for example rebuild when core-internal sources are newer than the sentinels, or build to a fresh temp outDir for this suite.

Why this was flagged

The trigger is a local checkout whose packages/client/dist was built before this commit, then running the client test suite. treeShaking.test.ts:27-30 calls ensureBuilt, and packages/client/test/helpers/ensureBuilt.ts:62-65 returns immediately when every sentinel file exists, with no freshness check. The stale dist still holds the old register() loops from specTypeSchema.ts, so esbuild keeps var specTypeSchemas = and the not.toMatch at treeShaking.test.ts:45 fails. The reverse also holds: after a dist is built with this change, removing a /* @ __PURE__ */ in source leaves the test green locally. On base the two older tests in this file share the helper, but none of them asserts on core-internal source content that this same diff changes. CI is not affected because .github/workflows/main.yml:33 runs pnpm run build:all first. Remedy: add a staleness check or a fresh build for this suite.

Verification: Triggers when a local checkout already has a complete packages/client/dist built before this commit and the client suite is run without rebuilding. packages/client/test/helpers/ensureBuilt.ts:65 is if (haveAll() && !existsSync(lockDir)) return;. A dist built earlier holds the old code, so the not.toMatch at :45 fails. CI is unaffected.

Finding ID: f-5558cc1c

function byTypeName<T>(value: (schema: z.ZodType) => T): Readonly<Record<string, T>> {
const entries: [string, z.ZodType][] = [
// eslint-disable-next-line import/namespace -- key is constrained to keyof typeof schemas via the satisfies clause above
...SPEC_SCHEMA_KEYS.map(key => [key, schemas[key]] as [string, z.ZodType]),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 (optional) Maintainers lose a compile-time check: a non-Zod export listed in SPEC_SCHEMA_KEYS now compiles and breaks isSpecType.X at call time. The base passed each value to register(key: string, schema: z.ZodType), so every listed export had to be assignable to z.ZodType. The new code at specTypeSchema.ts:261 uses as [string, z.ZodType], and an assertion on a union only needs some member to be comparable. Fix: keep the values checked, not asserted, for example type the mapped tuple with satisfies or pass schemas[key] through a z.ZodType parameter. No such export exists in schemas.ts today.

Why this was flagged

The trigger is a future edit that adds to SPEC_SCHEMA_KEYS a key of ./schemas whose value is not a Zod schema; the list is only constrained by satisfies readonly (keyof typeof schemas)[] at packages/core-internal/src/types/specTypeSchema.ts:196. At specTypeSchema.ts:261 the pair is built as [key, schemas[key]] as [string, z.ZodType]. schemas[key] is a union over about 200 schema types, and a type assertion is accepted when any union member is comparable to the target, so one bad member does not fail the build. The consumer then gets a TypeError from schema.safeParse at specTypeSchema.ts:307 on the first isSpecType.X(...) call, and a non-schema value in specTypeSchemas.X. On the base branch the same edit is a compile error, because the loop called register(key, schemas[key]) with a schema: z.ZodType parameter. The dismissal relied on the satisfies clause, which checks key names only, not value types. I grepped schemas.ts and found no non-schema export today, so this is a removed guard, not a live failure.

Verification: Triggers only when a future edit adds a non-Zod export of ./schemas to SPEC_SCHEMA_KEYS; nothing breaks in the code as merged. Base passed each value through a typed parameter: function register(key: string, schema: z.ZodType). Head, packages/core-internal/src/types/specTypeSchema.ts:261, uses as [string, z.ZodType]. The list is only constrained by satisfies readonly (keyof typeof schemas)[] (line 196).

Finding ID: f-4b3a23d4

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants