Assert tool request handlers do not already exist

This commit is contained in:
Justin Spahr-Summers 2025-01-07 14:13:47 +00:00
commit 4bf5c5358a
No known key found for this signature in database
2 changed files with 12 additions and 1 deletions

View file

@ -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 => ({

View file

@ -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.
*