Overview
TheApp class provides a framework-agnostic way to build interactive MCP Apps that run inside host applications. It extends ProtocolWithEvents (which itself extends the MCP SDK’s Protocol class) and handles the connection lifecycle, initialization handshake, and bidirectional communication with the host. The ProtocolWithEvents base class adds DOM-style addEventListener/removeEventListener support for composable, multi-listener event handling alongside the traditional on* setter properties.
Constructor
Implementation
required
App identification:
{ name: string; version: string }.McpUiAppCapabilities
default:"{}"
Features this app provides. Set
tools to expose app-side tools to the host. Set availableDisplayModes to declare supported display modes.AppOptions
default:"{ autoResize: true }"
boolean
default:"true"
Automatically report size changes to the host using
ResizeObserver.boolean
default:"false"
Throw when host-bound methods run before the initialization handshake completes. The default
logs a warning. Throwing is planned to become the SDK default in a future release.
boolean
default:"false"
Allow schema parsing paths that require CSP
unsafe-eval. Keep this disabled for normal MCP
App Views.Strict initialization checks
Host-bound methods such ascallServerTool(), sendMessage(), and updateModelContext() must run after connect() completes. Calling them earlier can race the ui/initialize handshake and leave a strict Host waiting for a View that never becomes ready.
Use strict: true during development to turn this ordering mistake into an immediate error:
CSP-safe schema parsing
MCP App Views run under a restrictive Content Security Policy. By default, the SDK configures Zod’s parser to avoid generated functions that requireunsafe-eval.
Keep allowUnsafeEval at its default false unless you control the Host CSP and have a measured reason to enable the faster JIT path. Setting it to true does not change the Host CSP, so parsing will still fail if the Host blocks unsafe-eval.
connect()
Establishes connection with the host and performs the initialization handshake.- Connects the transport layer
- Sends
ui/initializewith app info and capabilities - Receives host capabilities and context
- Sends
ui/notifications/initialized(see Lifecycle) - Sets up auto-resize if enabled
new PostMessageTransport(window.parent, window.parent).
appInfo.version, see Protocol Versions and Compatibility.
Accessors
getHostCapabilities()
Returns the host’s capabilities from the initialization handshake.undefined before connection.
getHostVersion()
Returns the host’s name and version.undefined before connection.
getHostContext()
Returns the current host context (theme, locale, display mode, etc.). Automatically updated when the host sends context change notifications.PostMessageTransport
The transport layer for iframe-to-parent communication usingwindow.postMessage. In most cases you never need to use this directly — App.connect() creates one automatically when no transport is provided.
Auto-Resize
By default,App monitors document.body and document.documentElement for size changes and sends ui/notifications/size-changed to the host. Disable with { autoResize: false }. In React, you can use the useAutoResize hook instead.
For the full host sizing contract, including fixed vs flexible containerDimensions and display mode changes, see Layout and Display.
setupSizeChangedNotifications()
Manually start auto-resize reporting. Returns a cleanup function. Automatically called byconnect() when autoResize: true.