diff --git a/src/server/index.ts b/src/server/index.ts index 77c5f5c7..a5345dd4 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -343,7 +343,9 @@ export class Server< } private setToolRequestHandlers() { - // TODO: Check that these handlers do not already exist + this.assertCanSetRequestHandler(ListToolsRequestSchema.shape.method.value); + this.assertCanSetRequestHandler(CallToolRequestSchema.shape.method.value); + // TODO: Register tool capability this.setRequestHandler(ListToolsRequestSchema, (): ListToolsResult => ({ diff --git a/src/shared/protocol.ts b/src/shared/protocol.ts index f430b319..9b4ba569 100644 --- a/src/shared/protocol.ts +++ b/src/shared/protocol.ts @@ -511,6 +511,15 @@ export abstract class Protocol< this._requestHandlers.delete(method); } + /** + * Asserts that a request handler has not already been set for the given method, in preparation for a new one being automatically installed. + */ + protected assertCanSetRequestHandler(method: string): void { + if (this._requestHandlers.has(method)) { + throw new Error(`A request handler for ${method} already exists, which would be overridden`); + } + } + /** * Registers a handler to invoke when this protocol object receives a notification with the given method. *