Skip to main content
MCP Apps SDK

Overview

useApp creates an App instance, connects it to the host via PostMessageTransport, and returns the connection state. Options are only used on the initial mount — the hook does not reconnect when options change.

Signature

UseAppOptions

{ name: string; version: string }
required
App identification sent to the host during ui/initialize.
McpUiAppCapabilities
default:"{}"
Features this app provides (e.g., { tools: { listChanged: true } }).
boolean
default:"true"
Report size changes to the host. See App options.
boolean
default:"false"
Throw when host-bound methods run before the initialization handshake completes.
boolean
default:"true"
Bridge app tools and WebMCP in both directions. See WebMCP interoperability.
(app: App) => void
Called after App is created but before connect(). Use this to register event handlers that must be in place before the initialization handshake.

AppState

App | null
The connected App instance, or null during initialization.
boolean
Whether initialization completed successfully.
Error | null
Connection error if initialization failed, null otherwise.

Usage

Behavior

  • Unmount — The hook closes its App when the component unmounts. In React Strict Mode’s double mount, and when Fast Refresh re-runs effects, the first App closes and a new one connects. The upstream JSDoc still says the App is not closed, but the 2.1 code closes it. sunpeak’s AppProvider keeps one App at module scope across both cases.
  • Auto-resize — The App is created with autoResize: true by default. Pass autoResize, strict, or webMcpBridge to change the App options. For anything else, create the App manually and use useAutoResize.
  • Options stability — Options are only read on initial mount. Changing appInfo, capabilities, onAppCreated, autoResize, strict, or webMcpBridge after mount has no effect.
The sunpeak framework provides its own useApp that returns just App | null via React context, with additional framework integration.