/** * Type-checked code examples for the patterns documentation. * * These examples are included in {@link ./patterns.md} via `@includeCode` tags. * Each function's region markers define the code snippet that appears in the docs. * * @module */ import { App } from "../src/app.js"; import { applyDocumentTheme, applyHostFonts, applyHostStyleVariables, } from "../src/styles.js"; import type { McpUiHostContext } from "../src/types.js"; import { useApp, useHostStyles } from "../src/react/index.js"; /** * Example: Authenticated calls from App */ function authenticatedCalls(app: App) { //#region authenticatedCalls // TODO: Use tool calls / read resources // See PDF example to read binaries by chunks // Pass auth token in _meta + refresh token + store in local storage //#endregion authenticatedCalls } /** * Example: Giving errors back to model */ function errorsToModel(app: App) { //#region errorsToModel // Before app runs: validate inputs in tool call // After it runs: updateModelContext // TODO: Complete implementation //#endregion errorsToModel } /** * Example: Unified host styling (theme, CSS variables, fonts) */ function hostStylingVanillaJs(app: App) { //#region hostStylingVanillaJs function applyHostContext(ctx: McpUiHostContext) { if (ctx.theme) { applyDocumentTheme(ctx.theme); } if (ctx.styles?.variables) { applyHostStyleVariables(ctx.styles.variables); } if (ctx.styles?.css?.fonts) { applyHostFonts(ctx.styles.css.fonts); } } // Apply when host context changes app.onhostcontextchanged = applyHostContext; // Apply initial styles after connecting app.connect().then(() => { const ctx = app.getHostContext(); if (ctx) { applyHostContext(ctx); } }); //#endregion hostStylingVanillaJs } /** * Example: Host styling with React (CSS variables, theme, fonts) */ function hostStylingReact() { //#region hostStylingReact function MyApp() { const { app } = useApp({ appInfo: { name: "MyApp", version: "1.0.0" }, capabilities: {}, }); // Apply all host styles (variables, theme, fonts) useHostStyles(app, app?.getHostContext()); return (
Styled with host CSS variables and fonts
Uses [data-theme] selectors