diff --git a/.changeset/client-droppable.md b/.changeset/client-droppable.md new file mode 100644 index 0000000000..fb64c53019 --- /dev/null +++ b/.changeset/client-droppable.md @@ -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. diff --git a/packages/client/src/client/client.ts b/packages/client/src/client/client.ts index 0c45041337..a4605dc14e 100644 --- a/packages/client/src/client/client.ts +++ b/packages/client/src/client/client.ts @@ -463,8 +463,8 @@ const LIST_CHANGED_EVICTIONS: Readonly> = { 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( @@ -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) }; /** @@ -618,13 +623,6 @@ export class Client extends Protocol { /** 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 diff --git a/packages/client/test/client/treeShaking.test.ts b/packages/client/test/client/treeShaking.test.ts index 787883849d..9219299900 100644 --- a/packages/client/test/client/treeShaking.test.ts +++ b/packages/client/test/client/treeShaking.test.ts @@ -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/); + }); + test('a browser app that uses Client still gets its JSON Schema validator', async () => { expect(await bundleUsing('Client')).toMatch(/\bCfWorkerJsonSchemaValidator = class\b/); });