{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "host",
  "type": "registry:lib",
  "title": "Host helpers",
  "description": "Apply host theme, CSS variables, fonts, display mode and safe-area insets, with dev-only guideline warnings.",
  "files": [
    {
      "path": "registry/mcpblocks/lib/host.ts",
      "content": "/**\n * Small helpers for wiring mcpblocks components to an MCP Apps host.\n *\n * The host sends a `hostContext` during `ui/initialize` and on change.\n * Pass it here to apply host styles and expose safe-area insets as CSS\n * variables that the components read (`--mb-safe-top` … `--mb-safe-left`).\n */\n\nexport type SafeAreaInsets = {\n  top?: number\n  right?: number\n  bottom?: number\n  left?: number\n}\n\nexport type HostContextLike = {\n  theme?: \"light\" | \"dark\"\n  displayMode?: \"inline\" | \"fullscreen\" | \"pip\"\n  safeAreaInsets?: SafeAreaInsets\n  styles?: {\n    variables?: Record<string, string | undefined>\n    css?: { fonts?: string }\n  }\n}\n\n/** Writes safe-area insets to CSS variables on `el` (defaults to <html>). */\nexport function applySafeAreaInsets(\n  insets: SafeAreaInsets | undefined,\n  el: HTMLElement = document.documentElement\n) {\n  const { top = 0, right = 0, bottom = 0, left = 0 } = insets ?? {}\n  el.style.setProperty(\"--mb-safe-top\", `${top}px`)\n  el.style.setProperty(\"--mb-safe-right\", `${right}px`)\n  el.style.setProperty(\"--mb-safe-bottom\", `${bottom}px`)\n  el.style.setProperty(\"--mb-safe-left\", `${left}px`)\n}\n\nconst hostVariableName = /^--[\\w-]+$/\nconst hostFontsId = \"mcpblocks-host-fonts\"\n\nfunction applyHostFonts(fonts: string | undefined, doc: Document) {\n  if (!fonts?.trim()) return\n\n  const existing = doc.getElementById(hostFontsId)\n  if (existing) {\n    if (existing.textContent !== fonts) existing.textContent = fonts\n    return\n  }\n\n  const style = doc.createElement(\"style\")\n  style.id = hostFontsId\n  style.textContent = fonts\n  doc.head.appendChild(style)\n}\n\n/** Applies the host's theme, display mode, safe areas, style variables, and fonts. */\nexport function applyHostContext(\n  ctx: HostContextLike | undefined,\n  el: HTMLElement = document.documentElement\n) {\n  if (!ctx) return\n  if (ctx.theme) {\n    el.classList.toggle(\"dark\", ctx.theme === \"dark\")\n    el.style.colorScheme = ctx.theme\n  }\n  if (ctx.displayMode) el.dataset.displayMode = ctx.displayMode\n  applySafeAreaInsets(ctx.safeAreaInsets, el)\n\n  let hasHostVariables = false\n  for (const [name, value] of Object.entries(ctx.styles?.variables ?? {})) {\n    if (!hostVariableName.test(name) || typeof value !== \"string\" || !value.trim()) continue\n    el.style.setProperty(name, value)\n    hasHostVariables = true\n  }\n  if (hasHostVariables) el.dataset.hostStyles = \"\"\n  applyHostFonts(ctx.styles?.css?.fonts, el.ownerDocument)\n}\n\ntype ModelContextSummary = { content: { type: \"text\"; text: string }[] }\n\ntype ModelContextApp = {\n  getHostCapabilities(): { updateModelContext?: { text?: object }; message?: { text?: object } } | undefined\n  updateModelContext(params: ModelContextSummary): Promise<unknown>\n  sendMessage(params: ModelContextSummary & { role: \"user\" }): Promise<{ isError?: boolean }>\n}\n\n/** Reports a short summary of a completed change the user already saw. */\nexport async function tellModel(\n  app: ModelContextApp,\n  summary: string,\n  { fallbackToMessage = false }: { fallbackToMessage?: boolean } = {},\n): Promise<boolean> {\n  const characters = Array.from(summary.trim())\n  if (characters.length === 0) return false\n\n  const text = characters.length > 500 ? `${characters.slice(0, 499).join(\"\")}…` : characters.join(\"\")\n  const result: ModelContextSummary = { content: [{ type: \"text\", text }] }\n  const capabilities = app.getHostCapabilities()\n\n  try {\n    if (capabilities?.updateModelContext?.text) {\n      await app.updateModelContext(result)\n      return true\n    }\n    if (fallbackToMessage && (!capabilities?.message || capabilities.message.text)) {\n      return !(await app.sendMessage({ role: \"user\", ...result })).isError\n    }\n  } catch {\n    // The write already completed. A transport error here cannot undo it.\n  }\n  return false\n}\n\n// Bundlers (Vite, Next, webpack) replace `process.env.NODE_ENV` at build time. The\n// declaration keeps this file compiling in projects without @types/node.\ndeclare const process: { env: { NODE_ENV?: string } }\n\nfunction isProduction() {\n  try {\n    return process.env.NODE_ENV === \"production\"\n  } catch {\n    // No bundler replacement and no `process` global: behave like development.\n    return false\n  }\n}\n\n/** Logs a development-only warning when a guideline limit is exceeded. */\nexport function guideline(condition: boolean, message: string) {\n  if (condition && !isProduction()) {\n    console.warn(`[mcpblocks] ${message}`)\n  }\n}\n",
      "type": "registry:lib"
    }
  ]
}