Repository navigation
Conversation
Lets bundlers leave out unused modules. The workerd shim is listed so its schema warm-up survives. Co-authored-by: ochafik <ochafik@anthropic.com>
🦋 Changeset detectedLatest commit: 23538c4 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: |
| "sideEffects": [ | ||
| "./dist/shimsWorkerd.*" | ||
| ], |
There was a problem hiding this comment.
🟡 (optional) Workers apps built with webpack or rspack that import only re-exported names such as Protocol lose the schema warm-up they get on the base branch. The list at packages/client/package.json:10-12 leaves dist/index.mjs side-effect free, and those bundlers skip such a re-exporting module, so its _shims import is never reached. Fix: keep the workerd shim reachable without evaluating the root entry, e.g. also list ./dist/index.*, which covers the 2 sites listed. Same pattern at 2 sites (packages/client/package.json:12, packages/server/package.json:12). I did not run webpack; esbuild keeps the shim.
Why this was flagged
Trigger: an app bundled by webpack or rspack with the workerd condition whose only imports from the package are re-exports of code that never imports _shims, e.g. import { Protocol } from '@ modelcontextprotocol/client'. With the manifest flag, webpack points the import straight at the core-internal chunk and drops dist/index.mjs with its imports, so preloadSchemas(); (packages/client/src/shimsWorkerd.ts:15) is never bundled. On base there is no sideEffects field, the root entry is evaluated and the call runs at isolate warm-up; after, schema construction lands in the first request of each fresh isolate. The glob does mark the shim as side-effectful, but nothing kept imports it. packages/client/test/client/treeShaking.test.ts:43-45 pins this case for esbuild only. Apps using Client or McpServer are unaffected. Based on reading; webpack was not run.
Verification: packages/client/package.json:10-12 lists only ./dist/shimsWorkerd.*, so dist/index.mjs and every shared chunk are declared side-effect free. The warm-up is the module-scope preloadSchemas(); at packages/client/src/shimsWorkerd.ts:15. It is reached only through the _shims import. packages/client/test/client/treeShaking.test.ts:43-45 checks the Protocol case with esbuild only.
Finding ID: f-872e844a
| "sideEffects": [ | ||
| "./dist/shimsWorkerd.*" | ||
| ], |
There was a problem hiding this comment.
🟡 nit (optional): Workers users of the server package rely on a sideEffects list that no automated test bundles, so a lost schema warm-up would go unnoticed. packages/server/package.json:10-12 gets the same declaration as the client, but only the client has treeShaking.test.ts. The server's existing workerdSchemaPreload.test.ts reads dist files and never runs a bundler. Fix: add a server counterpart that bundles @ modelcontextprotocol/server with the workerd condition and asserts the top-level preloadSchemas(); call survives; packages/server/test/helpers/ensureBuilt.ts already exists for it.
Why this was flagged
The condition is a server dist whose entry or chunk layout reaches @ modelcontextprotocol/server/_shims differently from the client, now or after a later build change. packages/server/package.json:10-12 marks every dist file except ./dist/shimsWorkerd.* as side-effect free, so a bundler may drop the module whose top-level preloadSchemas() (packages/server/src/shimsWorkerd.ts:15) warms the schemas. Then a Workers app pays schema construction inside the first request of each fresh isolate, which the base branch never risks because it declares no sideEffects. packages/server/test/server/workerdSchemaPreload.test.ts:60-63 only checks that dist/shimsWorkerd.mjs contains the call; it does not bundle, so it stays green. The changeset at .changeset/side-effects-flag.md:7 states the warm-up is kept for all three packages, and for the server nothing in the repository checks that. I did not run a build or bundle to see the server output.
Verification: packages/server/package.json:10-12 adds "sideEffects": ["./dist/shimsWorkerd.*"], the same declaration as the client. packages/server/test/server/workerdSchemaPreload.test.ts:51-53 reads dist files with readFileSync and matches regexes on shimsWorkerd.mjs/.cjs (lines 60-63). Nothing here shows a present regression.
Finding ID: f-34033d40
|
|
||
| const pkgDir = join(dirname(fileURLToPath(import.meta.url)), '../..'); | ||
|
|
||
| /** Bundles an app that uses only `name` from the built package, the way a consumer's bundler would. */ |
There was a problem hiding this comment.
🟡 nit (optional): bundleUsing is a private test helper but carries a JSDoc block, which CLAUDE.md keeps for the public API. Fix: make it a one-line // comment, or drop it. [also at: packages/client/test/client/treeShaking.test.ts:12 - nit: CLAUDE.md principle 8 keeps JSDoc to the public API: bundleUsing in packages/client/test/client/treeShaking.test.ts is a file-local, unexported test helper, yet it carries a /** ... */ JSDoc block.]
Why this was flagged
There is no runtime trigger; this is a convention check on a new file. packages/client/test/client/treeShaking.test.ts:12 puts a /** ... */ block on bundleUsing, a non-exported function used only inside this test file. CLAUDE.md principle 8 limits JSDoc to the public API and asks for one-line inline comments elsewhere. Nothing a user or caller sees changes against the base branch. No safeguard applies: ESLint runs on src/ only (packages/client/package.json:145), so it does not flag test files.
Verification: nit — no runtime trigger; a comment-form rule from the root CLAUDE.md is broken by a line this diff adds. packages/client/test/client/treeShaking.test.ts:12 is a /** ... */ block on async function bundleUsing(...) at line 13. The function is not exported and is called only inside this file (lines 33, 40, 44), so it is not public API. Nothing a user, caller or build observes changes.
Finding ID: f-c9fe8190
Declares
sideEffectsin thecore,clientandservermanifests so bundlers can leave out modules an app does not use. First of a stack of three.Motivation and Context
Browser code that only needs
Protocolcurrently getsClient, the JSON Schema validator and@opentelemetry/apitoo. MCP Apps views and hosts (@modelcontextprotocol/ext-apps) are in that position.Minified size of a bundle using
Appfrom ext-apps, zod included, against packed builds ofmain:shimsWorkerdcallspreloadSchemas()at module scope, soclientandserverlist it instead of usingfalse.How Has This Been Tested?
New
treeShaking.test.tsbundles the built client with esbuild. It fails onmainand passes here, and checks the Workers warm-up survives. The server warm-up was checked by hand only. The core, core-internal, client, server, integration and e2e suites were run on the full stack with no new failures, using unlocked dependencies; three tests fail there with and without this change.Breaking Changes
None.
Types of changes
Additional context
Notes for review:
esbuildbecomes a devDependency ofclient(already in the lockfile). Fine, and wanted inservertoo?server-legacyand the middleware packages are not audited or changed.Generated by Claude Code