diff --git a/packages/client/src/client/client.ts b/packages/client/src/client/client.ts index 9c2c87290..b0a8e1c83 100644 --- a/packages/client/src/client/client.ts +++ b/packages/client/src/client/client.ts @@ -24,11 +24,11 @@ import type { ReadResourceRequest, Request, RequestHandlerExtra, - RequestMethod, RequestOptions, RequestTypeMap, Result, ServerCapabilities, + ServerToClientRequestMethod, SubscribeRequest, Tool, Transport, @@ -327,8 +327,10 @@ export class Client< /** * Override request handler registration to enforce client-side validation for elicitation. + * Only server-to-client methods are valid (sampling/createMessage, elicitation/create, roots/list). */ - public override setRequestHandler( + // @ts-expect-error - Intentionally narrowing the method constraint for type safety + public override setRequestHandler( method: M, handler: ( request: RequestTypeMap[M], diff --git a/packages/core/src/types/types.ts b/packages/core/src/types/types.ts index 9d659693f..63b0222f5 100644 --- a/packages/core/src/types/types.ts +++ b/packages/core/src/types/types.ts @@ -2615,6 +2615,10 @@ export type NotificationMethod = ClientNotification['method'] | ServerNotificati export type RequestTypeMap = MethodToTypeMap; export type NotificationTypeMap = MethodToTypeMap; +// Narrowed method types for Client and Server request handlers +export type ServerToClientRequestMethod = ServerRequest['method']; +export type ClientToServerRequestMethod = ClientRequest['method']; + /* Runtime schema lookup */ type RequestSchemaType = (typeof ClientRequestSchema.options)[number] | (typeof ServerRequestSchema.options)[number]; type NotificationSchemaType = (typeof ClientNotificationSchema.options)[number] | (typeof ServerNotificationSchema.options)[number]; diff --git a/packages/server/src/server/server.ts b/packages/server/src/server/server.ts index ca43272cf..7c9bd9c8d 100644 --- a/packages/server/src/server/server.ts +++ b/packages/server/src/server/server.ts @@ -1,5 +1,6 @@ import type { ClientCapabilities, + ClientToServerRequestMethod, CreateMessageRequest, CreateMessageRequestParamsBase, CreateMessageRequestParamsWithTools, @@ -21,7 +22,6 @@ import type { ProtocolOptions, Request, RequestHandlerExtra, - RequestMethod, RequestOptions, RequestTypeMap, ResourceUpdatedNotification, @@ -214,8 +214,10 @@ export class Server< /** * Override request handler registration to enforce server-side validation for tools/call. + * Only client-to-server methods are valid (tools/call, prompts/get, resources/read, etc.). */ - public override setRequestHandler( + // @ts-expect-error - Intentionally narrowing the method constraint for type safety + public override setRequestHandler( method: M, handler: ( request: RequestTypeMap[M],