Skip to main content
MCP Apps SDK

Overview

The App class supports two patterns for handling host notifications and requests:
  1. Setter properties (app.ontoolinput = ...) follow DOM-model replace semantics (like el.onclick). Assigning replaces any previous handler; assigning undefined clears it.
  2. addEventListener (app.addEventListener("toolinput", ...)) supports multiple listeners and cleanup via removeEventListener. Preferred when you need composable handlers.
Both patterns are convenience wrappers around setNotificationHandler() and setRequestHandler() from the MCP SDK’s Protocol class. For making requests from the View to the host, see Requests.
Register handlers before calling connect() to avoid missing notifications during the initialization handshake.

Notification Handlers

ontoolinput

Called when the host sends complete tool arguments. This arrives after a tool call begins and before the tool result.
Record<string, unknown>
Complete tool call arguments as key-value pairs.

ontoolinputpartial

Called as the host streams partial tool arguments during tool call initialization. Use for progressive rendering before complete input is available.
Record<string, unknown>
Partial tool arguments (healed JSON — incomplete objects may be truncated).
Partial arguments are “healed” JSON — the host closes unclosed brackets/braces to produce valid JSON. The last item in arrays or objects may be truncated. Use partial data only for preview UI, not for critical operations.

ontoolresult

Called when the host sends the result of a tool execution.
CallToolResult
Standard MCP tool execution result. Key fields:

ontoolcancelled

Called when tool execution is cancelled (user action, timeout, classifier intervention).
string
Optional reason for the cancellation.

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.
McpUiHostContext
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 declaring tools capability in the constructor. For the full picture of registering tools, see App Tools.
string
Name of the tool being called.
Record<string, unknown>
Tool arguments.

onlisttools

Return the list of tools this app provides. Requires declaring tools capability.

Use Method-keyed Handlers for Custom Messages

Use the typed setters and addEventListener() 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:
The three-argument form validates custom notification params before calling the handler. Custom requests use the same method-keyed shape with setRequestHandler(method, { params, result? }, handler).
Custom methods are outside the MCP Apps specification. A normal Host will not send or handle them, so use this API only when you own the Host integration. Do not replace built-in progress, cancellation, or ui/* handlers. See Update Custom Protocol Handlers when migrating 1.x code.
Register event handlers in the onAppCreated callback of useApp to ensure they’re set before the handshake. The sunpeak framework provides convenience hooks: useToolData, useTeardown, and useAppTools.