Skip to main content

createApp

createApp<Tools extends ToolMap>() from @toolgrain/react returns AppProvider, useToolResult, useToolCall, useHostContext, and useHostActions. It does not read browser globals or connect until mounting.
This reference describes the stable Toolgrain 0.3.0 API. Start with the Vite walkthrough for a runnable project, then add only the hooks your widget uses.

AppProvider

A connection is terminal after closure. Remount to reconnect or change identity/routing. Unmount and teardown remove owned resources. Server rendering does not connect.

Hooks

useToolResult(name?) returns status, selected tool name, optional original wire input, full result, and lifecycle/routing error. Without a name, the selected name discriminates the registered tool union. With a name, other active tools appear as waiting. useToolCall(name) returns call(input, options?), pending, optional full result, and optional error. Options accept AbortSignal and timeoutMs. Explicit tool errors resolve; RPC/transport failures reject. Each hook tracks its latest invocation independently of the initiating result. useHostContext() returns live AppHostContext. Fields are optional and self-reported by the host. useHostActions() returns available and stable request functions: Request options accept signal and timeoutMs; the provider default is 30000 milliseconds. Capability/content checks happen before sending. Explicit isError acknowledgements resolve; RPC/transport/validation failures reject. The hook owns no pending/error state. See host actions for availability, replacement context, and actual display-mode behavior. Exported authoring types: AppProviderProps, AppState, ToolState, ToolName, ToolInput, ToolResult, ToolCall, and ToolCallOptions. Host-action types are HostActions, HostActionAvailability, and HostActionOptions.

Server and wire contracts

getAppSupport(ctx) and AppSupport are exported by @toolgrain/mcp. Existing mcpResult() types preserve inferred metadata through an optional second generic parameter; existing single-generic usage remains valid. @toolgrain/protocol/apps exports APPS_PROTOCOL_VERSION, APPS_EXTENSION_ID, APPS_MIME_TYPE, AppToolMeta, AppResourceMeta, AppHostContext, AppHostCapabilities, AppDisplayMode, AppToolResult, AppInitializeParams, and AppInitializeResult. This export has no runtime dependencies and does not replace either supported outer MCP revision. Additional named exports describe resource settings, host presentation, and host-action envelopes: AppHostCapabilities now names sandbox grants/CSP, experimental extensions, content-message/context modalities, download support, and sampling tools, alongside existing server tool/resource, link, and logging advertisements. Unknown capability fields are preserved. The support table distinguishes implemented requests from other advertisements. Styling variables are optional. Use CSS fallbacks such as var(--color-text-primary, #111) and var(--font-sans, sans-serif). The provider applies supplied variables and font CSS by default; applyHostStyles={false} disables that behavior. The metadata guide includes resource fields and border examples. AppToolMeta rejects resource-only CSP/permissions, including when metadata is assigned through another variable. The runtime still preserves supplied metadata; the host owns enforcement. apps-provenance.json pins the reviewed stable specification and upstream type reference, including their SHA-256 hashes. Some type-reference advertisements go beyond the stable prose; their presence alone does not implement a React action.