| > `docs/integration.md`, `docs/profile-v1.md` and `docs/sdk-v1.md`: v1 is mini-HTML |
| 6 | > widgets (at most 4 KB compressed, 32 KB uncompressed, delivered in the message), |
| 7 | > no bundled libraries, Shadow DOM as the web renderer, QuickJS everywhere. |
| 8 |
| 9 | Overview of the discussion: requirements, architecture options, widget formats in the ecosystem, the "bot + Claude API" scenario and the risks. |
| 10 |
| 11 | --- |
| 12 |
| 13 | ## 1. The problem |
| 14 |
| 15 | Besides Markdown, AI agents send HTML widgets in their replies: charts, calculators, quizzes, forms. They have to be shown inside Telegram messages on every client. |
| 16 |
| 17 | **Hard requirement:** a widget must have no way to reveal the user's IP, i.e. no external network calls from the device. Requests to Telegram's servers are fine — Telegram knows the IP anyway. |
| 18 |
| 19 | **Target scenario:** the bot calls the Claude API → gets a reply with HTML → puts a single self-contained HTML file (inline JS + CSS) into the message → the client shows and executes it. |
| 20 |
| 21 | --- |
| 22 |
| 23 | ## 2. Why CSP alone is not enough |
| 24 |
| 25 | CSP closes `fetch`, XHR, WebSocket and subresources, but does not close: |
| 26 |
| 27 | - **Navigation of the frame itself.** `location = 'https://x/?…'` is not caught by `connect-src`, and `navigate-to` was removed from the specification. |
| 28 | - **WebRTC.** `RTCPeerConnection` with STUN/TURN on a foreign server reveals the public IP. The `webrtc` CSP directive is poorly supported. |
| 29 | - **Fresh copies of the API.** JS patches (deleting `RTCPeerConnection` and the like) are bypassed through an `about:blank`/`srcdoc` iframe. |
| 30 | - **DNS prefetch.** `<link rel=dns-prefetch|preconnect>` and link prefetch send a request through the resolver (with ECS — the user's subnet). |
| 31 |
| 32 | **Key observation:** every hard vector requires executing JS in the page context. |
| 33 |
| 34 | --- |
| 35 |
| 36 | ## 3. Option A: hardened WebView |
| 37 |
| 38 | Three layers: the server makes the widget self-contained, the client cuts the network at the engine level, CSP and JS hardening are the second line. |
| 39 |
| 40 | ### 3.1. Server, on send |
| 41 |
| 42 | - External images, fonts and CSS are downloaded and re-uploaded to Telegram's CDN (as in Instant View). |
| 43 | - Scripts from popular CDNs (Chart.js, D3, Tailwind, Mermaid) are not bundled (widget profile 1, `docs/profile-v1.md`): an external script never runs, and widgets draw charts with inline SVG or canvas. (An earlier prototype bundled Chart.js 4 and D3 7 under their CDN names; they left the product with profile 1.) |
| 44 | - Everything that cannot be localised is cut out. The widget is stored as a bundle: HTML + assets. |
| 45 |
| 46 | ### 3.2. Native clients |
| 47 |
| 48 | - The bundle is served from a custom scheme `tgwidget://<id>/` via `WKURLSchemeHandler`; on Android — a fake https host via `WebViewAssetLoader`. Every widget gets its own origin. |
| 49 | - CSP is delivered as an HTTP header, not a meta tag: |
| 50 |
| 51 | ``` |
| 52 | default-src 'none'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; |
| 53 | img-src 'self' data: blob:; font-src 'self' data:; connect-src 'none'; frame-src 'none'; |
| 54 | form-action 'none'; base-uri 'none' |
| 55 | ``` |
| 56 |
| 57 | - A network kill switch underneath CSP: |
| 58 |
| 59 | | Platform | Mechanism | |
| 60 | |---|---| |
| 61 | | iOS / macOS | `WKContentRuleList` blocking all http(s) and ws(s) URLs; non-persistent `WKWebsiteDataStore`; `decidePolicyFor` cancels everything not on our scheme | |
| 62 | | Android | `shouldInterceptRequest` returns an empty response for any foreign host; `shouldOverrideUrlLoading` cancels navigation. WebSocket does not pass through it — only `connect-src` holds it | |
| 63 | | Desktop (WebView2) | A separate environment with the flags `--host-resolver-rules="MAP * ~NOTFOUND"`, `--proxy-server` pointing at a dead address, `--webrtc-ip-handling-policy=disable_non_proxied_udp` — closes everything at once, DNS included | |
| 64 |
| 65 | - **WebRTC is the weakest spot.** On mobile: strip the RTC APIs with a document-start script (`WKUserScript`, `addDocumentStartJavaScript` from androidx.webkit) in every frame and forbid nested frames. Needs a dedicated pentest. |
| 66 | - Reject all permission requests, forbid `window.open`, disable DNS prefetch, strip `<link rel=preconnect|dns-prefetch|prefetch>`. |
| 67 | - A tap on a link goes through the standard "Open link?" dialog: a deliberate user action, like a link in text. |
| 68 |
| 69 | ### 3.3. Web clients |
| 70 |
| 71 | - `<iframe sandbox="allow-scripts">` without `allow-same-origin`: opaque origin, no popups, forms or top-window navigation. |
| 72 | - **Self-navigation of the frame:** an intermediate frame on a separate domain with a `frame-src 'none'` header, the widget inside it via `srcdoc`. The parent's `frame-src` cuts the child frame's navigations. Works in Chromium; Safari and Firefox need checking. |
| 73 | - **WebRTC cannot be reliably closed in the browser.** Options: a static mode without JS, or option B. |
| 74 |
| 75 | --- |
| 76 |
| 77 | ## 4. Option B: QuickJS sandbox + rendering through a remote DOM |
| 78 |
| 79 | This is **not a custom browser**. The system WebView stays the renderer but stops executing foreign code. What changes is where the widget's JS runs. |
| 80 |
| 81 | ``` |
| 82 | Sandbox (QuickJS) Host (our code) WebView |
| 83 | widget JS + DOM shim ──▶ mutation validator ──▶ renderer: only |
| 84 | no fetch/WS/RTC/... + sanitizer our JS, strict CSP |
| 85 | ◀── events (click, input) ◀── |
| 86 | ``` |
| 87 |
| 88 | - **Sandbox.** A separate interpreter; we assemble the global object ourselves (essentially an isolate with an allowlist of bindings). Inside: ECMAScript, the DOM shim, timers from the host, the bridge to the bot. `fetch`, XHR, `WebSocket`, `RTCPeerConnection` are not stripped — they simply do not exist. `import()` resolves only the built-in libraries. |
| 89 | - **DOM shim.** The widget works with `document`; every mutation becomes a protocol message. The idea is Shopify's remote-dom. |
| 90 | - *Refining the analogy:* in Figma, QuickJS-wasm executes the plugin logic without a DOM (working on the scene), while the plugin UI is an ordinary iframe with network access. From Figma we take the isolation principle, from Shopify the DOM mirroring. |
| 91 | - **Host.** Treats everything inside the sandbox as hostile, the shim included. Applies mutations by allowlist: permitted tags and attributes, URLs only on our scheme / `data:` / `blob:`, a `url()` filter in CSS. `<script>`, `<iframe>`, `<meta>`, `<base>`, `<link>` and `on*` attributes do not pass. |
| 92 | - **WebView.** A dumb renderer: executes only our bundle, CSP with `script-src 'nonce-…'`. Even an `onerror=` that slipped past the sanitizer will not execute. |
| 93 |
| 94 | ### 4.1. Why this closes the holes |
| 95 |
| 96 | Once JS is taken out of the page, only declarative leaks remain — URLs in markup and CSS. Their list is finite and is closed by the sanitizer, CSP and native request interception. Deny by default instead of hunting for holes. |
| 97 |
| 98 | ### 4.2. Universality |
| 99 |
| 100 | The security boundary is the same on every platform. |
| 101 |
| 102 | | Platform | Sandbox | Renderer | |
| 103 | |---|---|---| |
| 104 | | iOS / macOS | QuickJS (C) or a bare `JSContext` without web APIs | WKWebView | |
| 105 | | Android | QuickJS via JNI or `androidx.javascriptengine` (V8 in an isolated process, no network and no DOM) | Android WebView | |
| 106 | | Desktop | QuickJS | WebView2 / WKWebView / WebKitGTK | |
| 107 | | Web | QuickJS-wasm in a Worker | sandboxed iframe | |
| 108 |
| 109 | Recommendation: QuickJS everywhere (e.g. the quickjs-ng fork) — one engine, one shim, one protocol, one test suite. It has a memory limit and an interrupt handler for killing infinite loops. |
| 110 |
| 111 | ### 4.3. Why not a "lightweight webview" wholesale |
| 112 |
| 113 | The JS sandbox is light and portable; the heavy part is HTML/CSS rendering. There is litehtml (no JS, incomplete CSS), the experimental Blitz in Rust, the commercial Ultralight and Sciter. LLM widgets make heavy use of grid, SVG, animations, canvas — the fidelity gap would keep surfacing. The system WebView as the renderer is the better deal. |
| 114 |
| 115 | Native rendering is justified only if the format is constrained rather than the runtime: our own vocabulary of components (like Instant View blocks or A2UI). |
| 116 |
| 117 | --- |
| 118 |
| 119 | ## 5. Widget formats in the ecosystem |
| 120 |
| 121 | The main distinction is who writes the HTML: the model itself or the MCP server developer. |
| 122 |
| 123 | ### 5.1. Raw HTML from the model |
| 124 |
| 125 | Claude's artifacts and inline visualisations, canvas in ChatGPT, generative UI in consumer Gemini, or simply an ```html block in the text. There is no single standard: every client wraps it its own way (a tag, a tool-call parameter, a code fence). |
| 126 |
| 127 | The content is typical: one file or fragment, inline CSS and JS, libraries from cdnjs/jsdelivr, often Tailwind CDN and Google Fonts. |
| 128 |
| 129 | ### 5.2. MCP Apps — the de facto standard for interactive widgets |
| 130 |
| 131 | - A merger of MCP-UI and OpenAI's Apps SDK; the specification was written by Anthropic, OpenAI and MCP-UI. Version 2026-01-26 is Stable. Supported by ChatGPT, VS Code Copilot Chat, Goose. |
| 132 | - A tool declares its UI via `_meta.ui.resourceUri` → a `ui://…` resource with MIME `text/html;profile=mcp-app`. |
| 133 | - Rendered in a sandboxed iframe, communication with the host is JSON-RPC 2.0 over `postMessage` (`ui/initialize`, `ui/notifications/tool-input`, `ui/notifications/tool-result`, `tools/call`). |
| 134 | - **Deny-by-default CSP:** if the resource did not declare `_meta.ui.csp`, the host applies `default-src 'none'` and lets no undeclared domain through. External access is declared explicitly: `connectDomains`, `resourceDomains`, `frameDomains`, `baseUriDomains`. |
| 135 | - The specification allows hosts to preload and cache UI resources. |
| 136 |
| 137 | Older formats are still around: |
| 138 | - OpenAI's Apps SDK: `text/html+skybridge`, data via `window.openai`. |
| 139 | - MCP-UI: `text/html`, `text/uri-list`, `application/vnd.mcp-ui.remote-dom`. Remote DOM was removed from `createUIResource` in `@mcp-ui/server` v6 — agents will not send remote-dom, so the sandbox will have to execute ordinary HTML. |
| 140 |
| 141 | ### 5.3. Declarative JSON — A2UI |
| 142 |
| 143 | Google's format (December 2025): the agent sends JSON with component names from the client's catalogue, the client draws them with its own code. No executable code comes from the agent. Renderers: Lit, Flutter, React. |
| 144 |
| 145 | ### 5.4. Conclusions for Telegram |
| 146 |
| 147 | - **MCP Apps as the primary Bot API format.** The ecosystem already writes for it, and its security model almost matches the requirement. `connectDomains` are never honoured; `resourceDomains` are closed by re-uploading on send; live data goes through `tools/call` → the bot via Telegram's servers. |
| 148 | - **"Template + data".** The template is uploaded once as a file, the message carries a reference to it plus JSON with the data. Cheaper, cacheable, the template is checked once. |
| 149 | - **Raw HTML from the model** — the same container without a template: the whole HTML is in the message, the bridge is the same. |
| 150 | - **A2UI** — a second format, secure by construction: native rendering without JS, close to Instant View blocks. Suits forms, cards, tables; expressiveness is limited by the catalogue. |
| 151 |
| 152 | --- |
| 153 |
| 154 | ## 6. The "bot + Claude API" scenario on option B |
| 155 |
| 156 | The Claude API has no dedicated "widget" type: the bot declares a tool, e.g. `show_widget(html)`, and receives the HTML as ordinary tool use. A single self-contained HTML file is the most convenient format for the sandbox. |
| 157 |
| 158 | ### 6.1. How the client executes the file |
| 159 |
| 160 | 1. **Parsing.** The HTML is parsed by a DOM implementation inside QuickJS (something like linkedom) — this is the widget's initial tree. |
| 161 | 2. **First render.** The host strips `<script>`, `on*` attributes and external URLs and hands the tree to the WebView. The layout is visible before any JS runs. |
| 162 | 3. **Scripts.** Inline `<script>`s execute in QuickJS in document order, then `DOMContentLoaded` and `load` are emulated. `<script src>` resolves only to the built-in libraries (precompiled to bytecode). |
| 163 | 4. **Mutations.** Accumulated and sent to the WebView in a batch at the end of the tick or on `requestAnimationFrame`. |
| 164 | 5. **Events.** A click/input is serialised (node id, type, coordinates, value) and dispatched in the sandbox, including handlers from `onclick="..."`. |
| 165 |
| 166 | ### 6.2. Compatibility |
| 167 |
| 168 | **Works almost for free:** layout, all of CSS, CSS animations, SVG, forms, tabs, calculators, quizzes, sortable tables — the bulk of Claude's widgets. |
| 169 |
| 170 | **Needs engineering:** |
| 171 | - **Synchronous measurements** (`getBoundingClientRect`, `offsetWidth`, `getComputedStyle`, `getBBox`). QuickJS on its own thread blocks while the main thread asks the WebView. Every call is a round trip; D3 in a loop can make hundreds. A layout cache valid until the next mutation is needed. |
| 172 | - **Canvas 2D** (Chart.js is one of the most common libraries). The context is proxied as a command stream replayed on a real canvas; `measureText` — synchronously, with a cache. The biggest chunk of work. |
| 173 | - **Input fields.** The value lives in the WebView; the sandbox has to mirror it on every `input` event. |
| 174 |
| 175 | **Does not work:** WebGL / Three.js, Web Audio, workers, the network (by design). `localStorage` is emulated in memory; links and `window.open` go through the Telegram dialog. |
| 176 |
| 177 | ### 6.3. Performance |
| 178 |
| 179 | - QuickJS is an interpreter; precompile the built-in libraries to bytecode. |
| 180 | - The per-widget runtime is cheap, the WebView is more expensive. Execute only visible widgets; kill those that left the screen, keeping a static snapshot of the DOM; restart on return or on tap. |
| 181 |
| 182 | ### 6.4. The main lever — a runtime profile |
| 183 |
| 184 | The bot puts the Telegram runtime profile into the description of the `show_widget` tool: |
| 185 | - one file, no network; |
| 186 | - the list of available libraries and their path; |
| 187 | - no WebGL; |
| 188 | - a bridge like `Telegram.Widget.sendMessage()` for user actions. |
| 189 |
| 190 | The model follows such constraints well. The client then has to execute not an arbitrary widget but a widget written for a known profile — compatibility rises sharply. The profile can be published as part of the Bot API documentation. |
| 191 |
| 192 | ### 6.5. Validation and fallback |
| 193 |
| 194 | - **Validation:** run a few hundred typical requests through the API with and without the profile, collect the widgets, run them through the prototype, count the share rendered correctly. |
| 195 | - **Fallback:** for the unsupported (WebGL) — a static snapshot with an "Open" button into a full-screen WebView with the native kill switch from option A. |
| 196 |
| 197 | --- |
| 198 |
| 199 | ## 7. Open questions and risks |
| 200 |
| 201 | - **The synchronous bridge on the web** requires cross-origin isolation (COOP/COEP) — may break other embeddings in the web client. |
| 202 | - **`preventDefault` from the sandbox is asynchronous** — it cannot cancel the native default action in the renderer. The renderer always cancels navigation and submit; the other cases (checkboxes, selection) need to be described and worked around. |
| 203 | - **Focus, caret, IME** in input fields when mirroring the value. |
| 204 | - **D3 / Mermaid performance** with hundreds of `getBBox` calls. |
| 205 | - **Memory:** a WebView per visible widget. |
| 206 | - **WebRTC** in option A on mobile — API removal only, needs a pentest. |
| 207 | - **App Store Review Guidelines:** check executing downloaded JS in our own interpreter. |
| 208 | - **Self-navigation of the frame in the web client** (option A) — check the behaviour of the parent's `frame-src` in Safari and Firefox. |
| 209 |
| 210 | --- |
| 211 |
| 212 | ## 8. Next step |
| 213 |
| 214 | A prototype of option B in the browser: QuickJS-wasm in a Worker + a sandboxed iframe as the renderer + automated tests for leaks and compatibility. The browser version mirrors the native architecture (JS on a separate thread, the renderer separate) and immediately answers the question for the web client. |
| 215 |
| 216 | --- |
| 217 |
| 218 | ## Sources |
| 219 |
| 220 | - [MCP Apps: Interactive UIs Become an Official Part of MCP](https://rits.shanghai.nyu.edu/ai/mcp-apps-interactive-uis-become-an-official-part-of-mcp) |
| 221 | - [MCP Apps: The iframe Is the Agent](https://dev.to/rcsxplatform/mcp-apps-the-iframe-is-the-agent-40k9) |
| 222 | - [What Are MCP Apps](https://www.morphllm.com/mcp-apps) |
| 223 | - [Building MCP Apps (hexdocs)](https://hexdocs.pm/mcp_server/mcp_apps.html) |
| 224 | - [MCP Apps reference (nanohub)](https://nanohub-mcp.readthedocs.io/en/latest/skill/references/mcp-apps.html) |
| 225 | - [MCP-UI on GitHub](https://github.com/MCP-UI-Org/mcp-ui) |
| 226 | - [mcp-use widgets API reference](https://docs.mcp-use.com/typescript/api-reference/server/widgets) |
| 227 | - [Apps SDK Resources (mcp-use)](https://mcp-use.com/docs/typescript/server/apps-sdk-resources) |
| 228 | - [The call for an agentic standard (UX Collective)](https://uxdesign.cc/the-call-for-an-agentic-standard-we-need-to-stop-shipping-the-same-form-four-different-times-be5b9d40e370) |
| 229 | - [Google launches A2UI (SD Times)](https://sdtimes.com/ai/google-launches-a2ui-project-to-enable-agents-to-build-contextually-relevant-uis/) |
| 230 | - [A2UI (landscape)](https://landscape.jimmysong.io/projects/a2ui/) |
| 231 |