Overview
TheApp class supports two patterns for handling host notifications and requests:
- Setter properties (
app.ontoolinput = ...) follow DOM-model replace semantics (likeel.onclick). Assigning replaces any previous handler; assigningundefinedclears it. - addEventListener (
app.addEventListener("toolinput", ...)) supports multiple listeners and cleanup viaremoveEventListener. Preferred when you need composable handlers.
setNotificationHandler() and setRequestHandler() from the MCP SDK’s Protocol class. For making requests from the View to the host, see Requests.
Notification Handlers
ontoolinput
Called when the host sends complete tool arguments. This arrives after a tool call begins and before the tool result.ontoolinputpartial
Called as the host streams partial tool arguments during tool call initialization. Use for progressive rendering before complete input is available.ontoolresult
Called when the host sends the result of a tool execution.ontoolcancelled
Called when tool execution is cancelled (user action, timeout, classifier intervention).onhostcontextchanged
Called when the host context changes (theme toggle, resize, locale change, etc.). Params are automatically merged into the internal context before the callback —getHostContext() returns updated values.
Partial context update containing only changed fields. See Core Types for the full
McpUiHostContext structure.Request Handlers
onteardown
Called when the host requests graceful shutdown before unmounting the iframe. Return{} (or a promise resolving to {}) when ready.
oncalltool
Handle tool calls from the host directed at this app. Requires declaringtools capability in the constructor. For the full picture of registering tools, see App Tools.
onlisttools
Return the list of tools this app provides. Requires declaringtools capability.
Use Method-keyed Handlers for Custom Messages
Use the typed setters andaddEventListener() for standard MCP App events. If you control both the View and Host and need a custom protocol message, SDK 2.0 keys the underlying handlers by method name:
setRequestHandler(method, { params, result? }, handler).