Skip to content

Assigning undefined to on* request setters leaves broken -32603 handlers, locks MethodRegistry, and breaks default requestDisplayMode #863

Open

Description

@adityajai119

Summary

All on* request-handler setters on App (onteardown, oncalltool, onlisttools) and AppBridge (onmessage, onopenlink, ondownloadfile, onrequestdisplaymode, onupdatemodelcontext, oncalltool, oncreatesamplingmessage, onlistresources, onlistresourcetemplates, onreadresource, onlistprompts) accept undefined to clear a previously registered handler (or forward an optional callback property).

Currently, assigning undefined still invokes this.replaceRequestHandler(...) with a fallback closure that throws Error("No on... handler set"), causing four problems:

  1. AppBridge.onrequestdisplaymode = undefined breaks the built-in default handler: AppBridge installs a default ui/request-display-mode handler in its constructor that returns { mode: this._hostContext.displayMode ?? "inline" }. Assigning bridge.onrequestdisplaymode = undefined (either to clear a custom override or when forwarding an optional props.onRequestDisplayMode) overwrites that default handler, causing app.requestDisplayMode(...) to reject with JSON-RPC -32603 (No onrequestdisplaymode handler set).
  2. App.oncalltool = undefined and App.onlisttools = undefined throw without tools capability: Assigning undefined on an App constructed without { tools } capability calls replaceRequestHandler("tools/call", ...), which triggers assertRequestHandlerCapability and synchronously throws Client does not support tool capability (required for tools/call).
  3. Cleared optional handlers return -32603 instead of -32601: Before an optional on* request setter (such as app.onteardown or bridge.onopenlink) is set, incoming requests fail with JSON-RPC -32601 (Method not found). After assigning a handler and clearing it with = undefined, incoming requests instead fail with -32603 (Internal error: No on... handler set).
  4. MethodRegistry remains locked after clearing: Because replaceRequestHandler keeps the method claimed in _methods instead of calling removeRequestHandler, calling setRequestHandler(method, ...) after on* = undefined still throws Handler for "<method>" already registered.

Minimal reproduction

import { InMemoryTransport } from "@modelcontextprotocol/client";
import { App } from "@modelcontextprotocol/ext-apps";
import { AppBridge } from "@modelcontextprotocol/ext-apps/app-bridge";

// 1. App without tools capability throws when assigning undefined:
const app = new App({ name: "view", version: "1.0.0" }, {}, { autoResize: false });
app.oncalltool = undefined; // Throws: Client does not support tool capability (required for tools/call)

// 2. AppBridge loses default ui/request-display-mode handler when assigned undefined:
const [appTransport, bridgeTransport] = InMemoryTransport.createLinkedPair();
const bridge = new AppBridge(null, { name: "host", version: "1.0.0" }, {});
bridge.onrequestdisplaymode = undefined;

await bridge.connect(bridgeTransport);
await app.connect(appTransport);
await app.requestDisplayMode({ mode: "fullscreen" });
// Rejects with ProtocolError (-32603): No onrequestdisplaymode handler set

Expected behavior

  • Assigning undefined to an on* request setter when a handler was previously set via that setter should unregister the request handler via removeRequestHandler(method) (releasing MethodRegistry and returning -32601 on subsequent requests), except AppBridge.onrequestdisplaymode = undefined, which should restore AppBridge's default ui/request-display-mode handler.
  • Assigning undefined when the on* request setter is already undefined should be a no-op (not throwing capability errors, not overwriting AppBridge's default ui/request-display-mode handler, and not removing a handler registered directly via setRequestHandler).

AI-assisted: root-caused and drafted with an AI coding agent; verified locally against main.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions