@@ -44,6 +44,9 @@ import {
4444 ProtocolErrorCode ,
4545 SUPPORTED_PROTOCOL_VERSIONS
4646} from '../types/index.js' ;
47+ import type { ZodLikeRequestSchema } from '../util/compatSchema.js' ;
48+ import { extractMethodLiteral , isZodLikeSchema } from '../util/compatSchema.js' ;
49+ import { deprecate } from '../util/deprecate.js' ;
4750import type { AnySchema , SchemaOutput } from '../util/schema.js' ;
4851import { parseSchema } from '../util/schema.js' ;
4952import type { StandardSchemaV1 } from '../util/standardSchema.js' ;
@@ -849,23 +852,48 @@ export class Protocol<S extends ProtocolSpec = ProtocolSpec, ContextT extends Ba
849852 resultSchema : R ,
850853 options ?: RequestOptions
851854 ) : Promise < SchemaOutput < R > > ;
855+ /** @deprecated The result schema is resolved automatically from the method name. Removed in v3. */
856+ request < M extends RequestMethod > (
857+ request : { method : M ; params ?: Record < string , unknown > } ,
858+ resultSchema : ZodLikeRequestSchema | AnySchema ,
859+ options ?: RequestOptions
860+ ) : Promise < ResultTypeMap [ M ] > ;
852861 request (
853862 requestOrMethod : { method : RequestMethod ; params ?: Record < string , unknown > } | string ,
854- optionsOrParams ?: RequestOptions | Record < string , unknown > ,
855- resultSchema ?: AnySchema ,
863+ optionsOrParams ?: RequestOptions | Record < string , unknown > | AnySchema | ZodLikeRequestSchema ,
864+ resultSchema ?: AnySchema | RequestOptions ,
856865 options ?: RequestOptions
857866 ) : Promise < unknown > {
858867 if ( typeof requestOrMethod === 'string' ) {
859868 return this . _requestWithSchema (
860869 { method : requestOrMethod , params : optionsOrParams as Record < string , unknown > | undefined } as Request ,
861- resultSchema ! ,
870+ resultSchema as AnySchema ,
862871 options
863872 ) ;
864873 }
874+ // v1-compat: request(reqObj, ResultSchema, opts?) — schema arg is ignored.
875+ if ( isZodLikeSchema ( optionsOrParams ) || ( optionsOrParams && '~standard' in ( optionsOrParams as object ) ) ) {
876+ deprecate (
877+ 'request(req, schema)' ,
878+ 'Protocol.request(request, ResultSchema) is deprecated; the result schema is resolved automatically from the method name.'
879+ ) ;
880+ const schema = getResultSchema ( requestOrMethod . method ) ;
881+ return this . _requestWithSchema ( requestOrMethod as Request , schema , resultSchema as RequestOptions | undefined ) ;
882+ }
865883 const schema = getResultSchema ( requestOrMethod . method ) ;
866884 return this . _requestWithSchema ( requestOrMethod as Request , schema , optionsOrParams as RequestOptions | undefined ) ;
867885 }
868886
887+ /**
888+ * Non-overloaded alias for {@linkcode notification} that always takes a
889+ * `Notification` object. Exists so that test code can replace
890+ * `client.notification` with a single-signature mock without TypeScript
891+ * complaining about overload-intersection assignability.
892+ */
893+ sendNotification ( notification : Notification , options ?: NotificationOptions ) : Promise < void > {
894+ return this . notification ( notification , options ) ;
895+ }
896+
869897 /**
870898 * Sends a request and waits for a response, using the provided schema for validation.
871899 *
@@ -1134,11 +1162,30 @@ export class Protocol<S extends ProtocolSpec = ProtocolSpec, ContextT extends Ba
11341162 paramsSchema : P ,
11351163 handler : ( params : SchemaOutput < P > , ctx : ContextT ) => Result | Promise < Result >
11361164 ) : void ;
1165+ /** @deprecated Pass the method string instead of a Zod schema. Removed in v3. */
1166+ setRequestHandler < T extends ZodLikeRequestSchema > (
1167+ requestSchema : T ,
1168+ handler : ( request : ReturnType < T [ 'parse' ] > , ctx : ContextT ) => Result | Promise < Result >
1169+ ) : void ;
11371170 setRequestHandler (
1138- method : string ,
1171+ method : string | ZodLikeRequestSchema ,
11391172 schemaOrHandler : AnySchema | ( ( request : Request , ctx : ContextT ) => Result | Promise < Result > ) ,
11401173 maybeHandler ?: ( params : unknown , ctx : ContextT ) => unknown
11411174 ) : void {
1175+ if ( isZodLikeSchema ( method ) ) {
1176+ deprecate (
1177+ 'setRequestHandler(schema)' ,
1178+ "setRequestHandler(ZodSchema, handler) is deprecated. Pass the method string, e.g. setRequestHandler('tools/call', handler)."
1179+ ) ;
1180+ const requestSchema = method ;
1181+ const methodStr = extractMethodLiteral ( requestSchema ) ;
1182+ const handler = schemaOrHandler as ( request : unknown , ctx : ContextT ) => Result | Promise < Result > ;
1183+ this . assertRequestHandlerCapability ( methodStr ) ;
1184+ this . _requestHandlers . set ( methodStr , ( request , ctx ) =>
1185+ Promise . resolve ( handler ( requestSchema . parse ( request ) , ctx ) )
1186+ ) ;
1187+ return ;
1188+ }
11421189 this . assertRequestHandlerCapability ( method ) ;
11431190
11441191 if ( maybeHandler === undefined ) {
@@ -1202,11 +1249,27 @@ export class Protocol<S extends ProtocolSpec = ProtocolSpec, ContextT extends Ba
12021249 paramsSchema : P ,
12031250 handler : ( params : SchemaOutput < P > ) => void | Promise < void >
12041251 ) : void ;
1252+ /** @deprecated Pass the method string instead of a Zod schema. Removed in v3. */
1253+ setNotificationHandler < T extends ZodLikeRequestSchema > (
1254+ notificationSchema : T ,
1255+ handler : ( notification : ReturnType < T [ 'parse' ] > ) => void | Promise < void >
1256+ ) : void ;
12051257 setNotificationHandler (
1206- method : string ,
1258+ method : string | ZodLikeRequestSchema ,
12071259 schemaOrHandler : AnySchema | ( ( notification : Notification ) => void | Promise < void > ) ,
12081260 maybeHandler ?: ( params : unknown ) => void | Promise < void >
12091261 ) : void {
1262+ if ( isZodLikeSchema ( method ) ) {
1263+ deprecate (
1264+ 'setNotificationHandler(schema)' ,
1265+ "setNotificationHandler(ZodSchema, handler) is deprecated. Pass the method string, e.g. setNotificationHandler('notifications/initialized', handler)."
1266+ ) ;
1267+ const notificationSchema = method ;
1268+ const methodStr = extractMethodLiteral ( notificationSchema ) ;
1269+ const handler = schemaOrHandler as ( notification : unknown ) => void | Promise < void > ;
1270+ this . _notificationHandlers . set ( methodStr , n => Promise . resolve ( handler ( notificationSchema . parse ( n ) ) ) ) ;
1271+ return ;
1272+ }
12101273 if ( maybeHandler === undefined ) {
12111274 const handler = schemaOrHandler as ( notification : Notification ) => void | Promise < void > ;
12121275 const schema = getNotificationSchema ( method as NotificationMethod ) ;
0 commit comments