Add ui/notifications/request-teardown notification for view-initiated termination (#215)

* Add ui/close-resource request for UI to initiate termination

* Add missing METHOD export

* Fix typo in remaining ui/request-close

* Align spec terminology to teardown/termination instead of close/clearnup

* Implement final comments

* Removing view behavior on initiation of request termination
This commit is contained in:
Frédéric Barthelet 2026-03-19 21:01:59 +01:00 • committed by GitHub
commit c026b398a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -74,6 +74,8 @@ import {
McpUiDownloadFileResult,
McpUiResourceTeardownRequest,
McpUiResourceTeardownResultSchema,
McpUiRequestTeardownNotification,
McpUiRequestTeardownNotificationSchema,
McpUiSandboxProxyReadyNotification,
McpUiSandboxProxyReadyNotificationSchema,
McpUiSizeChangedNotificationSchema,
@ -673,6 +675,41 @@ export class AppBridge extends Protocol<
);
}
/**
* Register a handler for app-initiated teardown request notifications from the view.
*
* The view sends `ui/notifications/request-teardown` when it wants the host to tear it down.
* If the host decides to proceed, it should send
* `ui/resource-teardown` (via {@link teardownResource `teardownResource`}) to allow
* the view to perform gracefull termination, then unmount the iframe after the view responds.
*
* @param callback - Handler that receives teardown request params
* - params - Empty object (reserved for future use)
*
* @example
* ```typescript
* bridge.onrequestteardown = async (params) => {
* console.log("App requested teardown");
* // Initiate teardown to allow the app to persist unsaved state
* // Alternatively, the callback can early return to prevent teardown
* await bridge.teardownResource({});
* // Now safe to unmount the iframe
* iframe.remove();
* };
* ```
*
* @see {@link McpUiRequestTeardownNotification `McpUiRequestTeardownNotification`} for the notification type
* @see {@link teardownResource `teardownResource`} for initiating teardown
*/
set onrequestteardown(
callback: (params: McpUiRequestTeardownNotification["params"]) => void,
) {
this.setNotificationHandler(
McpUiRequestTeardownNotificationSchema,
(request) => callback(request.params),
);
}
/**
* Register a handler for display mode change requests from the view.
*