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 } }).(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
- HMR persistence — The
Appinstance persists across React Fast Refresh, so code changes don’t trigger reconnection. A full page reload establishes a fresh connection. - Strict Mode — The
Appis intentionally not closed on unmount to avoid cleanup issues during React Strict Mode’s double-mount cycle. Callapp.close()manually if needed. - Auto-resize — The
Appis created withautoResize: trueby default. For custom options, create theAppmanually and useuseAutoResize. - Options stability — Options are only read on initial mount. Changing
appInfo,capabilities, oronAppCreatedafter mount has no effect.