Skip to content

fix(client): let bundlers drop an unused Client - #3256

Open
ochafik wants to merge 1 commit into
claude/loving-johnson-onwvrg-side-effectsfrom
claude/loving-johnson-onwvrg-droppable-client
Open

ochafik wants to merge 1 commit into
claude/loving-johnson-onwvrg-side-effectsfrom
claude/loving-johnson-onwvrg-droppable-client

Conversation

@ochafik

@ochafik ochafik commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Replaces the static {} block in Client with 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 Client and the JSON Schema validator. A bundle using App from @modelcontextprotocol/ext-apps went 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 main alone this saves about 82 kB. On top of #3254 it saves nothing today, because Client happens 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.ts bundles 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

  • Bug fix (non-breaking change which fixes an issue)

Additional context

Notes for review:

  • Quoted access to private members is not renamed by property manglers, so an app that mangles _-prefixed names inside its dependencies would break ext/skills. Acceptable, or is another shape preferred?
  • Server has a similar static block, left alone here.

Generated by Claude Code

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-bot

changeset-bot Bot commented Nov 30, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b1394d0

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/codemod Patch
@modelcontextprotocol/core Patch
@modelcontextprotocol/server-legacy Patch
@modelcontextprotocol/server 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@3256

@modelcontextprotocol/codemod

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

@modelcontextprotocol/core

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

@modelcontextprotocol/server

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

@modelcontextprotocol/server-legacy

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

@modelcontextprotocol/express

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

@modelcontextprotocol/fastify

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: b1394d0

@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 14:59
@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:

  • 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 Client in a bundle: packages/client/src/client/client.ts has no other static member or block, and the new clientInternals initialiser 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).

Comment on lines +482 to +485
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)

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.

🟡 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/);

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 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.

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.

🟡 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

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