Skip to content

Commit d07003c

Browse files
committed
fix: use getLiteralValue for method literal extraction in setRequestHandler
Replace inline Zod v3/v4 literal extraction in Server.setRequestHandler and Client.setRequestHandler with the existing getLiteralValue() helper from zod-compat.ts, which already handles the _def.values[0] fallback for late Zod v3 releases (e.g., zod@3.25.1) where the literal value is stored under _def.values instead of _def.value. This fixes "Schema method literal must be a string" errors when constructing McpServer with zod@3.25.x. Fixes #1380
1 parent 9edbab7 commit d07003c

2 files changed

Lines changed: 4 additions & 30 deletions

File tree

src/client/index.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,9 @@ import type { JsonSchemaType, JsonSchemaValidator, jsonSchemaValidator } from '.
5656
import {
5757
AnyObjectSchema,
5858
SchemaOutput,
59+
getLiteralValue,
5960
getObjectShape,
60-
isZ4Schema,
6161
safeParse,
62-
type ZodV3Internal,
63-
type ZodV4Internal
6462
} from '../server/zod-compat.js';
6563
import type { RequestHandlerExtra } from '../shared/protocol.js';
6664
import { ExperimentalClientTasks } from '../experimental/tasks/client.js';
@@ -339,18 +337,7 @@ export class Client<
339337
throw new Error('Schema is missing a method literal');
340338
}
341339

342-
// Extract literal value using type-safe property access
343-
let methodValue: unknown;
344-
if (isZ4Schema(methodSchema)) {
345-
const v4Schema = methodSchema as unknown as ZodV4Internal;
346-
const v4Def = v4Schema._zod?.def;
347-
methodValue = v4Def?.value ?? v4Schema.value;
348-
} else {
349-
const v3Schema = methodSchema as unknown as ZodV3Internal;
350-
const legacyDef = v3Schema._def;
351-
methodValue = legacyDef?.value ?? v3Schema.value;
352-
}
353-
340+
const methodValue = getLiteralValue(methodSchema);
354341
if (typeof methodValue !== 'string') {
355342
throw new Error('Schema method literal must be a string');
356343
}

src/server/index.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,10 @@ import { AjvJsonSchemaValidator } from '../validation/ajv-provider.js';
4646
import type { JsonSchemaType, jsonSchemaValidator } from '../validation/types.js';
4747
import {
4848
AnyObjectSchema,
49+
getLiteralValue,
4950
getObjectShape,
50-
isZ4Schema,
5151
safeParse,
5252
SchemaOutput,
53-
type ZodV3Internal,
54-
type ZodV4Internal
5553
} from './zod-compat.js';
5654
import { RequestHandlerExtra } from '../shared/protocol.js';
5755
import { ExperimentalServerTasks } from '../experimental/tasks/server.js';
@@ -228,18 +226,7 @@ export class Server<
228226
throw new Error('Schema is missing a method literal');
229227
}
230228

231-
// Extract literal value using type-safe property access
232-
let methodValue: unknown;
233-
if (isZ4Schema(methodSchema)) {
234-
const v4Schema = methodSchema as unknown as ZodV4Internal;
235-
const v4Def = v4Schema._zod?.def;
236-
methodValue = v4Def?.value ?? v4Schema.value;
237-
} else {
238-
const v3Schema = methodSchema as unknown as ZodV3Internal;
239-
const legacyDef = v3Schema._def;
240-
methodValue = legacyDef?.value ?? v3Schema.value;
241-
}
242-
229+
const methodValue = getLiteralValue(methodSchema);
243230
if (typeof methodValue !== 'string') {
244231
throw new Error('Schema method literal must be a string');
245232
}

0 commit comments

Comments
 (0)