Repository navigation
Conversation
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 detectedLatest commit: 28d9473 The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
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 |
@modelcontextprotocol/client
@modelcontextprotocol/codemod
@modelcontextprotocol/core
@modelcontextprotocol/server
@modelcontextprotocol/server-legacy
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
There was a problem hiding this comment.
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.assigndoes mutatebuildInputRequired, but that function is referenced nowhere else inpackages/core-internal/src, so a bundler that drops the annotated call cannot leave a caller holding a builder withoutelicitand its siblings.packages/core-internal/src/types/specTypeSchema.ts—byTypeNamewalksSPEC_SCHEMA_KEYSand thenObject.entries(authSchemas)in the same order as the two removed loops, with the sameSchemasuffix trim and the same freeze, so the keys and their order inspecTypeSchemasandisSpecTypeare 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.
| 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); |
There was a problem hiding this comment.
🟡 (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]), |
There was a problem hiding this comment.
🟡 (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
Lets bundlers drop
specTypeSchemas,isSpecType,inputRequiredand 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
Appfrom@modelcontextprotocol/ext-apps, zod included:It also helps apps that use the main classes: about 14 kB less for
Clientand 27 kB less for the server (esbuild).How Has This Been Tested?
A new case in
treeShaking.test.tsfails 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
Additional context
Notes for review:
Protocollinks statically. Making the codec per-instance measured another 22 kB but adds API, so it is not proposed here. Related: v2: make protocol message validation pluggable so applications can drop zod #3178.Generated by Claude Code