Check capabilities when request handlers are set

This commit is contained in:
Justin Spahr-Summers 2024-11-12 12:16:43 +00:00
commit 8b307a5d62
No known key found for this signature in database

View file

@ -277,6 +277,13 @@ export abstract class Protocol<
method: SendNotificationT["method"],
): void;
/**
* A method to check if a request handler is supported by the local side, for the given method to be handled.
*
* This should be implemented by subclasses.
*/
protected abstract assertRequestHandlerCapability(method: string): void;
/**
* Sends a request and wait for a response, with optional progress notifications in the meantime (if supported by the server).
*
@ -360,7 +367,9 @@ export abstract class Protocol<
requestSchema: T,
handler: (request: z.infer<T>) => SendResultT | Promise<SendResultT>,
): void {
this._requestHandlers.set(requestSchema.shape.method.value, (request) =>
const method = requestSchema.shape.method.value;
this.assertRequestHandlerCapability(method);
this._requestHandlers.set(method, (request) =>
Promise.resolve(handler(requestSchema.parse(request))),
);
}