/** * Bridge -- PostMessage-based communication between a prefab app * (running in an iframe) and its host (the parent window). * * Dual protocol: * 1. prefab:* — custom protocol (self-hosted, any postMessage host) * 2. ui/* — MCP Apps JSON-RPC 2.0 (VS Code, Claude, ChatGPT) * * Protocol detection: races prefab:init and ui/initialize JSON-RPC * in parallel — whichever responds first wins. * * prefab:* messages: * App → Host: prefab:init, prefab:tool-call, prefab:send-message, * prefab:request-mode, prefab:open-link, prefab:update-context * Host → App: prefab:init-response, prefab:tool-input, prefab:tool-result, * prefab:tool-cancelled, prefab:theme-update, prefab:state-update * * ui/* JSON-RPC messages (MCP Apps spec 2026-01-26): * App → Host: ui/initialize, ui/notifications/initialized, * ui/notifications/size-changed, ui/open-link, ui/message, * ui/request-display-mode, ui/update-model-context * Host → App: ui/notifications/tool-input, ui/notifications/tool-result, * ui/notifications/tool-cancelled, ui/notifications/tool-input-partial, * ui/notifications/host-context-changed, ui/resource-teardown */ import type { McpTransport } from './actions.js'; export interface BridgeMessage { type: T; id?: string; payload?: Record; } export interface AppCapabilities { toolInput?: boolean; partialInput?: boolean; displayModes?: DisplayMode[]; version?: string; } export interface HostCapabilities { toast?: boolean; clipboard?: boolean; navigation?: boolean; displayModes?: DisplayMode[]; messaging?: boolean; } export type DisplayMode = 'inline' | 'fullscreen' | 'pip'; export interface HostContext { hostName?: string; hostVersion?: string; capabilities: HostCapabilities; theme?: HostTheme; toolInput?: Record; meta?: Record; } export interface HostTheme { variables?: Record; colorScheme?: 'light' | 'dark' | 'auto'; /** Raw CSS for @font-face / @import rules (from MCP Apps styles.css.fonts) */ fontCss?: string; } export declare class Bridge { private hostOrigin; private listeners; private protocol; private cleanup; private pending; private callIdCounter; private rpcPending; private rpcIdCounter; private sentRpcIds; private postFn; private _hostSupportsSubscriptions; constructor(hostOrigin?: string); /** Start listening for messages (prefab:* and JSON-RPC ui/*). */ connect(): void; /** * Init handshake. Races prefab:init and ui/initialize JSON-RPC in parallel. * Whichever protocol responds first wins. */ initialize(appCapabilities: AppCapabilities): Promise; /** Create an McpTransport that routes through the active protocol. */ createTransport(): McpTransport; /** Request a display mode change. */ requestMode(mode: DisplayMode): void; /** Request the host to open a URL. */ openLink(url: string, target?: string): void; /** Send context updates to the host. */ updateContext(context: Record): void; /** * Observe an element's size and notify the host whenever it changes. * Mirrors the ext-apps SDK's `autoResize: true` behaviour — uses a * `ResizeObserver` on the target element and sends * `ui/notifications/size-changed` (JSON-RPC) or * `prefab:size-changed` (prefab protocol). * * Returns a teardown function that disconnects the observer. */ setupAutoResize(el: HTMLElement): () => void; /** * Notify the host of declarative layout/size hints from the wire format. * Sends once when the UI mounts so the host can pre-allocate space. * * Uses `ui/notifications/preferred-size` (JSON-RPC) or * `prefab:preferred-size` (prefab protocol). */ notifyPreferredSize(layout: { preferredHeight?: number; minHeight?: number; maxHeight?: number; }): void; /** * Subscribe to a resource URI for real-time push updates. * * Sends `ui/resources/subscribe` (JSON-RPC) or `prefab:subscribe` (prefab) * to the host and registers a listener for incoming update notifications. * * @returns Cleanup function that unsubscribes from the resource. */ subscribe(uri: string, onData: (data: unknown) => void): () => void; /** Whether the host indicated subscription support during handshake. */ get supportsSubscriptions(): boolean; /** Register a handler for a message type (prefab:* or internal). */ on(type: string, handler: (payload: Record) => void): void; /** Remove a handler. */ off(type: string, handler: (payload: Record) => void): void; /** Disconnect and clean up. */ disconnect(): void; /** Which protocol is active after initialize(). */ get activeProtocol(): 'prefab' | 'jsonrpc'; private initPrefab; private createPrefabTransport; private sendPrefab; /** Handle incoming JSON-RPC message. */ private handleJsonRpc; /** Initialize via MCP Apps JSON-RPC ui/initialize handshake. */ private initJsonRpc; /** Create transport that routes tool calls via JSON-RPC tools/call. */ private createJsonRpcTransport; /** Send a JSON-RPC request and return a promise for the response. */ private sendRpcRequest; /** Send a JSON-RPC notification (no id, no response expected). */ private sendRpcNotification; /** Low-level: post a JSON-RPC envelope. Uses acquireVsCodeApi if available. */ private postJsonRpc; private dispatch; } export declare function applyHostTheme(root: HTMLElement, hostTheme: HostTheme): void; export declare function isIframe(): boolean; //# sourceMappingURL=bridge.d.ts.map