Skip to content

Commit dbba3a7

Browse files
refactor(core): un-deprecate schema-arg overloads; inline _registerCompatRequestHandler
The schema-arg forms of request()/setRequestHandler()/setNotificationHandler() are a parallel supported style, not superseded: request(req, schema) is the only typed form for non-spec method results, and the schema-first handler form gives full-envelope validation. Replaces the @deprecated tags with plain JSDoc guidance so the PR's own custom-method examples no longer render with strikethrough. Also inlines _registerCompatRequestHandler (single callsite) and fixes the stale 'schema parameter removed' note in migration.md.
1 parent 52843d3 commit dbba3a7

4 files changed

Lines changed: 15 additions & 23 deletions

File tree

docs/migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ import { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/server/valida
903903

904904
The following APIs are unchanged between v1 and v2 (only the import paths changed):
905905

906-
- `Client` constructor and most client methods (`connect`, `listTools`, `listPrompts`, `listResources`, `readResource`, etc.) — note: `callTool()` signature changed (schema parameter removed)
906+
- `Client` constructor and most client methods (`connect`, `listTools`, `listPrompts`, `listResources`, `readResource`, etc.) — note: `callTool()` schema parameter is now optional
907907
- `McpServer` constructor, `server.connect(transport)`, `server.close()`
908908
- `Server` (low-level) constructor and all methods
909909
- `StreamableHTTPClientTransport`, `SSEClientTransport`, `StdioClientTransport` constructors and options

packages/client/src/client/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ export class Client extends Protocol<ClientContext> {
343343
method: M,
344344
handler: (request: RequestTypeMap[M], ctx: ClientContext) => ResultTypeMap[M] | Promise<ResultTypeMap[M]>
345345
): void;
346-
/** @deprecated For spec methods, pass the method string instead. */
346+
/** For spec methods the method-string form is more concise; this overload is the supported call form for non-spec methods or when you want full-envelope validation. */
347347
public override setRequestHandler<T extends ZodLikeRequestSchema>(
348348
requestSchema: T,
349349
handler: (request: ReturnType<T['parse']>, ctx: ClientContext) => Result | Promise<Result>
@@ -891,7 +891,7 @@ export class Client extends Protocol<ClientContext> {
891891
* ```
892892
*/
893893
async callTool(params: CallToolRequest['params'], options?: RequestOptions): Promise<CallToolResult>;
894-
/** @deprecated The result schema is resolved internally; use `callTool(params)`. The second argument is accepted for v1 source compatibility and ignored. */
894+
/** The `resultSchema` argument is accepted for v1 source compatibility and ignored; output validation uses the tool's declared `outputSchema`. Prefer `callTool(params, options)`. */
895895
async callTool(params: CallToolRequest['params'], resultSchema: unknown, options?: RequestOptions): Promise<CallToolResult>;
896896
async callTool(
897897
params: CallToolRequest['params'],

packages/core/src/shared/protocol.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export type BaseContext = {
213213
request: { method: M; params?: Record<string, unknown> },
214214
options?: TaskRequestOptions
215215
): Promise<ResultTypeMap[M]>;
216-
/** @deprecated For spec methods, the result schema is resolved automatically; use `send(req)`. */
216+
/** For spec methods the one-argument form is more concise; this overload is the supported call form for non-spec methods or custom result shapes. */
217217
<T extends AnySchema>(request: Request, resultSchema: T, options?: TaskRequestOptions): Promise<SchemaOutput<T>>;
218218
};
219219

@@ -804,7 +804,7 @@ export abstract class Protocol<ContextT extends BaseContext> {
804804
request: { method: M; params?: Record<string, unknown> },
805805
options?: RequestOptions
806806
): Promise<ResultTypeMap[M]>;
807-
/** @deprecated For spec methods, the result schema is resolved automatically; use `request(req)`. */
807+
/** For spec methods the one-argument form is more concise; this overload is the supported call form for non-spec methods or custom result shapes. */
808808
request<T extends AnySchema>(request: Request, resultSchema: T, options?: RequestOptions): Promise<SchemaOutput<T>>;
809809
request(request: Request, optionsOrSchema?: RequestOptions | AnySchema, maybeOptions?: RequestOptions): Promise<unknown> {
810810
if (optionsOrSchema && '~standard' in optionsOrSchema) {
@@ -1043,32 +1043,24 @@ export abstract class Protocol<ContextT extends BaseContext> {
10431043
method: M,
10441044
handler: (request: RequestTypeMap[M], ctx: ContextT) => Result | Promise<Result>
10451045
): void;
1046-
/** @deprecated For spec methods, pass the method string instead. */
1046+
/** For spec methods the method-string form is more concise; this overload is the supported call form for non-spec methods or when you want full-envelope validation. */
10471047
setRequestHandler<T extends ZodLikeRequestSchema>(
10481048
requestSchema: T,
10491049
handler: (request: ReturnType<T['parse']>, ctx: ContextT) => Result | Promise<Result>
10501050
): void;
10511051
setRequestHandler(method: string | ZodLikeRequestSchema, handler: (request: Request, ctx: ContextT) => Result | Promise<Result>): void {
10521052
if (isZodLikeSchema(method)) {
1053-
return this._registerCompatRequestHandler(method, handler as (request: unknown, ctx: ContextT) => Result | Promise<Result>);
1053+
const requestSchema = method;
1054+
const methodStr = extractMethodLiteral(requestSchema);
1055+
this.assertRequestHandlerCapability(methodStr);
1056+
this._requestHandlers.set(methodStr, (request, ctx) =>
1057+
Promise.resolve((handler as (req: unknown, ctx: ContextT) => Result | Promise<Result>)(requestSchema.parse(request), ctx))
1058+
);
1059+
return;
10541060
}
10551061
this._setRequestHandlerByMethod(method, handler);
10561062
}
10571063

1058-
/**
1059-
* Dispatches the Zod-schema form of `setRequestHandler` — extracts the method literal from the
1060-
* schema and registers a handler that parses the full request through it. Called by the base
1061-
* {@linkcode Protocol.setRequestHandler} overload dispatcher for the schema-first signature.
1062-
*/
1063-
protected _registerCompatRequestHandler(
1064-
requestSchema: ZodLikeRequestSchema,
1065-
handler: (request: unknown, ctx: ContextT) => Result | Promise<Result>
1066-
): void {
1067-
const methodStr = extractMethodLiteral(requestSchema);
1068-
this.assertRequestHandlerCapability(methodStr);
1069-
this._requestHandlers.set(methodStr, (request, ctx) => Promise.resolve(handler(requestSchema.parse(request), ctx)));
1070-
}
1071-
10721064
/**
10731065
* Registers a request handler by method string, bypassing the public overload set.
10741066
* Used by `Client`/`Server` overrides to forward without `as RequestMethod` casts.
@@ -1109,7 +1101,7 @@ export abstract class Protocol<ContextT extends BaseContext> {
11091101
method: M,
11101102
handler: (notification: NotificationTypeMap[M]) => void | Promise<void>
11111103
): void;
1112-
/** @deprecated For spec methods, pass the method string instead. */
1104+
/** For spec methods the method-string form is more concise; this overload is the supported call form for non-spec methods or when you want full-envelope validation. */
11131105
setNotificationHandler<T extends ZodLikeRequestSchema>(
11141106
notificationSchema: T,
11151107
handler: (notification: ReturnType<T['parse']>) => void | Promise<void>

packages/server/src/server/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export class Server extends Protocol<ServerContext> {
231231
method: M,
232232
handler: (request: RequestTypeMap[M], ctx: ServerContext) => ResultTypeMap[M] | Promise<ResultTypeMap[M]>
233233
): void;
234-
/** @deprecated For spec methods, pass the method string instead. */
234+
/** For spec methods the method-string form is more concise; this overload is the supported call form for non-spec methods or when you want full-envelope validation. */
235235
public override setRequestHandler<T extends ZodLikeRequestSchema>(
236236
requestSchema: T,
237237
handler: (request: ReturnType<T['parse']>, ctx: ServerContext) => Result | Promise<Result>

0 commit comments

Comments
 (0)