Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/client-droppable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/client': patch
---

esbuild, webpack and Bun can again drop `Client` from bundles that do not use it. Since 2.4.0 they kept it, together with the JSON Schema validator. No runtime change.
16 changes: 7 additions & 9 deletions packages/client/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ const LIST_CHANGED_EVICTIONS: Readonly<Record<string, readonly string[]>> = {

const DEFAULT_LIST_MAX_PAGES = 64;

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

export const clientInternals: {
enforceStrictCapabilities(client: Client): boolean;
logger(client: Client): SdkLogger;
walkPages<R extends { nextCursor?: string }>(
Expand All @@ -478,6 +478,11 @@ export const clientInternals = {} as {
params: ReadResourceRequest['params'],
options?: CacheableRequestOptions
): Promise<{ result: ReadResourceResult; cached: boolean }>;
} = {
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)
Comment on lines +482 to +485

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

};

/**
Expand Down Expand Up @@ -618,13 +623,6 @@ export class Client extends Protocol<ClientContext> {
/** Backing store for {@linkcode getDiscoverResult}. Per-connection. */
private _discoverResult?: DiscoverResult;

static {
clientInternals.enforceStrictCapabilities = client => client._enforceStrictCapabilities;
clientInternals.logger = client => client._logger;
clientInternals.walkPages = (client, method, fetchPage, items) => client._walkPages(method, fetchPage, items);
clientInternals.readResource = (client, params, options) => client._readResource(params, options);
}

/**
* Clears every per-connection field in one place. Called at the start of
* each fresh (non-resuming) connect and from `close()`, so a stale
Expand Down
4 changes: 4 additions & 0 deletions packages/client/test/client/treeShaking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ describe('tree shaking (built dist, bundled with esbuild)', () => {
expect(bundle).not.toMatch(/cfWorkerProvider|@opentelemetry\/api/);
});

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

});

test('a browser app that uses Client still gets its JSON Schema validator', async () => {
expect(await bundleUsing('Client')).toMatch(/\bCfWorkerJsonSchemaValidator = class\b/);
});
Expand Down
Loading