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
20 changes: 18 additions & 2 deletions packages/core/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2707,10 +2707,26 @@ export function getResultSchema<M extends RequestMethod>(method: M): z.ZodType<R
type RequestSchemaType = (typeof ClientRequestSchema.options)[number] | (typeof ServerRequestSchema.options)[number];
type NotificationSchemaType = (typeof ClientNotificationSchema.options)[number] | (typeof ServerNotificationSchema.options)[number];

function buildSchemaMap<T extends { shape: { method: { value: string } } }>(schemas: readonly T[]): Record<string, T> {
type MethodLiteralSchema = {
value?: unknown;
_def?: {
value?: unknown;
values?: readonly unknown[];
};
};

function getMethodLiteral(methodSchema: MethodLiteralSchema): string {
const method = methodSchema.value ?? methodSchema._def?.value ?? methodSchema._def?.values?.[0];
if (typeof method !== 'string') {
throw new TypeError('Schema method literal must be a string');
}
return method;
}

function buildSchemaMap<T extends { shape: { method: MethodLiteralSchema } }>(schemas: readonly T[]): Record<string, T> {
const map: Record<string, T> = {};
for (const schema of schemas) {
const method = schema.shape.method.value;
const method = getMethodLiteral(schema.shape.method);
map[method] = schema;
}
return map;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Regression test for https://github.com/modelcontextprotocol/typescript-sdk/issues/1380
*
* Some Zod runtimes store literal values only in `._def.values[0]`, with both
* the top-level `.value` shortcut and `._def.value` absent. This test verifies
* McpServer initialization still succeeds in that scenario.
*/

import { afterEach, describe, expect, test, vi } from 'vitest';

describe('Issue #1380: Zod literal method extraction', () => {
afterEach(() => {
vi.resetModules();
vi.doUnmock('zod/v4');
});

test('should construct McpServer when method literal is stored only in _def.values[0]', async () => {
vi.resetModules();
vi.doMock('zod/v4', async () => {
const actual = await vi.importActual<typeof import('zod/v4')>('zod/v4');

return {
...actual,
literal: ((...args: Parameters<typeof actual.literal>) => {
const schema = actual.literal(...args);

// Simulate a Zod runtime that stores the literal value only in
// _def.values[0], with neither the top-level .value shortcut
// nor _def.value present.
//
// We wrap the schema in a plain object that shadows .value with
// undefined and exposes a new _def that has only .values (no .value).
const originalDef = (schema as Record<string, unknown>)._def as Record<string, unknown>;
const value = originalDef.value ?? originalDef.values?.[0] ?? (schema as Record<string, unknown>).value;

const strippedDef: Record<string, unknown> = {};
for (const [k, v] of Object.entries(originalDef)) {
if (k !== 'value') strippedDef[k] = v;
}
if (!strippedDef.values) {
strippedDef.values = [value];
}

return Object.create(schema as object, {
value: { get: () => void 0, enumerable: true, configurable: true },
_def: { get: () => strippedDef, enumerable: true, configurable: true }
}) as typeof schema;
}) as typeof actual.literal
};
});

const { McpServer } = await import('@modelcontextprotocol/server');

expect(
() =>
new McpServer({
name: 'test server',
version: '1.0'
})
).not.toThrow();
});
});
Loading