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,EAAI,IAAI,CAAA,CAAA;AAAA,IAC3B,MAAA,EAAQ,IAAA;AAAA,IACR,KAAA,EAAO,MACL,IAAI,OAAA,CAAc,CAAAA,QAAAA,KAAW;AAC3B,MAAA,IAAA,CAAK,KAAA,CAAM,MAAMA,QAAAA,EAAS,CAAA;AAC1B,MAAA,IAAA,CAAK,mBAAA,EAAoB;AAAA,IAC3B,CAAC;AAAA,GACL;AACF","file":"app-mcp-server.mjs","sourcesContent":["/**\n * Dynamic MCP-app panels for installed `@agentproto/app` apps that ship\n * a `ui` block (`defineApp({ ui: { html, title?, tools?, csp? } })`,\n * `app-tools.ts`'s `performInstall`). Bridges `AppRegistry` → `AgnoMcpApp[]`\n * so `registerMcpApps` (mcp-apps-adapter.ts) can mount them next to the\n * built-in panels (agentproto_sessions, agents_overview, ...).\n *\n * `mcpServerFactory` (index.ts) rebuilds a fresh McpServer per `/mcp`\n * request, so this module never throws — a bad app record must not take\n * down every other request. A collided tool id is still skipped with a\n * `console.warn` (there's nowhere else for a second panel to live). Every\n * OTHER failure — a removed app dir, a missing/unreadable `ui.path`, a\n * failed or in-flight `ui.build` — still mounts the panel, serving a\n * readable HTML placeholder/error page in place of the real bundle\n * (app-ui-placeholder.ts) instead of skipping it outright: a `ui://`\n * resource that silently doesn't exist just shows as a JSON-RPC \"resource\n * not found\" in a host, which is what motivated this file's placeholder\n * page in the first place — a HOST-VISIBLE page, even a stand-in one, beats\n * a resource the host has to explain away as missing.\n */\n\nimport { existsSync } from \"node:fs\"\nimport { readFile, stat } from \"node:fs/promises\"\nimport { z } from \"zod\"\nimport { RUNNER_SELECT_SCRIPT } from \"@agentproto/app-client/runner-select\"\nimport { DISPLAY_MODE_SCRIPT } from \"@agentproto/app-client/display-mode\"\nimport type { AppRegistry } from \"./app-registry.js\"\nimport { resolveAppUiBuildState } from \"./app-ui-build.js\"\nimport { renderAppUiBuildingHtml, renderAppUiErrorHtml } from \"./app-ui-placeholder.js\"\nimport type { AgnoMcpApp } from \"@agentproto/apps\"\n\n/** Derive the MCP tool id for an installed app's UI panel — strips the\n *  `@owner/` scope (if any) and maps every non `[a-z0-9]` character to `_`. */\nexport function appUiToolId(appId: string): string {\n  const slug = appId.replace(/^@[^/]+\\//, \"\").replace(/[^a-z0-9]/g, \"_\")\n  return `app_ui_${slug}`\n}\n\ninterface UiHtmlCache {\n  /** Bridge-injected html for `path`, with `baseUrl`'s public-base script\n   *  applied when given. The same string is returned to every caller that\n   *  asks for the same (path, stamp, baseUrl). */\n  get(path: string, version: string, baseUrl?: string): Promise<string>\n}\n\n/**\n * The MCP Apps wire (spec `2026-01-26`) over `postMessage` JSON-RPC, exposed\n * as `window.McpApp.connect() -> Promise<{ callTool, sendMessage,\n * updateModelContext, openLink, onTeardown }>`. No host in this stack injects\n * an SDK global into the app iframe, so every UI panel that talks through\n * `window.McpApp` (all of them — see each app's `ui.ts` under\n * `packages/apps/src`) would otherwise throw `Cannot read properties of\n * undefined (reading 'connect')` at boot. Injected here, at serve time, so\n * every installed app's panel is covered without each app shipping its own\n * copy of the bridge.\n *\n * Bridge surface:\n * - `callTool(name, args)` — JSON-RPC `tools/call`\n * - `sendMessage(content)` — JSON-RPC `ui/message`\n * - `updateModelContext({content?, structuredContent?})` — JSON-RPC\n *   `ui/update-model-context`; each call replaces the previous context.\n * - `openLink(url)` — JSON-RPC `ui/open-link` {url}\n * - `onTeardown(cb)` — register a cleanup callback invoked on\n *   `ui/resource-teardown` from the host, at which point the bridge also\n *   replies `{result:{}}` automatically.\n * - `getHostContext()` / `onHostContext(cb)` — the host context captured\n *   from the `ui/initialize` result and merged on every\n *   `ui/notifications/host-context-changed` (the notification carries only\n *   the changed keys, per the ext-apps spec).\n * - `getToolInput()` / `onToolInput(cb)` — the arguments the panel's own tool\n *   (`app_ui_<slug>`) was invoked with, from `ui/notifications/tool-input`,\n *   falling back to the tool's own result (`ui/notifications/tool-result`,\n *   which `registerMcpApps` fills with `execute()`'s return value) for a host\n *   that forwards results but not inputs. A late subscriber is replayed the\n *   last value, same as `onHostContext`. Lets an MCP-hosted panel resolve a\n *   deep link (e.g. `{view: \"wallets/my-profile\"}`) the way the browser-served\n *   panel resolves `location.hash`.\n * - `displayMode` — `{get, available, request, onChange, mountToggle}`, the\n *   shared controller from `@agentproto/app-client/display-mode` (also\n *   published as `window.McpApp.displayMode` once connected). It is what\n *   mounts the floating ⤢ toggle; an app that wants the control in its own\n *   header opts the floating one out (`<meta name=\"agentproto-display-toggle\"\n *   content=\"none\">`, or `connect({displayToggle: \"none\"})`) and calls\n *   `displayMode.mountToggle(el)` instead.\n */\nexport const MCP_APP_BRIDGE_SCRIPT = `<script>\n(function () {\n  window.__AGENTPROTO_UI_TRANSPORT__ = \"mcp\";\n  if (window.McpApp) return;\n  var nextId = 1, pending = {}, teardownCbs = [];\n  var hostContext = null, hostContextCbs = [];\n  var toolInput = null, toolInputCbs = [];\n  function post(m) { window.parent.postMessage(m, \"*\"); }\n  function getHostContext() { return hostContext; }\n  function onHostContext(cb) {\n    hostContextCbs.push(cb);\n    // Replay the last context so a late subscriber isn't stuck blind.\n    if (hostContext) { try { cb(hostContext); } catch (_) {} }\n  }\n  function setHostContext(ctx) {\n    if (!ctx || typeof ctx !== \"object\") return;\n    // host-context-changed carries only the changed keys — merge, matching\n    // the official ext-apps App behaviour.\n    hostContext = Object.assign({}, hostContext || {}, ctx);\n    for (var i = 0; i < hostContextCbs.length; i++) {\n      try { hostContextCbs[i](hostContext); } catch (_) {}\n    }\n  }\n  function getToolInput() { return toolInput; }\n  function onToolInput(cb) {\n    toolInputCbs.push(cb);\n    // Replay the last known input so a late subscriber isn't stuck blind —\n    // same convention as onHostContext.\n    if (toolInput) { try { cb(toolInput); } catch (_) {} }\n  }\n  function setToolInput(input) {\n    if (!input || typeof input !== \"object\") return;\n    toolInput = input;\n    for (var i = 0; i < toolInputCbs.length; i++) {\n      try { toolInputCbs[i](toolInput); } catch (_) {}\n    }\n  }\n  // A ui/notifications/tool-result carries the full CallToolResult of the\n  // tool call that opened this view (spec 2026-01-26) — the same envelope\n  // registerMcpApps wraps execute()'s return value in (JSON text content, or\n  // structuredContent). Read as a fallback source of the tool's arguments\n  // for a host that forwards results but not tool-input.\n  function extractToolResultInput(result) {\n    try {\n      if (result && result.structuredContent && typeof result.structuredContent === \"object\") {\n        return result.structuredContent;\n      }\n      var c = result && Array.isArray(result.content) ? result.content[0] : null;\n      if (c && c.type === \"text\" && typeof c.text === \"string\") return JSON.parse(c.text);\n    } catch (_) {}\n    return null;\n  }\n  window.addEventListener(\"message\", function (e) {\n    if (e.source !== window.parent) return;\n    var m = e.data;\n    if (typeof m === \"string\") { try { m = JSON.parse(m); } catch (_) { return; } }\n    if (!m || m.jsonrpc !== \"2.0\") return;\n    if (m.method === \"ui/resource-teardown\") {\n      for (var i = 0; i < teardownCbs.length; i++) teardownCbs[i]();\n      post({ jsonrpc: \"2.0\", id: m.id, result: {} });\n      return;\n    }\n    if (m.method === \"ui/notifications/host-context-changed\") { setHostContext(m.params || {}); return; }\n    if (m.method === \"ui/notifications/tool-input\") { setToolInput((m.params || {}).arguments); return; }\n    if (m.method === \"ui/notifications/tool-result\") { setToolInput(extractToolResultInput(m.params)); return; }\n    if (m.id != null && pending[m.id]) { var cb = pending[m.id]; delete pending[m.id]; cb(m.result, m.error); }\n  });\n  function request(method, params) {\n    return new Promise(function (resolve, reject) {\n      var id = nextId++;\n      pending[id] = function (r, err) { if (err) reject(new Error((err && err.message) || String(err))); else resolve(r); };\n      post({ jsonrpc: \"2.0\", id: id, method: method, params: params });\n    });\n  }\n  function requestDisplayMode(mode) { return request(\"ui/request-display-mode\", { mode: mode }); }\n  // The shared toggle (@agentproto/app-client/display-mode), injected right\n  // after this script. Absent only when the app ships its own\n  // window.AgentprotoUI namespace, which suppresses that injection — the\n  // fallback keeps window.McpApp.displayMode callable either way.\n  function installDisplayMode(opts) {\n    var api = { getHostContext: getHostContext, onHostContext: onHostContext, requestDisplayMode: requestDisplayMode };\n    if (window.AgentprotoUI && window.AgentprotoUI.installDisplayMode) {\n      return window.AgentprotoUI.installDisplayMode(api, opts);\n    }\n    return {\n      get: function () { return (hostContext && hostContext.displayMode) || \"inline\"; },\n      available: function () { return (hostContext && hostContext.availableDisplayModes) || []; },\n      request: requestDisplayMode,\n      onChange: function (cb) { onHostContext(function (c) { cb((c && c.displayMode) || \"inline\", c); }); return function () {}; },\n      mountToggle: function () { return null; }\n    };\n  }\n  window.McpApp = {\n    connect: function (opts) {\n      if (window.parent === window) return Promise.reject(new Error(\"no host (standalone)\"));\n      return request(\"ui/initialize\", {\n        appInfo: { name: \"agentproto-app\", version: \"1.0\" },\n        appCapabilities: { availableDisplayModes: [\"inline\", \"fullscreen\"] },\n        protocolVersion: \"2026-01-26\"\n      }).then(function (result) {\n        // The initialize result carries the initial hostContext (displayMode,\n        // availableDisplayModes, theme, safeAreaInsets) — capture it before\n        // notifying the host, so the toggle renders in the right place on\n        // its first paint rather than after a host-context-changed.\n        if (result && result.hostContext) setHostContext(result.hostContext);\n        post({ jsonrpc: \"2.0\", method: \"ui/notifications/initialized\", params: {} });\n        var displayMode = installDisplayMode((opts && opts.displayToggle) ? { toggle: opts.displayToggle } : null);\n        window.McpApp.displayMode = displayMode;\n        return {\n          callTool: function (name, args) { return request(\"tools/call\", { name: name, arguments: args || {} }); },\n          sendMessage: function (p) {\n            var params = typeof p === \"string\" ? { content: p } : (p || {});\n            var content = typeof params.content === \"string\" ? [{ type: \"text\", text: params.content }] : (params.content || []);\n            return request(\"ui/message\", { role: \"user\", content: content });\n          },\n          updateModelContext: function (p) { return request(\"ui/update-model-context\", p || {}); },\n          openLink: function (url) { return request(\"ui/open-link\", { url: url }); },\n          onTeardown: function (cb) { teardownCbs.push(cb); },\n          getHostContext: getHostContext,\n          onHostContext: onHostContext,\n          getToolInput: getToolInput,\n          onToolInput: onToolInput,\n          displayMode: displayMode\n        };\n      });\n    }\n  };\n})();\n</script>\n`\n\n/** Insert `script` right after the earliest structural opening tag the\n *  document has (`<head>`, else `<body>`, else `<html>`, else prepend), so\n *  the injected bridge is defined before any app script runs. */\nfunction injectAfterStructuralTag(html: string, script: string): string {\n  for (const tag of [\"head\", \"body\", \"html\"]) {\n    const match = html.match(new RegExp(`<${tag}[^>]*>`, \"i\"))\n    if (match?.index !== undefined) {\n      const insertAt = match.index + match[0].length\n      return html.slice(0, insertAt) + script + html.slice(insertAt)\n    }\n  }\n  return script + html\n}\n\n/** `window.__AGENTPROTO_BASEURL__` assignment for the public base this page was\n *  reached at (origin plus any `/d/<id>` prefix), so an app builds absolute\n *  links against it instead of `location` or 127.0.0.1. `<` is escaped so the\n *  value cannot close the script. */\nfunction publicBaseScript(baseUrl: string): string {\n  return `<script>window.__AGENTPROTO_BASEURL__=${JSON.stringify(baseUrl).replace(/</g, \"\\\\u003c\")};</script>`\n}\n\n/** Apply the public base to already bridge-injected html (the cached form);\n *  a no-op when this exact assignment is already present. */\nexport function injectPublicBaseUrl(html: string, baseUrl: string): string {\n  const script = publicBaseScript(baseUrl)\n  return html.includes(script) ? html : injectAfterStructuralTag(html, script)\n}\n\n/** Inject `MCP_APP_BRIDGE_SCRIPT` followed by the two\n *  `window.AgentprotoUI` scripts (`DISPLAY_MODE_SCRIPT`, then\n *  `RUNNER_SELECT_SCRIPT`) — every installed app UI gets the shared\n *  display-mode toggle and `mountRunnerSelect` for free, same as it gets\n *  `window.McpApp` (see `injectAfterStructuralTag` for the placement rule).\n *  The halves are independently idempotent, so a single\n *  `injectAfterStructuralTag` call inserts only whichever ones the document\n *  doesn't already define — inserting them one call each would place the\n *  second BEFORE the first (all target \"right after the same structural\n *  tag\"), which is why they're concatenated first instead. `window.\n *  AgentprotoUI` gates BOTH of its scripts as one unit: a document that\n *  already defines that namespace brings its own, and the bridge's\n *  `installDisplayMode` falls back to a controller with no button rather\n *  than fighting it. The bridge guard matches an assignment only — every\n *  app panel CONSUMES `window.McpApp.connect()`, so matching any mention\n *  would skip injection for exactly the documents that need it, leaving\n *  `window.McpApp` undefined in the host iframe. */\nexport function injectMcpAppBridge(html: string, baseUrl?: string): string {\n  const hasBridge = /window\\.McpApp\\s*=/.test(html)\n  const hasUiNamespace = /window\\.AgentprotoUI\\s*=/.test(html)\n  const baseScript = baseUrl && !html.includes(publicBaseScript(baseUrl)) ? publicBaseScript(baseUrl) : \"\"\n  if (hasBridge && hasUiNamespace && !baseScript) return html\n  const script =\n    baseScript +\n    (hasBridge ? \"\" : MCP_APP_BRIDGE_SCRIPT) +\n    (hasUiNamespace ? \"\" : DISPLAY_MODE_SCRIPT + RUNNER_SELECT_SCRIPT)\n  return injectAfterStructuralTag(html, script)\n}\n\n/**\n * The standalone-browser-tab twin of `MCP_APP_BRIDGE_SCRIPT` — same\n * `window.McpApp.connect() -> Promise<{ callTool, sendMessage,\n * updateModelContext, openLink, onTeardown }>` surface, but `callTool` POSTs\n * to the sibling `./tool-call` route (http-server.ts's `POST\n * /apps/:appId/tool-call`) instead of JSON-RPC over `postMessage`, so\n * `GET /apps/:appId/ui` renders a working app with no host iframe at all.\n * `callTool` resolves with the route's body — the same MCP result envelope a\n * postMessage host's `tools/call` reply carries — so an app's unwrapping code\n * (see media-viewer's `unwrapText`) behaves identically in both modes. There\n * is no chat host to receive `ui/message`, so `sendMessage` rejects; there is\n * no context host either, so `updateModelContext` rejects; `openLink` opens a\n * new tab via `window.open`; `onTeardown` is a no-op.\n *\n * `displayMode` is the same shared controller the postMessage bridge builds\n * (`@agentproto/app-client/display-mode`), wired to a host context of\n * `{displayMode:\"inline\", availableDisplayModes:[]}` — an empty list is what\n * keeps the toggle hidden in a plain browser tab, where there is no host to\n * grant fullscreen, while leaving the API callable so an app doesn't have to\n * branch on which bridge it got.\n *\n * Injected INSTEAD OF (not on top of) the postMessage bridge: that bridge's\n * `connect()` rejects when `window.parent === window`, which is exactly the\n * standalone case this one exists for.\n */\nexport const STANDALONE_REST_BRIDGE_SCRIPT = `<script>\n(function () {\n  window.__AGENTPROTO_UI_TRANSPORT__ = \"http\";\n  if (window.McpApp) return;\n  // No host in a standalone tab: inline, nothing else on offer. Fed to the\n  // shared display-mode controller so its toggle stays hidden here.\n  var hostContext = { displayMode: \"inline\", availableDisplayModes: [] };\n  function installDisplayMode(opts) {\n    var api = {\n      getHostContext: function () { return hostContext; },\n      onHostContext: function (cb) { try { cb(hostContext); } catch (_) {} },\n      requestDisplayMode: function () {\n        return Promise.reject(new Error(\"requestDisplayMode: no host (standalone mode)\"));\n      }\n    };\n    if (window.AgentprotoUI && window.AgentprotoUI.installDisplayMode) {\n      return window.AgentprotoUI.installDisplayMode(api, opts);\n    }\n    return {\n      get: function () { return \"inline\"; },\n      available: function () { return []; },\n      request: api.requestDisplayMode,\n      onChange: function (cb) { try { cb(\"inline\", hostContext); } catch (_) {} return function () {}; },\n      mountToggle: function () { return null; }\n    };\n  }\n  window.McpApp = {\n    connect: function (opts) {\n      var displayMode = installDisplayMode((opts && opts.displayToggle) ? { toggle: opts.displayToggle } : null);\n      window.McpApp.displayMode = displayMode;\n      return Promise.resolve({\n        callTool: function (name, args) {\n          return fetch(\"./tool-call\", {\n            method: \"POST\",\n            headers: { \"content-type\": \"application/json\" },\n            body: JSON.stringify({ tool: name, args: args || {} })\n          }).then(function (res) {\n            return res.json().catch(function () {\n              throw new Error(\"tool-call failed: HTTP \" + res.status);\n            }).then(function (body) {\n              if (!res.ok) throw new Error((body && (body.message || body.error)) || (\"tool-call failed: HTTP \" + res.status));\n              return body;\n            });\n          });\n        },\n        sendMessage: function () {\n          return Promise.reject(new Error(\"sendMessage: no chat host (standalone mode)\"));\n        },\n        updateModelContext: function () {\n          return Promise.reject(new Error(\"updateModelContext: no host (standalone mode)\"));\n        },\n        openLink: function (url) {\n          window.open(url, \"_blank\");\n          return Promise.resolve({});\n        },\n        onTeardown: function () {},\n        getHostContext: function () { return hostContext; },\n        onHostContext: function (cb) { try { cb(hostContext); } catch (_) {} },\n        // No host to notify tool input in standalone mode — a browser tab\n        // navigates via location.hash instead (see @agentproto/config's ui.ts).\n        getToolInput: function () { return null; },\n        onToolInput: function () {},\n        displayMode: displayMode\n      });\n    }\n  };\n})();\n</script>\n`\n\n/** Inject the standalone REST bridge followed by (idempotently)\n *  `DISPLAY_MODE_SCRIPT` + `RUNNER_SELECT_SCRIPT` — same pairing\n *  `injectMcpAppBridge` does, gated as one unit on the same\n *  `window.AgentprotoUI` guard, in one\n *  `injectAfterStructuralTag` call for the same reason (see its doc: two\n *  separate calls targeting the same tag would place the second script\n *  BEFORE the first). Meant for raw `ui.path` html — the bridge script\n *  itself is always (re-)injected, unguarded at the html level, same as\n *  before this change; the script's own `if (window.McpApp) return;` guard\n *  is the runtime-level idempotency, not this function. */\nexport function injectStandaloneAppBridge(\n  html: string,\n  baseUrl?: string,\n): string {\n  const hasUiNamespace = /window\\.AgentprotoUI\\s*=/.test(html)\n  const baseUrlScript = baseUrl ? publicBaseScript(baseUrl) : \"\"\n  const script =\n    baseUrlScript +\n    STANDALONE_REST_BRIDGE_SCRIPT +\n    (hasUiNamespace ? \"\" : DISPLAY_MODE_SCRIPT + RUNNER_SELECT_SCRIPT)\n  return injectAfterStructuralTag(html, script)\n}\n\n/** Per-path HTML cache keyed on the file's own identity — a request rebuilds\n *  the McpServer every time, but re-reading an installed app's `ui.path` off\n *  disk on every `/mcp` call would be wasteful when nothing changed. The\n *  cache lives at gateway scope (created once in index.ts, outside\n *  `mcpServerFactory`) so it actually persists across requests.\n *\n *  The `version` argument (the app's `updatedAt`) alone is NOT enough: it\n *  only changes on `app_install`, not when the committed `ui/index.html`\n *  bundle is rebuilt in place (e.g. a merged PR) — keying on it alone served\n *  stale html until the app was reinstalled or the daemon restarted. Each\n *  `get` additionally `stat`s the file and folds `mtimeMs:size` into the\n *  cache key, so an in-place rewrite invalidates immediately with no\n *  reinstall needed; `version` stays in the key too since a stat is already\n *  paid for and it still catches the reinstall case for free. A stat per\n *  request is cheap compared to the read + bridge injection it usually\n *  saves. The cached value is post-bridge-injection, so injection only runs\n *  once per (path, stamp) rather than on every served request.\n *\n *  The public-base variant is cached too, per (path, stamp, baseUrl): every\n *  McpServer built for the same base gets the SAME string. Injecting it per\n *  build gave each live server a private copy of every installed panel\n *  (~110 MB of duplicated html measured on a daemon with 17 servers). The\n *  bases a daemon is reached at are few (loopback, a tunnel, a relay\n *  prefix), but each `/d/<id>` relay prefix is its own base, so the variants\n *  per file are capped (`maxBaseVariants`, least recently used out). A\n *  concurrent miss shares one in-flight read instead of reading per caller. */\nexport function createUiHtmlCache(\n  opts: { injectBase?: (html: string, baseUrl: string) => string; maxBaseVariants?: number } = {},\n): UiHtmlCache {\n  const injectBase = opts.injectBase ?? injectPublicBaseUrl\n  const maxBaseVariants = Math.max(1, opts.maxBaseVariants ?? 16)\n  const cache = new Map<string, { stamp: string; html: Promise<string>; variants: Map<string, string> }>()\n  return {\n    async get(path, version, baseUrl) {\n      const st = await stat(path)\n      const stamp = `${version}:${st.mtimeMs}:${st.size}`\n      let entry = cache.get(path)\n      if (!entry || entry.stamp !== stamp) {\n        const html = readFile(path, \"utf8\").then(raw => injectMcpAppBridge(raw))\n        const fresh = { stamp, html, variants: new Map<string, string>() }\n        entry = fresh\n        cache.set(path, fresh)\n        // A failed read must not stick: the next request retries it.\n        html.catch(() => {\n          if (cache.get(path) === fresh) cache.delete(path)\n        })\n      }\n      const html = await entry.html\n      if (!baseUrl) return html\n      const hit = entry.variants.get(baseUrl)\n      if (hit !== undefined) {\n        entry.variants.delete(baseUrl)\n        entry.variants.set(baseUrl, hit)\n        return hit\n      }\n      const withBase = injectBase(html, baseUrl)\n      entry.variants.set(baseUrl, withBase)\n      if (entry.variants.size > maxBaseVariants) {\n        const oldest = entry.variants.keys().next().value\n        if (oldest !== undefined) entry.variants.delete(oldest)\n      }\n      return withBase\n    },\n  }\n}\n\n/**\n * Build one `AgnoMcpApp` per installed app that has a `ui` block. The only\n * case still SKIPPED (with a `console.warn`) is a derived tool id colliding\n * with `existingToolNames` (built-in panels) or an earlier installed app in\n * this same pass — there's nowhere else for a second panel with the same id\n * to live. Every other failure (removed app dir, missing/unreadable\n * `ui.path`, a failed or still-running `ui.build`) still mounts the panel,\n * with its `html` swapped for a placeholder/error page — see the module\n * doc's rationale and app-ui-placeholder.ts.\n */\nexport async function makeInstalledAppUiApps(\n  appRegistry: AppRegistry,\n  cache: UiHtmlCache,\n  existingToolNames: Set<string>,\n  /** Public base of the request building this server (see `resolvePublicBase`);\n   *  baked into each app's html. Absent: the html is served without one. */\n  baseUrl?: string,\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n): Promise<AgnoMcpApp<any, any>[]> {\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  const apps: AgnoMcpApp<any, any>[] = []\n  const seen = new Set<string>(existingToolNames)\n\n  for (const app of appRegistry.listApps()) {\n    const ui = app.ui\n    if (!ui) continue\n\n    const toolId = appUiToolId(app.appId)\n    if (seen.has(toolId)) {\n      console.warn(\n        `[app-ui-apps] skipping UI panel for app \"${app.appId}\": tool id \"${toolId}\" ` +\n          \"collides with an existing tool or another installed app's panel.\",\n      )\n      continue\n    }\n\n    const appName = app.name ?? app.appId\n    const html = !existsSync(app.dir)\n      ? renderAppUiErrorHtml({\n          appName,\n          message: `The app's directory \"${app.dir}\" no longer exists.`,\n          detailPath: app.dir,\n          fix: `Reinstall with \\`app_install <dir>\\`, or remove the stale install.`,\n        })\n      : await (async () => {\n          const state = await resolveAppUiBuildState({ dir: app.dir, uiPath: ui.path, build: ui.build })\n          if (state.kind === \"building\") {\n            return renderAppUiBuildingHtml({ appName, startedAt: state.startedAt, logTail: state.logTail })\n          }\n          if (state.kind === \"error\") {\n            return renderAppUiErrorHtml({\n              appName,\n              message: state.message,\n              detailPath: state.logPath,\n              logTail: state.logTail,\n            })\n          }\n          try {\n            return await cache.get(ui.path, app.updatedAt, baseUrl)\n          } catch (err) {\n            return renderAppUiErrorHtml({\n              appName,\n              message: `Could not read the built ui html at \"${ui.path}\".`,\n              detailPath: ui.path,\n              logTail: err instanceof Error ? err.message : String(err),\n            })\n          }\n        })()\n\n    seen.add(toolId)\n    apps.push({\n      id: toolId,\n      title: ui.title ?? app.name ?? app.appId,\n      ...(ui.description ? { description: ui.description } : {}),\n      inputSchema: z.object({\n        view: z\n          .string()\n          .max(200)\n          .optional()\n          .describe(\n            \"Deep-link fragment to open the panel at (e.g. \\\"wallets/my-profile\\\"), for an \" +\n              \"MCP host that cannot express a URL hash. Ignored by the browser-served panel, \" +\n              \"which reads location.hash instead.\",\n          ),\n      }),\n      execute: async (args) => ({ appId: app.appId, tools: ui.tools ?? [], view: args?.view }),\n      html,\n      ...(ui.csp\n        ? {\n            csp: {\n              ...(ui.csp.connectDomains ? { connectDomains: [...ui.csp.connectDomains] } : {}),\n              ...(ui.csp.resourceDomains ? { resourceDomains: [...ui.csp.resourceDomains] } : {}),\n              ...(ui.csp.frameDomains ? { frameDomains: [...ui.csp.frameDomains] } : {}),\n            },\n          }\n        : {}),\n    })\n  }\n\n  return apps\n}\n","/**\n * Publish ONE agentproto app as a standalone MCP App server: the app's own\n * bundled tools (`.agentproto/tools/<id>/TOOL.md` run through its\n * `.agentproto/drivers/*\\/DRIVER.md` via AIP-30 `runTool`) plus its UI as a\n * `ui://` resource. No daemon behind it and no other tool: unlike the daemon\n * gateway (local-trust by design), this is the surface that may face the\n * public internet, so it serves exactly the app's declared `ui.tools`, and\n * refuses any of them that is not one of the app's own bundled tools.\n *\n * Multi-tenant: `startAppMcpHttp({ tenants })` serves `/mcp/<tenant>`, each\n * tenant with its own resolved secrets (e.g. one shop's API key per tenant).\n *\n * NO BUILT-IN AUTHENTICATION. Anyone who can reach the port can call the\n * served tools with the (tenant's) secrets. The tenant slug in the URL only\n * SELECTS which secrets apply; it is not a credential and must not be treated\n * as one. For anything beyond loopback, put a front proxy in front that\n * terminates TLS and authenticates the caller (and maps caller -> tenant slug).\n *\n * Built-in hardening: Host/Origin are checked against an allowlist when bound\n * to loopback (DNS-rebinding guard) or when `allowedHosts` is set, request\n * bodies are capped (`maxBodyBytes`), and only POST is served.\n */\n\nimport { createServer, type IncomingMessage, type Server, type ServerResponse } from \"node:http\"\nimport type { AddressInfo } from \"node:net\"\nimport { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport { StreamableHTTPServerTransport } from \"@modelcontextprotocol/sdk/server/streamableHttp.js\"\nimport { loadAppBundledTools, loadAppHandle } from \"@agentproto/app\"\nimport { registerUiResource, toMcpTool } from \"@agentproto/mcp-server\"\nimport { appUiToolId } from \"./app-ui-apps.js\"\n\nexport interface PublishedApp {\n  readonly appId: string\n  readonly version: string\n  /** The `ui://…/view` uri the rendering tools point at. */\n  readonly resourceUri: string\n  /** The tool ids served (the app's `ui.tools`). */\n  readonly toolIds: readonly string[]\n  /** Secret names the app declares (`requirements.secrets`). */\n  readonly secretNames: readonly string[]\n  /** Build a fresh McpServer (the stateless HTTP transport is single-use,\n   *  so every request gets its own server). */\n  build(secrets?: Record<string, string>): McpServer\n}\n\nexport async function loadPublishedApp(dir: string): Promise<PublishedApp> {\n  const handle = await loadAppHandle(dir)\n  if (!handle.id) throw new Error(`app at '${dir}' has no id`)\n  const appId = handle.id\n  const ui = handle.ui\n  if (!ui) throw new Error(`app \"${appId}\" has no ui block: nothing to publish as an MCP App`)\n  const toolIds = [...(ui.tools ?? [])]\n  if (toolIds.length === 0) {\n    throw new Error(`app \"${appId}\" declares no ui.tools: refusing to publish without an explicit tool allowlist`)\n  }\n  const { tools, drivers } = await loadAppBundledTools(dir)\n  const byId = new Map(tools.map(t => [t.id, t]))\n  const missing = toolIds.filter(id => !byId.has(id))\n  if (missing.length > 0) {\n    throw new Error(\n      `app \"${appId}\": ui.tools ${missing.map(m => `'${m}'`).join(\", \")} not bundled as .agentproto/tools/<id>/TOOL.md; ` +\n        \"standalone publishing serves only the app's own bundled tools (there is no daemon behind it)\",\n    )\n  }\n  // `ui.renders` (G3) narrows which tools open the UI; absent = all of them.\n  const declaredRenders = (ui as { readonly renders?: readonly string[] }).renders\n  const renders = new Set(declaredRenders ?? toolIds)\n  const resourceUri = `ui://${appUiToolId(appId)}/view`\n  const version = handle.version ?? \"0.0.0\"\n  const csp = ui.csp\n    ? {\n        ...(ui.csp.connectDomains ? { connectDomains: [...ui.csp.connectDomains] } : {}),\n        ...(ui.csp.resourceDomains ? { resourceDomains: [...ui.csp.resourceDomains] } : {}),\n        ...(ui.csp.frameDomains ? { frameDomains: [...ui.csp.frameDomains] } : {}),\n      }\n    : undefined\n\n  return {\n    appId,\n    version,\n    resourceUri,\n    toolIds,\n    secretNames: [...handle.requirements.secrets],\n    build(secrets) {\n      const server = new McpServer({ name: appId, version })\n      registerUiResource(server, {\n        name: `${ui.title ?? handle.name ?? appId} UI`,\n        uri: resourceUri,\n        html: ui.html,\n        ...(ui.description !== undefined ? { description: ui.description } : {}),\n        ...(csp ? { csp } : {}),\n      })\n      for (const id of toolIds) {\n        toMcpTool(server, {\n          tool: byId.get(id)!,\n          candidates: drivers,\n          ...(secrets ? { secrets } : {}),\n          ...(renders.has(id) ? { ui: { resourceUri } } : {}),\n        })\n      }\n      return server\n    },\n  }\n}\n\nexport interface StartAppMcpHttpOptions {\n  app: PublishedApp\n  /** 0 or omitted = auto-assign. */\n  port?: number\n  /** Default 127.0.0.1. Put a TLS reverse proxy in front for public use. */\n  host?: string\n  /** Single-tenant secrets, served at `/mcp`. Ignored when `tenants` is set. */\n  secrets?: Record<string, string>\n  /** Multi-tenant: tenant slug → its secrets, served at `/mcp/<tenant>`. */\n  tenants?: Record<string, Record<string, string>>\n  /**\n   * Hostnames (no port) accepted in the `Host` header and, when present, the\n   * `Origin` header. Defaults to the loopback names when bound to a loopback\n   * address; when bound elsewhere and unset, no Host/Origin check is applied\n   * (the front proxy owns that). DNS-rebinding guard.\n   */\n  allowedHosts?: readonly string[]\n  /** Max request body in bytes (default 1 MiB). Larger → 413. */\n  maxBodyBytes?: number\n}\n\nexport interface AppMcpHttpHandle {\n  readonly url: string\n  readonly server: Server\n  close(): Promise<void>\n}\n\nconst TENANT_SLUG = /^[a-z0-9][a-z0-9-]{0,62}$/\nconst TENANT_PATH = /^\\/mcp\\/([a-z0-9][a-z0-9-]{0,62})$/\nconst LOOPBACK_HOSTS = [\"localhost\", \"127.0.0.1\", \"[::1]\", \"::1\"]\nconst DEFAULT_MAX_BODY_BYTES = 1024 * 1024\n\nexport function isValidTenantSlug(slug: string): boolean {\n  return TENANT_SLUG.test(slug)\n}\n\nfunction isLoopbackBind(host: string): boolean {\n  return host === \"localhost\" || host === \"::1\" || host === \"[::1]\" || /^127\\./.test(host)\n}\n\nfunction hostnameOfHostHeader(value: string): string | undefined {\n  try {\n    return new URL(`http://${value}`).hostname\n  } catch {\n    return undefined\n  }\n}\n\nclass BodyTooLargeError extends Error {}\n\nasync function readJsonBody(req: IncomingMessage, limit: number): Promise<unknown> {\n  const declared = Number(req.headers[\"content-length\"])\n  if (Number.isFinite(declared) && declared > limit) throw new BodyTooLargeError()\n  const chunks: Buffer[] = []\n  let size = 0\n  for await (const chunk of req) {\n    const buf = chunk as Buffer\n    size += buf.length\n    if (size > limit) throw new BodyTooLargeError()\n    chunks.push(buf)\n  }\n  if (size === 0) return undefined\n  return JSON.parse(Buffer.concat(chunks).toString(\"utf8\"))\n}\n\n/**\n * The SDK's stateless transport refuses a POST whose `Accept` does not\n * advertise both `application/json` and `text/event-stream` (406) and answers\n * in SSE mode by default. For a JSON-only Accept this server switches to a\n * plain JSON body (`enableJsonResponse`) and widens the header here so the\n * SDK's check passes, the same coercion the daemon's `serveMcp` applies.\n * Legacy clients that already accept `text/event-stream` keep the SSE path\n * untouched. Rewrites both the parsed header and the raw pair:\n * `@hono/node-server` builds its Headers from `req.rawHeaders`, not\n * `req.headers`. Returns true when the Accept was JSON-only and had to be\n * widened.\n */\nfunction coerceJsonOnlyAccept(req: IncomingMessage): boolean {\n  const headerValue = req.headers[\"accept\"]\n  const accept = Array.isArray(headerValue) ? headerValue.join(\",\") : headerValue\n  if (typeof accept !== \"string\") return false\n  if (!/application\\/json/i.test(accept) || /text\\/event-stream/i.test(accept)) return false\n  const widened = `${accept}, text/event-stream`\n  req.headers[\"accept\"] = widened\n  const rawHeaders = req.rawHeaders\n  for (let i = 0; i < rawHeaders.length - 1; i += 2) {\n    if (rawHeaders[i]?.toLowerCase() === \"accept\") {\n      rawHeaders[i + 1] = widened\n    }\n  }\n  return true\n}\n\nexport async function startAppMcpHttp(opts: StartAppMcpHttpOptions): Promise<AppMcpHttpHandle> {\n  const host = opts.host ?? \"127.0.0.1\"\n  const maxBody = opts.maxBodyBytes ?? DEFAULT_MAX_BODY_BYTES\n  const allowed = new Set(\n    (opts.allowedHosts ?? (isLoopbackBind(host) ? LOOPBACK_HOSTS : [])).map(h => h.toLowerCase()),\n  )\n\n  function hostAllowed(req: IncomingMessage): boolean {\n    if (allowed.size === 0) return true\n    const hostHeader = req.headers.host\n    const hostname = hostHeader ? hostnameOfHostHeader(hostHeader) : undefined\n    if (hostname === undefined || !(allowed.has(hostname) || allowed.has(`[${hostname}]`))) return false\n    const origin = req.headers.origin\n    if (origin !== undefined) {\n      const originHost = hostnameOfHostHeader(origin.replace(/^[a-z][a-z0-9+.-]*:\\/\\//i, \"\"))\n      if (originHost === undefined || !(allowed.has(originHost) || allowed.has(`[${originHost}]`))) return false\n    }\n    return true\n  }\n\n  async function handle(req: IncomingMessage, res: ServerResponse): Promise<void> {\n    if (!hostAllowed(req)) {\n      res.writeHead(403, { \"content-type\": \"text/plain\" }).end(\"forbidden host or origin\")\n      return\n    }\n    const path = (req.url ?? \"/\").split(\"?\")[0] ?? \"/\"\n    let secrets: Record<string, string> | undefined\n    if (opts.tenants) {\n      const m = TENANT_PATH.exec(path)\n      const tenant = m?.[1]\n      if (tenant === undefined || !Object.hasOwn(opts.tenants, tenant)) {\n        res.writeHead(404, { \"content-type\": \"text/plain\" }).end(\"unknown tenant\")\n        return\n      }\n      secrets = opts.tenants[tenant]\n    } else {\n      if (path !== \"/mcp\") {\n        res.writeHead(404, { \"content-type\": \"text/plain\" }).end(\"not found\")\n        return\n      }\n      secrets = opts.secrets\n    }\n    if (req.method !== \"POST\") {\n      res.writeHead(405, { \"allow\": \"POST\", \"content-type\": \"text/plain\" }).end(\"method not allowed\")\n      return\n    }\n    let body: unknown\n    try {\n      body = await readJsonBody(req, maxBody)\n    } catch (err) {\n      if (err instanceof BodyTooLargeError) {\n        res.writeHead(413, { \"content-type\": \"text/plain\", connection: \"close\" }).end(\"request body too large\")\n      } else {\n        res.writeHead(400, { \"content-type\": \"text/plain\" }).end(\"invalid JSON body\")\n      }\n      return\n    }\n    const jsonResponse = coerceJsonOnlyAccept(req)\n    const server = opts.app.build(secrets)\n    const transport = new StreamableHTTPServerTransport({\n      sessionIdGenerator: undefined,\n      ...(jsonResponse ? { enableJsonResponse: true } : {}),\n    })\n    res.on(\"close\", () => {\n      void transport.close()\n      void server.close()\n    })\n    try {\n      await server.connect(transport)\n      await transport.handleRequest(req, res, body)\n    } catch (err) {\n      if (!res.headersSent) {\n        res.writeHead(500, { \"content-type\": \"text/plain\" }).end(err instanceof Error ? err.message : String(err))\n      }\n    }\n  }\n\n  const http = createServer((req, res) => {\n    void handle(req, res)\n  })\n  await new Promise<void>((resolve, reject) => {\n    http.once(\"error\", reject)\n    http.listen(opts.port ?? 0, host, () => resolve())\n  })\n  const { port } = http.address() as AddressInfo\n  return {\n    url: `http://${host}:${port}`,\n    server: http,\n    close: () =>\n      new Promise<void>(resolve => {\n        http.close(() => resolve())\n        http.closeAllConnections()\n      }),\n  }\n}\n"]}