Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/client/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import type {
ReadResourceRequest,
Request,
RequestHandlerExtra,
RequestMethod,
RequestOptions,
RequestTypeMap,
Result,
ServerCapabilities,
ServerToClientRequestMethod,
SubscribeRequest,
Tool,
Transport,
Expand Down Expand Up @@ -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<M extends RequestMethod>(
// @ts-expect-error - Intentionally narrowing the method constraint for type safety
public override setRequestHandler<M extends ServerToClientRequestMethod>(
method: M,
handler: (
request: RequestTypeMap[M],
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2615,6 +2615,10 @@ export type NotificationMethod = ClientNotification['method'] | ServerNotificati
export type RequestTypeMap = MethodToTypeMap<ClientRequest | ServerRequest>;
export type NotificationTypeMap = MethodToTypeMap<ClientNotification | ServerNotification>;

// 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];
Expand Down
6 changes: 4 additions & 2 deletions packages/server/src/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
ClientCapabilities,
ClientToServerRequestMethod,
CreateMessageRequest,
CreateMessageRequestParamsBase,
CreateMessageRequestParamsWithTools,
Expand All @@ -21,7 +22,6 @@ import type {
ProtocolOptions,
Request,
RequestHandlerExtra,
RequestMethod,
RequestOptions,
RequestTypeMap,
ResourceUpdatedNotification,
Expand Down Expand Up @@ -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<M extends RequestMethod>(
// @ts-expect-error - Intentionally narrowing the method constraint for type safety
public override setRequestHandler<M extends ClientToServerRequestMethod>(
method: M,
handler: (
request: RequestTypeMap[M],
Expand Down
Loading