Skip to content

Commit d4083ab

Browse files
ochafikclaude
andcommitted
refactor: remove setDefaultRequestHandler, use replaceRequestHandler
setDefaultRequestHandler was an untracked handler registration that could silently overwrite user-set handlers if called after on* setters. replaceRequestHandler serves the same purpose (allows re-registration) without the silent-overwrite risk. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent a94154e commit d4083ab

3 files changed

Lines changed: 4 additions & 17 deletions

File tree

src/app-bridge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export class AppBridge extends ProtocolWithEvents<
372372

373373
// Default handler for requestDisplayMode - returns current mode from host context.
374374
// Hosts can override this by setting bridge.onrequestdisplaymode = ...
375-
this.setDefaultRequestHandler(
375+
this.replaceRequestHandler(
376376
McpUiRequestDisplayModeRequestSchema,
377377
(request) => {
378378
const currentMode = this._hostContext.displayMode ?? "inline";

src/app.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,9 @@ export class App extends ProtocolWithEvents<
415415
}
416416
this._toolHandlersInitialized = true;
417417

418-
// Register via setDefaultRequestHandler so users can still override with
418+
// Register via replaceRequestHandler so users can still override with
419419
// their own oncalltool/onlisttools after calling registerTool.
420-
this.setDefaultRequestHandler(
420+
this.replaceRequestHandler(
421421
CallToolRequestSchema,
422422
async (request, extra) => {
423423
const tool = this._registeredTools[request.params.name];
@@ -427,7 +427,7 @@ export class App extends ProtocolWithEvents<
427427
return (tool.handler as any)(request.params.arguments as any, extra);
428428
},
429429
);
430-
this.setDefaultRequestHandler(ListToolsRequestSchema, async () => {
430+
this.replaceRequestHandler(ListToolsRequestSchema, async () => {
431431
const tools: Tool[] = Object.entries(this._registeredTools)
432432
.filter(([_, tool]) => tool.enabled)
433433
.map(([name, tool]) => {

src/events.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,6 @@ export abstract class ProtocolWithEvents<
188188

189189
// ── Handler registration with double-set protection ─────────────────
190190

191-
/**
192-
* Register a request handler without tracking it. Subclass constructors
193-
* use this for overridable defaults — a later `setRequestHandler` call
194-
* (or `on*` setter) for the same method will succeed, replacing the
195-
* default.
196-
*/
197-
protected setDefaultRequestHandler: Protocol<
198-
SendRequestT,
199-
SendNotificationT,
200-
SendResultT
201-
>["setRequestHandler"] = (schema, handler) =>
202-
super.setRequestHandler(schema, handler);
203-
204191
// The two overrides below are arrow-function class fields rather than
205192
// prototype methods so that Protocol's constructor — which registers its
206193
// own ping/cancelled/progress handlers via `this.setRequestHandler`

0 commit comments

Comments
 (0)