From 4bf5c5358a4c00303938d4a88790d6a97ee6b0f0 Mon Sep 17 00:00:00 2001 From: Justin Spahr-Summers Date: Tue, 7 Jan 2025 14:13:47 +0000 Subject: [PATCH] Assert tool request handlers do not already exist --- src/server/index.ts | 4 +++- src/shared/protocol.ts | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) 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. *