Repository navigation
fix(client): let bundlers drop an unused Client #3256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: claude/loving-johnson-onwvrg-side-effects
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
| export const clientInternals: { | ||
| enforceStrictCapabilities(client: Client): boolean; | ||
| logger(client: Client): SdkLogger; | ||
| walkPages<R extends { nextCursor?: string }>( | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 nit (optional): Apps that mangle Why this was flaggedA consumer build that runs esbuild 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: |
||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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/); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 (optional) Contributors who already have a built Why this was flaggedThe trigger is a local checkout where Verification: Triggers when a local checkout already has a complete Finding ID: |
||
| }); | ||
|
|
||
| test('a browser app that uses Client still gets its JSON Schema validator', async () => { | ||
| expect(await bundleUsing('Client')).toMatch(/\bCfWorkerJsonSchemaValidator = class\b/); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
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
clientInternalsare told a static block keepsClientin 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 withoutClient = 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 onclientInternalssays 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 declaressideEffectsand packages/client/test/client/treeShaking.test.ts:32-37 assertsexpect(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 whenignoreAnnotations: trueis 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 insideClient, packages/client/test/client/treeShaking.test.ts already assertedexpect(bundle).not.toMatch(/\bClient = class\b/)for a default esbuild bundle. Nothing breaks at runtime.Finding ID:
f-450fcd24