Repository navigation
Conversation
The static block that filled clientInternals made the class a side effect for esbuild, webpack and Bun. Define the accessors as a plain object. Co-authored-by: ochafik <ochafik@anthropic.com>
🦋 Changeset detectedLatest commit: b1394d0 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:
- Type checking of the new accessors: the four members are TypeScript
private(not#private), so the bracket access is still checked against their declared types, including the generic signature of_walkPages. - Anything else that would keep
Clientin a bundle:packages/client/src/client/client.tshas no otherstaticmember or block, and the newclientInternalsinitialiser is an object literal of arrow functions with no call at module level. This was settled by reading the source; I did not build the package or run the new esbuild test, and webpack and Bun output was not examined.
Extended reasoning...
The change touches three files in the client package: it replaces a class static block in packages/client/src/client/client.ts with a module-level object of accessor closures, adds one esbuild bundle test, and adds a patch changeset. The accessors are package-internal and used only by packages/client/src/ext/skills/skillsClient.ts. It touches no security-sensitive surface (no auth, input handling or data exposure).
| enforceStrictCapabilities: client => client['_enforceStrictCapabilities'], | ||
| logger: client => client['_logger'], | ||
| walkPages: (client, method, fetchPage, items) => client['_walkPages'](method, fetchPage, items), | ||
| readResource: (client, params, options) => client['_readResource'](params, options) |
There was a problem hiding this comment.
🟡 nit (optional): Apps that mangle _-prefixed properties inside dependencies get a broken SkillsClient after this change; on the base it keeps working. The four accessors at client.ts:482-485 read private members with quoted keys such as client['_walkPages']. esbuild mangleProps renames the class members but leaves quoted keys alone. Fix: reach all four members with dot access, for example through a cast to a package-internal interface, so a mangler renames both sides together. Reach is narrow: such a build must already reserve _meta, which protocol.ts:763 writes unquoted to the wire.
Why this was flagged
A consumer build that runs esbuild mangleProps matching _-prefixed names over @ modelcontextprotocol/client, reserves wire names such as _meta, and uses SkillsClient from ext/skills. The mangler renames _logger (client.ts:586), _enforceStrictCapabilities (client.ts:608), _walkPages (client.ts:1810) and _readResource (client.ts:1964), but not the quoted keys at client.ts:482-485. Then skillsClient.ts:164 reads undefined, so supported ignores enforceStrictCapabilities; skillsClient.ts:183-185 throws a TypeError reading debug of undefined; skillsClient.ts:243 and skillsClient.ts:345 throw "is not a function". The base static block used dot access inside the class, so both sides were renamed together. No safeguard applies: crossBundleBrand.ts:47-48 only documents mangling as breaking the brand statics.
Verification: The head reaches the four private members with string keys at packages/client/src/client/client.ts:482-485. The same members are declared and used unquoted in the class. A mangler that skips quoted keys renames these and leaves 482-485 alone. The base used dot access inside the removed static block, so both sides were renamed together.
Finding ID: f-f4a68379
| }); | ||
|
|
||
| test('Client is dropped on its own merits, not only because of the chunk it lands in', async () => { | ||
| expect(await bundleUsing('Protocol', { ignoreAnnotations: true })).not.toMatch(/\bClient = class\b/); |
There was a problem hiding this comment.
🟡 (optional) Contributors who already have a built dist will likely see the new test fail after pulling this change, with no hint that a rebuild is needed. The test bundles the built package, but ensureBuilt returns at once when the sentinel files exist (packages/client/test/helpers/ensureBuilt.ts:65). A dist built on the parent commit still has the static {} block, so the bundle should still contain Client = class. Fix: make the test rebuild when src is newer than dist, or have it fail with a message that names the stale build. I did not run the test.
Why this was flagged
The trigger is a local checkout where packages/client/dist was built before this commit, for example by running the tree-shaking suite on the parent commit. ensureBuilt at packages/client/test/helpers/ensureBuilt.ts:62-65 only checks that the sentinel files exist and never compares them with the sources. This change adds no sentinel, so the old dist passes the fast path. That dist still holds the static block the diff removes from packages/client/src/client/client.ts, and with ignoreAnnotations: true esbuild keeps the class, so the assertion at treeShaking.test.ts:40 fails. The fast path is base code, but the base tests at treeShaking.test.ts:32-37 rely on the live package.json sideEffects field, not on freshly built class bodies. This is the first case in the file that turns red on a dist from the immediate parent. The reverse also holds: a stale post-change dist hides a later regression. CI was not checked here.
Verification: Triggers when a local checkout already has a complete packages/client/dist built before this commit. packages/client/test/helpers/ensureBuilt.ts:62 defines haveAll as sentinels.every(s => existsSync(join(pkgDir, 'dist', s))), and line 65 returns immediately. Nothing compares dist with src. CI is unaffected.
Finding ID: f-06c92560
|
|
||
| // Package-internal (not exported from the package): what the `ext/` clients share with Client. Set in Client's static block. | ||
| export const clientInternals = {} as { | ||
| // Package-internal (not exported from the package): what the `ext/` clients share with Client. Not a static block, which would keep Client in every bundle. |
There was a problem hiding this comment.
🟡 nit (optional): Maintainers reading the new comment on clientInternals are told a static block keeps Client in every bundle, which the repo's own test contradicts. The first case in treeShaking.test.ts:32-37 was added on the base, while the static block was still there, and asserts an esbuild bundle without Client = class. Rollup and rolldown were never affected either. Fix: make the comment say only what holds, for example that esbuild, webpack and Bun treat a static block as a side effect, or drop the reasoning clause and leave it to the commit message. [also at: packages/client/src/client/client.ts:466 - nit: CLAUDE.md principle 6 ("The words are true") asks comments to say what the code does: the rewritten comment on clientInternals says a static block "would keep Client in every bundle".]
Why this was flagged
No runtime trigger; this is a wording check on the comment changed at packages/client/src/client/client.ts:466. The comment now reads "Not a static block, which would keep Client in every bundle." On the base, where the static {} block still existed, packages/client/package.json:10-12 already declares sideEffects and packages/client/test/client/treeShaking.test.ts:32-37 asserts expect(bundle).not.toMatch(/\bClient = class\b/) for a default esbuild bundle, so a static block did not keep Client in every bundle. The new test at treeShaking.test.ts:39-41 only shows the difference when ignoreAnnotations: true is set. I did not run the tests; this rests on reading them. Nothing breaks for users, but a later maintainer gets a stronger claim than the code supports, which is what the "words are true" principle guards against. No safeguard applies to comment text.
Verification: The changed line at packages/client/src/client/client.ts:466 now reads: "Not a static block, which would keep Client in every bundle." At the base commit, where the static {} block was still inside Client, packages/client/test/client/treeShaking.test.ts already asserted expect(bundle).not.toMatch(/\bClient = class\b/) for a default esbuild bundle. Nothing breaks at runtime.
Finding ID: f-450fcd24
Replaces the
static {}block inClientwith a plain object, so esbuild, webpack and Bun can drop the class when an app does not use it. Stacked on #3254 for its test helper.Motivation and Context
The block added in #3047 (released in 2.4.0) counts as a side effect for those bundlers, so they keep
Clientand the JSON Schema validator. A bundle usingAppfrom@modelcontextprotocol/ext-appswent from 255 kB on 2.3.1 to 319 kB on 2.4.0 (esbuild, minified, zod included). Rollup and rolldown are not affected.On
mainalone this saves about 82 kB. On top of #3254 it saves nothing today, becauseClienthappens to sit in its own chunk. It stops the result depending on that chunking.How Has This Been Tested?
A new case in
treeShaking.test.tsbundles with annotations ignored; it fails before and passes after. Bun was checked by hand. Suites as described in #3254.Breaking Changes
None expected; see below.
Types of changes
Additional context
Notes for review:
_-prefixed names inside its dependencies would breakext/skills. Acceptable, or is another shape preferred?Serverhas a similar static block, left alone here.Generated by Claude Code