Skip to main content
MCP Apps SDK

Overview

onteardown is a request handler called when the host is about to unmount the iframe and wants the app to shut down gracefully. This is your opportunity to save unsaved state, close open connections, flush analytics, or perform any other cleanup before the View is destroyed. Because this is a request (not a notification), the host waits for your response before proceeding. Return {} (or a promise resolving to {}) to signal that cleanup is complete. If the handler throws or the promise rejects, the host may force-close the iframe.

Signature

Parameters

This handler receives no parameters.

Returns

Return {} or a Promise<{}> to indicate that shutdown is complete.
Register handlers before calling connect() to avoid missing notifications during the initialization handshake.

Usage

Save state before shutdown

Flush pending work

Synchronous cleanup

If your cleanup is synchronous, you can return {} directly:
The host may impose a timeout on the teardown request. Avoid long-running operations — save critical state quickly and let non-critical work fail gracefully.