@guuey/mcp-apps-host 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/action.d.ts +6 -0
- package/dist/action.d.ts.map +1 -1
- package/dist/action.js +11 -6
- package/dist/card-mount.d.ts +18 -0
- package/dist/card-mount.d.ts.map +1 -1
- package/dist/card-mount.js +25 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/react.d.ts +94 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +158 -0
- package/dist/sandbox-page.d.ts +75 -0
- package/dist/sandbox-page.d.ts.map +1 -0
- package/dist/sandbox-page.js +40 -0
- package/dist/view-host-protocol.d.ts +167 -0
- package/dist/view-host-protocol.d.ts.map +1 -0
- package/dist/view-host-protocol.js +168 -0
- package/dist/view-host.d.ts +119 -0
- package/dist/view-host.d.ts.map +1 -0
- package/dist/view-host.js +143 -0
- package/package.json +18 -1
- package/src/action.ts +11 -6
- package/src/card-mount.ts +27 -0
- package/src/index.ts +33 -0
- package/src/react.tsx +268 -0
- package/src/sandbox-page.ts +116 -0
- package/src/view-host-protocol.ts +299 -0
- package/src/view-host.ts +241 -0
package/src/action.ts
CHANGED
|
@@ -58,7 +58,12 @@ export const UI_ACTION_TOOLS: ReadonlySet<string> = new Set([
|
|
|
58
58
|
export const UI_ACTION_UNAVAILABLE_TEXT =
|
|
59
59
|
"This action isn't available right now.";
|
|
60
60
|
|
|
61
|
-
|
|
61
|
+
/**
|
|
62
|
+
* The in-band `isError` result for an action that cannot be performed —
|
|
63
|
+
* the relay's own refusals use it, and `attachViewHost` posts it when an
|
|
64
|
+
* embedder-supplied relay hook rejects (the view is always answered).
|
|
65
|
+
*/
|
|
66
|
+
export function unavailableToolCallResult(): McpToolCallResult {
|
|
62
67
|
return {
|
|
63
68
|
content: [{ type: "text", text: UI_ACTION_UNAVAILABLE_TEXT }],
|
|
64
69
|
isError: true,
|
|
@@ -152,15 +157,15 @@ export function createMcpUiActionRelay(
|
|
|
152
157
|
deps: CreateMcpUiActionRelayDeps,
|
|
153
158
|
): (request: UiActionRequest) => Promise<McpToolCallResult> {
|
|
154
159
|
return async (request) => {
|
|
155
|
-
if (!UI_ACTION_TOOLS.has(request.name)) return
|
|
156
|
-
if (!request.resourceUri.startsWith("ui://")) return
|
|
160
|
+
if (!UI_ACTION_TOOLS.has(request.name)) return unavailableToolCallResult();
|
|
161
|
+
if (!request.resourceUri.startsWith("ui://")) return unavailableToolCallResult();
|
|
157
162
|
let raw: unknown;
|
|
158
163
|
try {
|
|
159
164
|
raw = await deps.callTool(request.resourceUri, request.name, request.arguments);
|
|
160
165
|
} catch {
|
|
161
|
-
return
|
|
166
|
+
return unavailableToolCallResult(); // transport failure == unavailable, in-band
|
|
162
167
|
}
|
|
163
|
-
if (raw === undefined) return
|
|
164
|
-
return asToolCallResult(raw) ??
|
|
168
|
+
if (raw === undefined) return unavailableToolCallResult();
|
|
169
|
+
return asToolCallResult(raw) ?? unavailableToolCallResult();
|
|
165
170
|
};
|
|
166
171
|
}
|
package/src/card-mount.ts
CHANGED
|
@@ -143,6 +143,33 @@ export function snapshotViewMount(cardSnapshot: JsonValue): ViewMount | undefine
|
|
|
143
143
|
return undefined;
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
/**
|
|
147
|
+
* The resolved-only convenience over the mount union (guuey#186 G6): every
|
|
148
|
+
* consumer that renders was writing the same two-call walk — narrow the
|
|
149
|
+
* union, then feed the `"locator"` arm to a reader. This collapses it:
|
|
150
|
+
*
|
|
151
|
+
* - already-resolved mounts pass through untouched (no reader round-trip);
|
|
152
|
+
* - a `"locator"` arm resolves via the reader — or the honest `undefined`
|
|
153
|
+
* (placeholder) when no reader is wired: never a stale mount;
|
|
154
|
+
* - a reader that answers with ANOTHER locator is treated as a miss. The
|
|
155
|
+
* {@link UiResourceReader} contract says a read yields mount material or
|
|
156
|
+
* nothing (guuey#127); a locator answer would loop, so the honest
|
|
157
|
+
* reading is "could not resolve", not recursion.
|
|
158
|
+
*
|
|
159
|
+
* Takes `ViewMount | undefined` so it chains directly off
|
|
160
|
+
* `toolResultViewMount`/`snapshotViewMount` without a narrowing dance at
|
|
161
|
+
* the call site.
|
|
162
|
+
*/
|
|
163
|
+
export async function resolveViewMount(
|
|
164
|
+
mount: ViewMount | undefined,
|
|
165
|
+
reader?: UiResourceReader,
|
|
166
|
+
): Promise<ResolvedViewMount | undefined> {
|
|
167
|
+
if (mount === undefined || mount.channel !== "locator") return mount;
|
|
168
|
+
if (reader === undefined) return undefined;
|
|
169
|
+
const read = await reader(mount.resourceUri);
|
|
170
|
+
return read === undefined || read.channel === "locator" ? undefined : read;
|
|
171
|
+
}
|
|
172
|
+
|
|
146
173
|
/**
|
|
147
174
|
* The blocks to scan inside a card snapshot: the stored `AgArtifact`'s `parts`
|
|
148
175
|
* when present, then the snapshot root itself — exactly `snapshotUiResource`'s own
|
package/src/index.ts
CHANGED
|
@@ -28,6 +28,7 @@ export {
|
|
|
28
28
|
type GguiShellHtmlOptions,
|
|
29
29
|
} from "./ggui-render.js";
|
|
30
30
|
export {
|
|
31
|
+
resolveViewMount,
|
|
31
32
|
snapshotViewMount,
|
|
32
33
|
toolResultViewMount,
|
|
33
34
|
type LocatorViewMount,
|
|
@@ -45,6 +46,7 @@ export {
|
|
|
45
46
|
export {
|
|
46
47
|
asToolCallResult,
|
|
47
48
|
createMcpUiActionRelay,
|
|
49
|
+
unavailableToolCallResult,
|
|
48
50
|
UI_ACTION_TOOLS,
|
|
49
51
|
UI_ACTION_UNAVAILABLE_TEXT,
|
|
50
52
|
type CreateMcpUiActionRelayDeps,
|
|
@@ -53,3 +55,34 @@ export {
|
|
|
53
55
|
type McpToolStructuredContent,
|
|
54
56
|
type UiActionRequest,
|
|
55
57
|
} from "./action.js";
|
|
58
|
+
export {
|
|
59
|
+
initializeResult,
|
|
60
|
+
initialViewHostState,
|
|
61
|
+
teardownMessage,
|
|
62
|
+
toolCallResponse,
|
|
63
|
+
TOOLS_CALL_METHOD,
|
|
64
|
+
viewHostElapsed,
|
|
65
|
+
viewHostReceive,
|
|
66
|
+
type ViewHostBehavior,
|
|
67
|
+
type ViewHostEffect,
|
|
68
|
+
type ViewHostOutbound,
|
|
69
|
+
type ViewHostPhase,
|
|
70
|
+
type ViewHostInfo,
|
|
71
|
+
type ViewHostState,
|
|
72
|
+
type ViewHostTransition,
|
|
73
|
+
type ViewRequestId,
|
|
74
|
+
} from "./view-host-protocol.js";
|
|
75
|
+
export {
|
|
76
|
+
attachViewHost,
|
|
77
|
+
viewDocumentHtml,
|
|
78
|
+
type AttachViewHostConfig,
|
|
79
|
+
type ViewFrameLike,
|
|
80
|
+
type ViewHostEvents,
|
|
81
|
+
} from "./view-host.js";
|
|
82
|
+
export {
|
|
83
|
+
attachSandboxPageDelivery,
|
|
84
|
+
isSandboxProxyReady,
|
|
85
|
+
SANDBOX_PROXY_READY_METHOD,
|
|
86
|
+
SANDBOX_RESOURCE_READY_METHOD,
|
|
87
|
+
type SandboxPageDeliveryConfig,
|
|
88
|
+
} from "./sandbox-page.js";
|
package/src/react.tsx
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React entry point (`@guuey/mcp-apps-host/react`).
|
|
3
|
+
*
|
|
4
|
+
* `<GuueyView>` is the one React-coupled surface — the root subpath stays
|
|
5
|
+
* React-free (narrowing, rehydration, the relay, `attachViewHost` itself),
|
|
6
|
+
* so server-side consumers never import React at all. The component is a
|
|
7
|
+
* CONVENIENCE composition of the framework-agnostic primitive: iframe
|
|
8
|
+
* creation + the sandbox invariant + the handshake + lifecycle. A host
|
|
9
|
+
* that needs a different composition (a transcript renderer, a non-React
|
|
10
|
+
* surface) uses `attachViewHost` directly.
|
|
11
|
+
*
|
|
12
|
+
* ## Sandbox posture (invariant, not preference)
|
|
13
|
+
*
|
|
14
|
+
* `sandbox="allow-scripts"` WITHOUT `allow-same-origin`: a `srcdoc` frame
|
|
15
|
+
* inherits its embedder's origin, so granting both would run
|
|
16
|
+
* agent-generated HTML AS the embedding page — reach into its DOM and its
|
|
17
|
+
* signed-in session, an XSS by construction. Dropping `allow-same-origin`
|
|
18
|
+
* puts the document in an opaque origin instead. Extra flags ride ON TOP
|
|
19
|
+
* via {@link GuueyViewProps.dangerouslyAddSandboxFlags} — named the way it
|
|
20
|
+
* is because every flag it adds widens what agent-generated HTML can do.
|
|
21
|
+
* `allow="clipboard-write"` is delegated by default: generated views own
|
|
22
|
+
* copy buttons, and without the delegation every one of them silently
|
|
23
|
+
* no-ops inside the opaque origin.
|
|
24
|
+
*
|
|
25
|
+
* ## Who paints which state
|
|
26
|
+
*
|
|
27
|
+
* While `"negotiating"`, the component shows a small non-blocking status
|
|
28
|
+
* line — never a bare blank frame (guuey#186 audit). On `"connected"` the
|
|
29
|
+
* view owns its pixels (and its own failures) and the line disappears. On
|
|
30
|
+
* `"no-handshake"` the meaning is channel-aware: a `"ggui"` shell always
|
|
31
|
+
* negotiates, so silence is a boot failure and is labeled as one; an
|
|
32
|
+
* `"inline"` card is arbitrary tenant HTML with no handshake obligation,
|
|
33
|
+
* so the status line simply retires and the document stands as rendered.
|
|
34
|
+
*/
|
|
35
|
+
import { useEffect, useMemo, useRef, useState, type CSSProperties, type ReactNode } from "react";
|
|
36
|
+
import { attachViewHost, viewDocumentHtml, type AttachViewHostConfig } from "./view-host.js";
|
|
37
|
+
import { attachSandboxPageDelivery } from "./sandbox-page.js";
|
|
38
|
+
import type { ViewHostPhase } from "./view-host-protocol.js";
|
|
39
|
+
import type { ResolvedViewMount } from "./card-mount.js";
|
|
40
|
+
|
|
41
|
+
export { attachViewHost, viewDocumentHtml } from "./view-host.js";
|
|
42
|
+
export type { AttachViewHostConfig, ViewFrameLike, ViewHostEvents } from "./view-host.js";
|
|
43
|
+
export {
|
|
44
|
+
attachSandboxPageDelivery,
|
|
45
|
+
isSandboxProxyReady,
|
|
46
|
+
SANDBOX_PROXY_READY_METHOD,
|
|
47
|
+
SANDBOX_RESOURCE_READY_METHOD,
|
|
48
|
+
type SandboxPageDeliveryConfig,
|
|
49
|
+
} from "./sandbox-page.js";
|
|
50
|
+
export type { ViewHostPhase } from "./view-host-protocol.js";
|
|
51
|
+
export type { ResolvedViewMount, ViewMount, ViewMountChannel } from "./card-mount.js";
|
|
52
|
+
|
|
53
|
+
/** Accessible name for a mounted view when the caller has nothing better. */
|
|
54
|
+
const DEFAULT_TITLE = "Generated view";
|
|
55
|
+
|
|
56
|
+
export interface GuueyViewProps
|
|
57
|
+
extends Pick<
|
|
58
|
+
AttachViewHostConfig,
|
|
59
|
+
"hostCapabilities" | "hostInfo" | "hostContext" | "onCallTool" | "negotiationTimeoutMs"
|
|
60
|
+
> {
|
|
61
|
+
/** The resolved card to mount (see `toolResultViewMount`/`resolveViewMount`). */
|
|
62
|
+
mount: ResolvedViewMount;
|
|
63
|
+
/**
|
|
64
|
+
* Opt into the TWO-ORIGIN mount: instead of `srcdoc`, the frame loads
|
|
65
|
+
* this host-served sandbox page (guuey's `/mcp-app-sandbox` pattern — the
|
|
66
|
+
* caller builds the full URL, channel/app query included) and the
|
|
67
|
+
* document is delivered over the page's relay protocol
|
|
68
|
+
* (`attachSandboxPageDelivery`). Why: a `srcdoc` frame INHERITS the
|
|
69
|
+
* embedder's CSP, so its egress confinement is whatever the page happens
|
|
70
|
+
* to carry; the sandbox page is served WITH the per-request CSP that
|
|
71
|
+
* confines the mount — and the untrusted document lands in the page's
|
|
72
|
+
* own inner opaque frame, never in this one. In this mode the frame's
|
|
73
|
+
* `sandbox` gains `allow-same-origin` — REQUIRED and safe: the frame
|
|
74
|
+
* holds the cross-origin RELAY PAGE (which must run as its real origin
|
|
75
|
+
* for its CSP + referrer checks to mean anything), never agent HTML.
|
|
76
|
+
* The page must be a genuinely different origin; a same-origin URL is
|
|
77
|
+
* refused with a labeled state, never mounted.
|
|
78
|
+
*/
|
|
79
|
+
sandboxPageUrl?: string;
|
|
80
|
+
/**
|
|
81
|
+
* Sandbox flags appended to the safe default (`allow-scripts`). Every
|
|
82
|
+
* entry widens what agent-generated HTML may do — `allow-same-origin`
|
|
83
|
+
* in particular hands it the embedder's origin. Prefer leaving unset.
|
|
84
|
+
* In `sandboxPageUrl` mode these forward to the INNER frame via the
|
|
85
|
+
* page's relay (which strips `allow-same-origin` regardless).
|
|
86
|
+
*/
|
|
87
|
+
dangerouslyAddSandboxFlags?: string[];
|
|
88
|
+
/** Permissions-Policy delegation for the frame. Default `clipboard-write`. */
|
|
89
|
+
allow?: string;
|
|
90
|
+
/** Accessible frame title. Default "Generated view". */
|
|
91
|
+
title?: string;
|
|
92
|
+
className?: string;
|
|
93
|
+
style?: CSSProperties;
|
|
94
|
+
/** Observe the negotiation phase (the same states the default UI labels). */
|
|
95
|
+
onPhaseChange?: (phase: ViewHostPhase) => void;
|
|
96
|
+
/**
|
|
97
|
+
* Replace the default status/failure line for a phase. Return `null` for
|
|
98
|
+
* "render nothing". The default: a quiet "Negotiating with view…" line
|
|
99
|
+
* while `"negotiating"`; a labeled failure for `"no-handshake"` on the
|
|
100
|
+
* `"ggui"` channel; nothing once `"connected"` (the view owns its
|
|
101
|
+
* pixels) and nothing for a silent `"inline"` card.
|
|
102
|
+
*/
|
|
103
|
+
renderStatus?: (phase: ViewHostPhase) => ReactNode;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const statusLineStyle: CSSProperties = {
|
|
107
|
+
position: "absolute",
|
|
108
|
+
insetInlineStart: 8,
|
|
109
|
+
insetBlockEnd: 8,
|
|
110
|
+
margin: 0,
|
|
111
|
+
padding: "2px 8px",
|
|
112
|
+
fontSize: 12,
|
|
113
|
+
lineHeight: "18px",
|
|
114
|
+
opacity: 0.65,
|
|
115
|
+
pointerEvents: "none",
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
function defaultStatus(phase: ViewHostPhase, channel: ResolvedViewMount["channel"]): ReactNode {
|
|
119
|
+
if (phase === "negotiating") {
|
|
120
|
+
return <p style={statusLineStyle}>Negotiating with view…</p>;
|
|
121
|
+
}
|
|
122
|
+
if (phase === "no-handshake" && channel === "ggui") {
|
|
123
|
+
// A ggui shell negotiates unconditionally before painting, so silence
|
|
124
|
+
// here is a boot failure with no other author — label it (role=alert
|
|
125
|
+
// so it is announced, not just drawn).
|
|
126
|
+
return (
|
|
127
|
+
<p role="alert" style={{ ...statusLineStyle, opacity: 1, pointerEvents: "auto" }}>
|
|
128
|
+
This view did not start — it never negotiated with the host.
|
|
129
|
+
</p>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Mount a resolved view and play the MCP Apps Host for it. See the module
|
|
137
|
+
* docblock for the sandbox and state contracts.
|
|
138
|
+
*/
|
|
139
|
+
export function GuueyView(props: GuueyViewProps): ReactNode {
|
|
140
|
+
const {
|
|
141
|
+
mount,
|
|
142
|
+
sandboxPageUrl,
|
|
143
|
+
dangerouslyAddSandboxFlags,
|
|
144
|
+
allow,
|
|
145
|
+
title,
|
|
146
|
+
className,
|
|
147
|
+
style,
|
|
148
|
+
onPhaseChange,
|
|
149
|
+
renderStatus,
|
|
150
|
+
...hostConfig
|
|
151
|
+
} = props;
|
|
152
|
+
const frameRef = useRef<HTMLIFrameElement>(null);
|
|
153
|
+
const [phase, setPhase] = useState<ViewHostPhase>("negotiating");
|
|
154
|
+
const html = viewDocumentHtml(mount.resource);
|
|
155
|
+
|
|
156
|
+
// Vet the sandbox page once per URL. Same-origin is REFUSED (the widget's
|
|
157
|
+
// ResourceMount precedent, generalized): the whole point of the page is
|
|
158
|
+
// being a different origin — same-origin would hand the relay page (and
|
|
159
|
+
// through `allow-same-origin`, everything it can reach) the embedder's
|
|
160
|
+
// own origin.
|
|
161
|
+
const sandboxPage: URL | "refused" | undefined = useMemo(() => {
|
|
162
|
+
if (sandboxPageUrl === undefined) return undefined;
|
|
163
|
+
let url: URL;
|
|
164
|
+
try {
|
|
165
|
+
url = new URL(sandboxPageUrl);
|
|
166
|
+
} catch {
|
|
167
|
+
return "refused";
|
|
168
|
+
}
|
|
169
|
+
if (typeof window !== "undefined" && url.origin === window.location.origin) return "refused";
|
|
170
|
+
return url;
|
|
171
|
+
}, [sandboxPageUrl]);
|
|
172
|
+
const page = sandboxPage instanceof URL ? sandboxPage : undefined;
|
|
173
|
+
|
|
174
|
+
// The attachment is keyed to the mounted DOCUMENT, not to every render's
|
|
175
|
+
// fresh callback identities — host config rides a ref so the effect's
|
|
176
|
+
// dependency list is honestly just the document identity.
|
|
177
|
+
const latest = useRef({ hostConfig, onPhaseChange, dangerouslyAddSandboxFlags });
|
|
178
|
+
latest.current = { hostConfig, onPhaseChange, dangerouslyAddSandboxFlags };
|
|
179
|
+
|
|
180
|
+
useEffect(() => {
|
|
181
|
+
// Keyed to the same identity the frame is (the resource uri): a new
|
|
182
|
+
// document boots fresh, and the previous negotiation's phase must not
|
|
183
|
+
// paper over it.
|
|
184
|
+
setPhase("negotiating");
|
|
185
|
+
const frame = frameRef.current;
|
|
186
|
+
if (frame === null || html === undefined) return;
|
|
187
|
+
if (sandboxPageUrl !== undefined && page === undefined) return; // refused config — nothing mounts
|
|
188
|
+
const resourceUri = mount.resource.uri;
|
|
189
|
+
const detachHost = attachViewHost(frame, {
|
|
190
|
+
...latest.current.hostConfig,
|
|
191
|
+
resourceUri,
|
|
192
|
+
onPhaseChange: (next) => {
|
|
193
|
+
setPhase(next);
|
|
194
|
+
latest.current.onPhaseChange?.(next);
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
if (page === undefined) return detachHost;
|
|
198
|
+
// Two-origin mode: the page announces readiness, the document is
|
|
199
|
+
// delivered over its relay (re-delivered on a reload's re-announce),
|
|
200
|
+
// and the view-host handshake crosses the same relay transparently.
|
|
201
|
+
const flags = latest.current.dangerouslyAddSandboxFlags;
|
|
202
|
+
const detachDelivery = attachSandboxPageDelivery(frame, {
|
|
203
|
+
pageOrigin: page.origin,
|
|
204
|
+
html,
|
|
205
|
+
...(flags !== undefined && flags.length > 0
|
|
206
|
+
? { sandbox: ["allow-scripts", ...flags].join(" ") }
|
|
207
|
+
: {}),
|
|
208
|
+
});
|
|
209
|
+
return () => {
|
|
210
|
+
detachDelivery();
|
|
211
|
+
detachHost();
|
|
212
|
+
};
|
|
213
|
+
}, [mount.resource.uri, html, sandboxPageUrl, page]);
|
|
214
|
+
|
|
215
|
+
if (html === undefined) {
|
|
216
|
+
// A resolved mount with no document is producer-side breakage; an
|
|
217
|
+
// empty frame would be a lie. Label it, in the same voice as the
|
|
218
|
+
// no-handshake state.
|
|
219
|
+
return (
|
|
220
|
+
<div className={className} style={{ position: "relative", ...style }}>
|
|
221
|
+
<p role="alert" style={{ ...statusLineStyle, opacity: 1, pointerEvents: "auto" }}>
|
|
222
|
+
This view could not be displayed — its resource carries no document.
|
|
223
|
+
</p>
|
|
224
|
+
</div>
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (sandboxPageUrl !== undefined && page === undefined) {
|
|
229
|
+
// A malformed or SAME-ORIGIN sandbox page is a configuration state, not
|
|
230
|
+
// a property of the card — refused, labeled, never mounted.
|
|
231
|
+
return (
|
|
232
|
+
<div className={className} style={{ position: "relative", ...style }}>
|
|
233
|
+
<p role="alert" style={{ ...statusLineStyle, opacity: 1, pointerEvents: "auto" }}>
|
|
234
|
+
Interactive view unavailable — the sandbox page is not usable from this origin.
|
|
235
|
+
</p>
|
|
236
|
+
</div>
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return (
|
|
241
|
+
<div className={className} style={{ position: "relative", ...style }}>
|
|
242
|
+
<iframe
|
|
243
|
+
ref={frameRef}
|
|
244
|
+
// Remount on a new resource (or mount mode) rather than reusing the
|
|
245
|
+
// frame: a view runtime boots once from the document it was handed,
|
|
246
|
+
// so swapping `srcDoc` in place would leave the old boot running
|
|
247
|
+
// against new markup.
|
|
248
|
+
key={`${page?.href ?? "srcdoc"}::${mount.resource.uri}`}
|
|
249
|
+
{...(page !== undefined ? { src: page.href } : { srcDoc: html })}
|
|
250
|
+
title={title ?? DEFAULT_TITLE}
|
|
251
|
+
// srcdoc mode: the INVARIANT — agent HTML in an opaque origin, extra
|
|
252
|
+
// flags only widen knowingly. Page mode: the frame holds the
|
|
253
|
+
// cross-origin RELAY PAGE, which must run as its real origin
|
|
254
|
+
// (`allow-same-origin`) for its CSP/referrer machinery to exist at
|
|
255
|
+
// all; the agent HTML lands in the page's own inner opaque frame,
|
|
256
|
+
// and the caller's extra flags travel to THAT frame via the relay.
|
|
257
|
+
sandbox={
|
|
258
|
+
page !== undefined
|
|
259
|
+
? "allow-scripts allow-same-origin allow-forms"
|
|
260
|
+
: ["allow-scripts", ...(dangerouslyAddSandboxFlags ?? [])].join(" ")
|
|
261
|
+
}
|
|
262
|
+
allow={allow ?? "clipboard-write"}
|
|
263
|
+
style={{ display: "block", width: "100%", height: "100%", border: 0 }}
|
|
264
|
+
/>
|
|
265
|
+
{renderStatus !== undefined ? renderStatus(phase) : defaultStatus(phase, mount.channel)}
|
|
266
|
+
</div>
|
|
267
|
+
);
|
|
268
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sandbox-PAGE document delivery — the client half of the two-origin mount
|
|
3
|
+
* (guuey#135 wave-3c, from the #186/#135 dogfood's finding 2).
|
|
4
|
+
*
|
|
5
|
+
* ## Why a second mount mode exists
|
|
6
|
+
*
|
|
7
|
+
* The default `<GuueyView>` mount is a `srcdoc` frame: zero configuration,
|
|
8
|
+
* opaque origin, correct sandbox posture — but a `srcdoc` document INHERITS
|
|
9
|
+
* the embedding page's Content-Security-Policy, so the strongest egress
|
|
10
|
+
* confinement it can have is whatever the embedder's page happens to carry.
|
|
11
|
+
* Guuey's production surfaces confine harder: the untrusted document mounts
|
|
12
|
+
* inside a HOST-SERVED sandbox page on a second origin, whose per-request
|
|
13
|
+
* CSP names exactly the egress that mount is entitled to (see the platform's
|
|
14
|
+
* `/mcp-app-sandbox` route — per-channel `connect-src`, per-app
|
|
15
|
+
* `frame-ancestors`). This module speaks that page's delivery protocol so
|
|
16
|
+
* any kit consumer can opt into the same confinement.
|
|
17
|
+
*
|
|
18
|
+
* ## The protocol (co-owned in-repo; two notifications)
|
|
19
|
+
*
|
|
20
|
+
* The page is the reference sandbox relay (adapted from the MCP ext-apps
|
|
21
|
+
* `basic-host` example, vendored at
|
|
22
|
+
* `create-agentic-app/templates-src/base/web/sandbox-proxy.ts` and served by
|
|
23
|
+
* the platform's landing route). Its wire is exactly two JSON-RPC
|
|
24
|
+
* notifications:
|
|
25
|
+
*
|
|
26
|
+
* 1. page → host: `ui/notifications/sandbox-proxy-ready` — the relay booted
|
|
27
|
+
* and is listening;
|
|
28
|
+
* 2. host → page: `ui/notifications/sandbox-resource-ready` with
|
|
29
|
+
* `params.html` (+ optional `params.sandbox` tokens for the INNER frame —
|
|
30
|
+
* the page strips `allow-same-origin` from any value regardless).
|
|
31
|
+
*
|
|
32
|
+
* Every other message crosses the page transparently in both directions,
|
|
33
|
+
* which is why `attachViewHost` works unchanged on top of this delivery: the
|
|
34
|
+
* view's `ui/initialize` arrives relayed with `event.source` still the OUTER
|
|
35
|
+
* frame's window, and the host's answers relay inward.
|
|
36
|
+
*
|
|
37
|
+
* ## Identity + targeting
|
|
38
|
+
*
|
|
39
|
+
* Inbound messages are matched by `event.source === frame.contentWindow` —
|
|
40
|
+
* the package's standing identity invariant (`view-host.ts`). Outbound
|
|
41
|
+
* delivery targets `config.pageOrigin` EXPLICITLY (never `'*'`): unlike a
|
|
42
|
+
* srcdoc view, the sandbox page has a real origin, and the document being
|
|
43
|
+
* delivered is agent-generated content the caller confined on purpose — if
|
|
44
|
+
* the frame somehow navigated elsewhere, the browser drops the message
|
|
45
|
+
* instead of handing the document to the wrong receiver.
|
|
46
|
+
*/
|
|
47
|
+
import type { ViewFrameLike, ViewHostEvents } from "./view-host.js";
|
|
48
|
+
|
|
49
|
+
export const SANDBOX_PROXY_READY_METHOD = "ui/notifications/sandbox-proxy-ready";
|
|
50
|
+
export const SANDBOX_RESOURCE_READY_METHOD = "ui/notifications/sandbox-resource-ready";
|
|
51
|
+
|
|
52
|
+
/** Structural check for the page's ready notification. */
|
|
53
|
+
export function isSandboxProxyReady(data: unknown): boolean {
|
|
54
|
+
return (
|
|
55
|
+
typeof data === "object" &&
|
|
56
|
+
data !== null &&
|
|
57
|
+
!Array.isArray(data) &&
|
|
58
|
+
(data as { method?: unknown }).method === SANDBOX_PROXY_READY_METHOD
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface SandboxPageDeliveryConfig {
|
|
63
|
+
/**
|
|
64
|
+
* The sandbox page's origin — the ONLY target the document is posted to.
|
|
65
|
+
* Derive it from the page URL the frame was given (`new URL(url).origin`).
|
|
66
|
+
*/
|
|
67
|
+
pageOrigin: string;
|
|
68
|
+
/** The document to deliver (the view's `viewDocumentHtml`). */
|
|
69
|
+
html: string;
|
|
70
|
+
/**
|
|
71
|
+
* Inner-frame sandbox tokens forwarded as `params.sandbox`. The page's
|
|
72
|
+
* `safeSandbox` strips `allow-same-origin` and guarantees `allow-scripts`
|
|
73
|
+
* whatever is sent — this only ever WIDENS within the page's own bounds.
|
|
74
|
+
*/
|
|
75
|
+
sandbox?: string;
|
|
76
|
+
/** Message-event source, injectable for tests. Default: `window`. */
|
|
77
|
+
events?: ViewHostEvents;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Deliver a view document to a mounted sandbox page, re-delivering on every
|
|
82
|
+
* `sandbox-proxy-ready` (a reloaded page announces again and must be
|
|
83
|
+
* re-seeded). Returns a detach function.
|
|
84
|
+
*/
|
|
85
|
+
export function attachSandboxPageDelivery(
|
|
86
|
+
frame: ViewFrameLike,
|
|
87
|
+
config: SandboxPageDeliveryConfig,
|
|
88
|
+
): () => void {
|
|
89
|
+
const deliver = (): void => {
|
|
90
|
+
frame.contentWindow?.postMessage(
|
|
91
|
+
{
|
|
92
|
+
jsonrpc: "2.0",
|
|
93
|
+
method: SANDBOX_RESOURCE_READY_METHOD,
|
|
94
|
+
params: {
|
|
95
|
+
html: config.html,
|
|
96
|
+
...(config.sandbox !== undefined ? { sandbox: config.sandbox } : {}),
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
config.pageOrigin,
|
|
100
|
+
);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const onMessage = (event: { data: unknown; source: unknown }): void => {
|
|
104
|
+
if (frame.contentWindow === null || event.source !== frame.contentWindow) return;
|
|
105
|
+
if (isSandboxProxyReady(event.data)) deliver();
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const { events } = config;
|
|
109
|
+
if (events !== undefined) {
|
|
110
|
+
events.addEventListener("message", onMessage);
|
|
111
|
+
return () => events.removeEventListener("message", onMessage);
|
|
112
|
+
}
|
|
113
|
+
const domListener = (event: MessageEvent): void => onMessage(event);
|
|
114
|
+
window.addEventListener("message", domListener);
|
|
115
|
+
return () => window.removeEventListener("message", domListener);
|
|
116
|
+
}
|