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
6 changes: 6 additions & 0 deletions .changeset/pure-lookup-tables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modelcontextprotocol/client': patch
'@modelcontextprotocol/server': patch
---

Bundlers can now drop `specTypeSchemas`, `isSpecType`, `inputRequired` and some internal lookup tables from bundles that do not use them. No runtime change.
5 changes: 5 additions & 0 deletions packages/client/test/client/treeShaking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ describe('tree shaking (built dist, bundled with esbuild)', () => {
expect(await bundleUsing('Protocol', { ignoreAnnotations: true })).not.toMatch(/\bClient = class\b/);
});

test('a browser app that uses only Protocol gets none of the lookup tables it does not use', async () => {
const bundle = await bundleUsing('Protocol');
expect(bundle).not.toMatch(/^var (specTypeSchemas|isSpecType|inputRequired|PROPERTY_KEYS_BY_TYPE|INBOUND_VALIDATION_LADDER) =/m);
});

test('a browser app that uses Client still gets its JSON Schema validator', async () => {
expect(await bundleUsing('Client')).toMatch(/\bCfWorkerJsonSchemaValidator = class\b/);
});
Expand Down
14 changes: 7 additions & 7 deletions packages/core-internal/src/shared/elicitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ function isAnnotationOnlyJsonSchemaKeyword(key: string): boolean {

// The wire grammar, derived from the wire schemas so it tracks spec revisions. `$schema`
// is spec-declared on the root but reaches the wire type via its catchall.
const ROOT_KEYS = new Set(['$schema', ...Object.keys(ElicitRequestFormParamsSchema.shape.requestedSchema.shape)]);
const ROOT_KEYS = /* @__PURE__ */ (() => new Set(['$schema', ...Object.keys(ElicitRequestFormParamsSchema.shape.requestedSchema.shape)]))();

const PROPERTY_KEYS_BY_TYPE: Record<string, ReadonlySet<string>> = {
string: shapeKeys([
string: /* @__PURE__ */ shapeKeys([
StringSchemaSchema,
UntitledSingleSelectEnumSchemaSchema,
TitledSingleSelectEnumSchemaSchema,
LegacyTitledEnumSchemaSchema
]),
number: shapeKeys([NumberSchemaSchema]),
integer: shapeKeys([NumberSchemaSchema]),
boolean: shapeKeys([BooleanSchemaSchema]),
array: shapeKeys([UntitledMultiSelectEnumSchemaSchema, TitledMultiSelectEnumSchemaSchema])
number: /* @__PURE__ */ shapeKeys([NumberSchemaSchema]),
integer: /* @__PURE__ */ shapeKeys([NumberSchemaSchema]),
boolean: /* @__PURE__ */ shapeKeys([BooleanSchemaSchema]),
array: /* @__PURE__ */ shapeKeys([UntitledMultiSelectEnumSchemaSchema, TitledMultiSelectEnumSchemaSchema])
};

const SUPPORTED_STRING_FORMATS: ReadonlySet<string> = new Set(StringSchemaSchema.shape.format.unwrap().options);
const SUPPORTED_STRING_FORMATS: ReadonlySet<string> = /* @__PURE__ */ (() => new Set(StringSchemaSchema.shape.format.unwrap().options))();

/** Walks one property node: keeps grammar keys, drops the library format pattern, rejects unknown constraints. */
function walkProperty(node: unknown, path: string, vendor: string, unsupported: string[]): unknown {
Expand Down
4 changes: 2 additions & 2 deletions packages/core-internal/src/shared/inboundClassification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export interface InboundValidationRungDescriptor {
* rung's rationale for the ordering caveat. The order is the precedence: a
* request that fails several rungs is answered by the earliest one.
*/
export const INBOUND_VALIDATION_LADDER: readonly InboundValidationRungDescriptor[] = [
export const INBOUND_VALIDATION_LADDER: readonly InboundValidationRungDescriptor[] = /* @__PURE__ */ (() => [
{
rung: 'http-method',
order: 1,
Expand Down Expand Up @@ -372,7 +372,7 @@ export const INBOUND_VALIDATION_LADDER: readonly InboundValidationRungDescriptor
'rung (e.g. order-6 `request-params`, -32602) is answered by this gate first with 400 / -32020, not by the ' +
'earlier-ordered rung.'
}
];
])();

/* ------------------------------------------------------------------------ *
* HTTP status mapping for ladder-originated errors
Expand Down
2 changes: 1 addition & 1 deletion packages/core-internal/src/shared/inputRequired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function buildInputRequired(spec: InputRequiredSpec): InputRequiredResult {
* });
* ```
*/
export const inputRequired: InputRequiredBuilder = Object.assign(buildInputRequired, {
export const inputRequired: InputRequiredBuilder = /* @__PURE__ */ Object.assign(buildInputRequired, {
elicit(params: ElicitInputParams): InputRequest {
try {
return { method: 'elicitation/create', params: normalizeElicitInputParams(params) };
Expand Down
24 changes: 9 additions & 15 deletions packages/core-internal/src/types/specTypeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,13 @@ type SpecTypeInputs = {
type SchemaRecord = { readonly [K in SpecTypeName]: StandardSchemaV1Sync<SpecTypeInputs[K], SpecTypes[K]> };
type GuardRecord = { readonly [K in SpecTypeName]: (value: unknown) => value is SpecTypeInputs[K] };

const _specTypeSchemas: Record<string, StandardSchemaV1> = {};
const _isSpecType: Record<string, (value: unknown) => boolean> = {};
function register(key: string, schema: z.ZodType): void {
const name = key.slice(0, -'Schema'.length);
_specTypeSchemas[name] = schema;
_isSpecType[name] = (v: unknown) => schema.safeParse(v).success;
}
for (const key of SPEC_SCHEMA_KEYS) {
// eslint-disable-next-line import/namespace -- key is constrained to keyof typeof schemas via the satisfies clause above
register(key, schemas[key]);
}
for (const [key, schema] of Object.entries(authSchemas)) {
register(key, schema);
function byTypeName<T>(value: (schema: z.ZodType) => T): Readonly<Record<string, T>> {
const entries: [string, z.ZodType][] = [
// eslint-disable-next-line import/namespace -- key is constrained to keyof typeof schemas via the satisfies clause above
...SPEC_SCHEMA_KEYS.map(key => [key, schemas[key]] as [string, z.ZodType]),
...Object.entries(authSchemas)
];
return Object.freeze(Object.fromEntries(entries.map(([key, schema]) => [key.slice(0, -'Schema'.length), value(schema)])));
}

/**
Expand All @@ -288,7 +282,7 @@ for (const [key, schema] of Object.entries(authSchemas)) {
* }
* ```
*/
export const specTypeSchemas: SchemaRecord = Object.freeze(_specTypeSchemas as SchemaRecord);
export const specTypeSchemas: SchemaRecord = /* @__PURE__ */ byTypeName<StandardSchemaV1>(schema => schema) as SchemaRecord;

/**
* Type predicates for every MCP spec type, keyed by type name.
Expand All @@ -310,4 +304,4 @@ export const specTypeSchemas: SchemaRecord = Object.freeze(_specTypeSchemas as S
* const blocks = mixed.filter(isSpecType.ContentBlock);
* ```
*/
export const isSpecType: GuardRecord = Object.freeze(_isSpecType as GuardRecord);
export const isSpecType: GuardRecord = /* @__PURE__ */ byTypeName(schema => (v: unknown) => schema.safeParse(v).success) as GuardRecord;
Loading