Skip to content

Commit f456351

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 f456351

2 files changed

Lines changed: 4 additions & 42 deletions

File tree

src/client/index.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,7 @@ import {
5353
} from '../types.js';
5454
import { AjvJsonSchemaValidator } from '../validation/ajv-provider.js';
5555
import type { JsonSchemaType, JsonSchemaValidator, jsonSchemaValidator } from '../validation/types.js';
56-
import {
57-
AnyObjectSchema,
58-
SchemaOutput,
59-
getObjectShape,
60-
isZ4Schema,
61-
safeParse,
62-
type ZodV3Internal,
63-
type ZodV4Internal
64-
} from '../server/zod-compat.js';
56+
import { AnyObjectSchema, SchemaOutput, getLiteralValue, getObjectShape, safeParse } from '../server/zod-compat.js';
6557
import type { RequestHandlerExtra } from '../shared/protocol.js';
6658
import { ExperimentalClientTasks } from '../experimental/tasks/client.js';
6759
import { assertToolsCallTaskCapability, assertClientRequestTaskCapability } from '../experimental/tasks/helpers.js';
@@ -339,18 +331,7 @@ export class Client<
339331
throw new Error('Schema is missing a method literal');
340332
}
341333

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-
334+
const methodValue = getLiteralValue(methodSchema);
354335
if (typeof methodValue !== 'string') {
355336
throw new Error('Schema method literal must be a string');
356337
}

src/server/index.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,7 @@ import {
4444
} from '../types.js';
4545
import { AjvJsonSchemaValidator } from '../validation/ajv-provider.js';
4646
import type { JsonSchemaType, jsonSchemaValidator } from '../validation/types.js';
47-
import {
48-
AnyObjectSchema,
49-
getObjectShape,
50-
isZ4Schema,
51-
safeParse,
52-
SchemaOutput,
53-
type ZodV3Internal,
54-
type ZodV4Internal
55-
} from './zod-compat.js';
47+
import { AnyObjectSchema, getLiteralValue, getObjectShape, safeParse, SchemaOutput } from './zod-compat.js';
5648
import { RequestHandlerExtra } from '../shared/protocol.js';
5749
import { ExperimentalServerTasks } from '../experimental/tasks/server.js';
5850
import { assertToolsCallTaskCapability, assertClientRequestTaskCapability } from '../experimental/tasks/helpers.js';
@@ -228,18 +220,7 @@ export class Server<
228220
throw new Error('Schema is missing a method literal');
229221
}
230222

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-
223+
const methodValue = getLiteralValue(methodSchema);
243224
if (typeof methodValue !== 'string') {
244225
throw new Error('Schema method literal must be a string');
245226
}

0 commit comments

Comments
 (0)