Skip to content
Draft
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
7 changes: 7 additions & 0 deletions .changeset/side-effects-flag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modelcontextprotocol/core': patch
'@modelcontextprotocol/client': patch
'@modelcontextprotocol/server': patch
---

The package manifests now declare `sideEffects`, so bundlers can leave out the modules an app does not use. The Cloudflare Workers shim is listed as having side effects, so its schema warm-up is kept. No runtime change.
4 changes: 4 additions & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"homepage": "https://modelcontextprotocol.io",
"bugs": "https://github.com/modelcontextprotocol/typescript-sdk/issues",
"type": "module",
"sideEffects": [
"./dist/shimsWorkerd.*"
],
"repository": {
"type": "git",
"url": "git+https://github.com/modelcontextprotocol/typescript-sdk.git"
Expand Down Expand Up @@ -172,6 +175,7 @@
"@types/eventsource": "catalog:devTools",
"@typescript/native-preview": "catalog:devTools",
"@eslint/js": "catalog:devTools",
"esbuild": "^0.27.7",
"eslint": "catalog:devTools",
"eslint-config-prettier": "catalog:devTools",
"eslint-plugin-n": "catalog:devTools",
Expand Down
46 changes: 46 additions & 0 deletions packages/client/test/client/treeShaking.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

import type { BuildOptions } from 'esbuild';
import { build } from 'esbuild';
import { beforeAll, describe, expect, test } from 'vitest';

import { ensureBuilt } from '../helpers/ensureBuilt';

const pkgDir = join(dirname(fileURLToPath(import.meta.url)), '../..');

/** Bundles an app that uses only `name` from the built package, the way a consumer's bundler would. */
async function bundleUsing(name: string, options?: BuildOptions): Promise<string> {
const { outputFiles } = await build({
stdin: { contents: `export { ${name} } from '@modelcontextprotocol/client';`, resolveDir: pkgDir },
bundle: true,
format: 'esm',
platform: 'browser',
tsconfigRaw: {},
...options,
write: false
});
return outputFiles[0]!.text;
}

describe('tree shaking (built dist, bundled with esbuild)', () => {
beforeAll(async () => {
await ensureBuilt(join(pkgDir, '../core'));
await ensureBuilt(pkgDir);
}, 180_000);

test('a browser app that uses only Protocol gets no Client, JSON Schema validator or OpenTelemetry', async () => {
const bundle = await bundleUsing('Protocol');
expect(bundle).toMatch(/\bProtocol = class\b/);
expect(bundle).not.toMatch(/\bClient = class\b/);
expect(bundle).not.toMatch(/cfWorkerProvider|@opentelemetry\/api/);
});

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

test.each(['Client', 'Protocol'])('a Workers app that uses only %s keeps the schema warm-up', async name => {
expect(await bundleUsing(name, { conditions: ['workerd'] })).toMatch(/^preloadSchemas\(\);/m);
});
});
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"homepage": "https://modelcontextprotocol.io",
"bugs": "https://github.com/modelcontextprotocol/typescript-sdk/issues",
"type": "module",
"sideEffects": false,
"repository": {
"type": "git",
"url": "git+https://github.com/modelcontextprotocol/typescript-sdk.git"
Expand Down
3 changes: 3 additions & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"homepage": "https://modelcontextprotocol.io",
"bugs": "https://github.com/modelcontextprotocol/typescript-sdk/issues",
"type": "module",
"sideEffects": [
"./dist/shimsWorkerd.*"
],
"repository": {
"type": "git",
"url": "git+https://github.com/modelcontextprotocol/typescript-sdk.git"
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading