@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 516 lines 20.1 KB
Raw
1 import type { EnvironmentId, ThreadId, TurnItemId } from "@t3tools/contracts";
2 import {
3 makeMcpAppHost,
4 McpAppHostRefusal,
5 mcpAppStyleVariables,
6 mcpResourceBytes,
7 type McpAppCallToolResult,
8 type McpAppDisplayMode,
9 type McpAppHost,
10 type McpAppHostContext,
11 } from "@t3tools/client-runtime/mcp-apps";
12 import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
13 import {
14 clampMcpAppHeight,
15 MCP_APP_COLOR_SCHEME_PARAM,
16 MCP_APP_DEFAULT_HEIGHT,
17 MCP_APP_MAX_HEIGHT,
18 mcpAppAllowAttribute,
19 mcpAppFileName,
20 type McpAppReference,
21 } from "@t3tools/shared/mcpApp";
22 import { msg, T, useGT, Var } from "gt-react";
23 import { Minimize2Icon } from "lucide-react";
24 import { useEffect, useRef, useState, type FocusEvent, type ToggleEvent } from "react";
25 import { flushSync } from "react-dom";
26
27 import { useLongLivedAssetUrl } from "~/assets/longLivedAssetUrls";
28 import { APP_VERSION } from "~/branding";
29 import { isConfirmDialogActive, requestConfirmDialog } from "~/confirmDialog";
30 import { useHtmlRenderTheme } from "~/hooks/useHtmlRenderTheme";
31 import { useStableCallback } from "~/hooks/useStableCallback";
32 import { Button } from "~/components/ui/button";
33 import { isElectron } from "~/env";
34 import { m } from "~/i18n/messages";
35 import { cn } from "~/lib/utils";
36 import { useEnvironmentQuery } from "~/state/query";
37 import { useTurnItemDetail } from "~/state/queries";
38 import { mcpAppEnvironment } from "~/state/mcpApps";
39 import { useAtomCommand } from "~/state/use-atom-command";
40 import { observeResize } from "~/lib/observeResize";
41 import { useNativeTitlebarHeight } from "~/lib/windowControlsOverlay";
42
43 const commandFailure = (result: {
44 readonly cause: Parameters<typeof squashAtomCommandFailure>[0]["cause"];
45 }) => {
46 const error = squashAtomCommandFailure(result);
47 return new McpAppHostRefusal({
48 message:
49 error instanceof Error && error.message.trim() !== "" ? error.message : "Request failed.",
50 });
51 };
52
53 const fullscreenSupported =
54 typeof HTMLElement !== "undefined" && "popover" in HTMLElement.prototype;
55
56 const MAX_DOWNLOAD_BYTES = 25 * 1024 * 1024;
57 // Exclude activation left by input elsewhere in T3.
58 const PAGE_ACTIVATION_MS = 5_000;
59
60 // Frame input does not bubble here. An app can refocus itself, so focus
61 // alone cannot exclude activation from input elsewhere in T3.
62 let lastPageInputAt = Number.NEGATIVE_INFINITY;
63 if (typeof window !== "undefined") {
64 const note = () => {
65 lastPageInputAt = performance.now();
66 };
67 if (navigator.userActivation?.isActive) note();
68 for (const type of ["pointerdown", "pointerup", "touchend", "keydown"] as const) {
69 window.addEventListener(type, note, true);
70 }
71 }
72
73 function saveBlob(blob: Blob, name: string) {
74 const url = URL.createObjectURL(blob);
75 const link = document.createElement("a");
76 link.href = url;
77 link.download = name;
78 link.click();
79 // Browsers may start downloading after the click's task; defer revocation.
80 setTimeout(() => URL.revokeObjectURL(url), 60_000);
81 }
82
83 export function McpAppFrame(props: {
84 readonly environmentId: EnvironmentId;
85 readonly threadId: ThreadId;
86 readonly conversationThreadId: ThreadId;
87 readonly itemId: TurnItemId;
88 readonly revision: string;
89 readonly app: McpAppReference;
90 readonly onSendMessage: ((text: string) => Promise<void>) | undefined;
91 readonly awaitingUser?: boolean;
92 readonly onFullscreenChange?: (fullscreen: boolean) => void;
93 }) {
94 const { app } = props;
95 const gt = useGT();
96 const theme = useHtmlRenderTheme();
97 const nativeTitlebarHeight = useNativeTitlebarHeight();
98 const frameRef = useRef<HTMLIFrameElement>(null);
99 const boxRef = useRef<HTMLDivElement>(null);
100 const [width, setWidth] = useState(0);
101 const [height, setHeight] = useState(MCP_APP_DEFAULT_HEIGHT);
102 const [navigatedAway, setNavigatedAway] = useState(false);
103 const [displayMode, setDisplayMode] = useState<McpAppDisplayMode>("inline");
104 const [closed, setClosed] = useState(false);
105 const [documentGeneration, setDocumentGeneration] = useState(0);
106 // Closing and reopening replaces the box, which re-attaches this.
107 const attachBox = (box: HTMLDivElement) => {
108 boxRef.current = box;
109 setWidth(box.clientWidth);
110 const stopResize = observeResize(box, ([entry]) => {
111 if (entry) setWidth(Math.round(entry.contentRect.width));
112 // Full screen reports the viewport, which can change height alone.
113 if (fullscreenSupported && box.matches(":popover-open")) {
114 hostRef.current?.updateHostContext();
115 }
116 });
117 return () => {
118 stopResize();
119 boxRef.current = null;
120 };
121 };
122
123 const resource = {
124 _tag: "attachment" as const,
125 attachmentId: app.attachmentId,
126 fileName: mcpAppFileName(app),
127 mimeType: "text/html",
128 disposition: "inline" as const,
129 renderIntent: "mcp-app" as const,
130 };
131 // The frame keeps its first URL: a re-minted one would reload the app.
132 const [src, setSrc] = useState<string | null>(null);
133 const asset = useLongLivedAssetUrl(src === null ? props.environmentId : null, resource);
134 if (src === null && asset._tag === "Success") {
135 // The app paints before `ui/initialize`, so it reads the initial scheme from its URL.
136 const url = new URL(asset.url);
137 url.searchParams.set(MCP_APP_COLOR_SCHEME_PARAM, theme.appearance);
138 setSrc(url.toString());
139 }
140
141 const detail = useTurnItemDetail({
142 environmentId: props.environmentId,
143 threadId: props.threadId,
144 itemId: props.itemId,
145 revision: props.revision,
146 });
147 const storedItem = detail.data?.item;
148 // Keep tool-call identity stable for the host's delivery effect.
149 const toolCall =
150 storedItem?.type === "dynamic_tool"
151 ? {
152 arguments: storedItem.input,
153 result: (storedItem.output as { readonly result?: unknown } | undefined)?.result as
154 | McpAppCallToolResult
155 | undefined,
156 }
157 : undefined;
158
159 const callTool = useAtomCommand(mcpAppEnvironment.callTool, { reportFailure: false });
160 const toolInfo = useAtomCommand(mcpAppEnvironment.toolInfo, { reportFailure: false });
161 const toolDefinition = useEnvironmentQuery(
162 mcpAppEnvironment.toolInfoQuery({
163 environmentId: props.environmentId,
164 input: { threadId: props.threadId, itemId: props.itemId, name: app.tool },
165 }),
166 ).data?.tool;
167 const readResource = useAtomCommand(mcpAppEnvironment.readResource, { reportFailure: false });
168 const updateModelContext = useAtomCommand(mcpAppEnvironment.updateModelContext, {
169 reportFailure: false,
170 });
171
172 // Read live values without rebuilding the host and losing the app session.
173 const readLive = useStableCallback(() => ({
174 theme,
175 width,
176 props,
177 callTool,
178 toolInfo,
179 readResource,
180 updateModelContext,
181 displayMode,
182 toolDefinition,
183 }));
184 const hostRef = useRef<McpAppHost | null>(null);
185
186 const [stepAsideFor, setStepAsideFor] = useState(props.awaitingUser);
187 if (stepAsideFor !== props.awaitingUser) {
188 setStepAsideFor(props.awaitingUser);
189 if (props.awaitingUser === true) setDisplayMode("inline");
190 }
191 // The box is in the top layer exactly while its full-screen chrome is
192 // mounted, so every way out of full screen (including the step-aside above,
193 // decided during render) closes the popover and releases the timeline pin.
194 const attachFullscreenChrome = (chrome: HTMLDivElement) => {
195 const box = chrome.parentElement;
196 if (box === null) return;
197 if (!box.matches(":popover-open")) box.showPopover();
198 readLive().props.onFullscreenChange?.(true);
199 return () => {
200 if (box.matches(":popover-open")) box.hidePopover();
201 readLive().props.onFullscreenChange?.(false);
202 };
203 };
204 // Light dismiss (Escape) closes the popover without React.
205 const onBoxToggle = (event: ToggleEvent<HTMLDivElement>) => {
206 if (event.newState === "closed") setDisplayMode("inline");
207 };
208 const onBoxBlur = (event: FocusEvent<HTMLDivElement>) => {
209 const next = event.relatedTarget;
210 if (
211 displayMode === "fullscreen" &&
212 next instanceof Node &&
213 !event.currentTarget.contains(next)
214 ) {
215 setDisplayMode("inline");
216 }
217 };
218
219 // Navigation retains the frame window; reject second loads to prevent impersonation.
220 const loads = useRef({ generation: 0, count: 0 });
221 const onFrameLoad = () => {
222 if (loads.current.generation !== documentGeneration) {
223 loads.current = { generation: documentGeneration, count: 0 };
224 }
225 loads.current.count += 1;
226 if (loads.current.count > 1) {
227 hostRef.current?.dispose();
228 setNavigatedAway(true);
229 }
230 };
231
232 // The host belongs to this frame element, which is replaced when the document is reopened.
233 const attachFrame = (frame: HTMLIFrameElement) => {
234 frameRef.current = frame;
235 const hostContext = (): McpAppHostContext => {
236 const current = readLive();
237 return {
238 theme: current.theme.appearance,
239 styles: { variables: mcpAppStyleVariables(current.theme.variables) },
240 displayMode: current.displayMode,
241 availableDisplayModes: fullscreenSupported ? ["inline", "fullscreen"] : ["inline"],
242 containerDimensions:
243 current.displayMode === "fullscreen"
244 ? {
245 width: frameRef.current?.clientWidth || window.innerWidth,
246 height: frameRef.current?.clientHeight || window.innerHeight,
247 }
248 : { width: current.width, maxHeight: MCP_APP_MAX_HEIGHT },
249 platform: isElectron ? "desktop" : "web",
250 locale: navigator.language,
251 timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
252 userAgent: `t3-code/${APP_VERSION}`,
253 deviceCapabilities: {
254 touch: window.matchMedia("(pointer: coarse)").matches,
255 hover: window.matchMedia("(hover: hover)").matches,
256 },
257 safeAreaInsets: { top: 0, right: 0, bottom: 0, left: 0 },
258 ...(current.toolDefinition === undefined
259 ? {}
260 : { toolInfo: { tool: current.toolDefinition } }),
261 };
262 };
263 // Return inline so approvals stay visible above the app.
264 const ask = async (message: string) => {
265 flushSync(() => setDisplayMode("inline"));
266 const approved = await requestConfirmDialog(message);
267 if (approved === undefined) {
268 throw new McpAppHostRefusal({ message: "T3 could not ask for approval here." });
269 }
270 return approved;
271 };
272 const target = () => frameRef.current?.contentWindow ?? null;
273 const scope = () => {
274 const { environmentId, threadId, itemId } = readLive().props;
275 return { environmentId, input: { threadId, itemId } };
276 };
277 const host = makeMcpAppHost({
278 app,
279 hostVersion: APP_VERSION,
280 // Opaque origins require "*"; the window reference scopes delivery to this frame.
281 post: (message) => target()?.postMessage(message, "*"),
282 hostContext,
283 callTool: async ({ name, arguments: args }) => {
284 const { environmentId, input } = scope();
285 const info = await readLive().toolInfo({ environmentId, input: { ...input, name } });
286 if (info._tag !== "Success") throw commandFailure(info);
287 if (!info.value.callable)
288 throw new McpAppHostRefusal({ message: "This app cannot call that tool." });
289 if (!info.value.readOnly) {
290 const approved = await ask(
291 `${m(msg("Allow {server} to run {tool}?", { server: app.server, tool: info.value.title ?? name }))}\n${JSON.stringify(args, null, 2)}`,
292 );
293 if (approved !== true) throw new McpAppHostRefusal({ message: "Declined by the user." });
294 }
295 const result = await readLive().callTool({
296 environmentId,
297 input: { ...input, name, arguments: args },
298 });
299 if (result._tag !== "Success") throw commandFailure(result);
300 return result.value;
301 },
302 readResource: async ({ uri }) => {
303 const { environmentId, input } = scope();
304 const result = await readLive().readResource({
305 environmentId,
306 input: { ...input, uri },
307 });
308 if (result._tag !== "Success") throw commandFailure(result);
309 return result.value;
310 },
311 openLink: async (url) => {
312 if (document.activeElement !== frameRef.current || !navigator.userActivation?.isActive) {
313 throw new McpAppHostRefusal({ message: "Links open only from a click in the app." });
314 }
315 window.open(url, "_blank", "noopener,noreferrer");
316 },
317 sendMessage: async (text) => {
318 const send = readLive().props.onSendMessage;
319 if (send === undefined)
320 throw new McpAppHostRefusal({ message: "Messages are not available here." });
321 const approved = await ask(
322 `${m(msg("Send this message from {server}?", { server: app.server }))}\n${text}`,
323 );
324 if (approved !== true) throw new McpAppHostRefusal({ message: "Declined by the user." });
325 await send(text);
326 },
327 updateModelContext: async (context) => {
328 const { environmentId, input } = scope();
329 const result = await readLive().updateModelContext({
330 environmentId,
331 input: {
332 ...input,
333 ...context,
334 conversationThreadId: readLive().props.conversationThreadId,
335 },
336 });
337 if (result._tag !== "Success") throw commandFailure(result);
338 },
339 requestDisplayMode: async (mode) => {
340 const otherFullscreen = document.querySelector("[data-mcp-app-fullscreen]");
341 if (
342 mode === "fullscreen" &&
343 (!fullscreenSupported ||
344 document.activeElement !== frameRef.current ||
345 !navigator.userActivation?.isActive ||
346 performance.now() - lastPageInputAt < PAGE_ACTIVATION_MS ||
347 readLive().props.awaitingUser === true ||
348 isConfirmDialogActive() ||
349 (otherFullscreen !== null && otherFullscreen !== boxRef.current))
350 ) {
351 return readLive().displayMode;
352 }
353 setDisplayMode(mode);
354 return mode;
355 },
356 downloadFile: async (files) => {
357 const names = files.map((file) => file.name).join(", ");
358 const approved = await ask(
359 m(msg("Save {names} from {server}?", { names, server: app.server })),
360 );
361 if (approved !== true) throw new McpAppHostRefusal({ message: "Declined by the user." });
362 for (const file of files) {
363 let bytes: Uint8Array | undefined;
364 let mimeType = file.mimeType ?? "application/octet-stream";
365 if (file._tag === "embedded") {
366 bytes = file.bytes;
367 } else {
368 const { environmentId, input } = scope();
369 const read = await readLive().readResource({
370 environmentId,
371 input: { ...input, uri: file.uri },
372 });
373 if (read._tag !== "Success") throw commandFailure(read);
374 const content = read.value.contents[0];
375 bytes = mcpResourceBytes(content);
376 const declared = (content as { readonly mimeType?: unknown } | undefined)?.mimeType;
377 if (typeof declared === "string") mimeType = declared;
378 }
379 if (bytes === undefined)
380 throw new McpAppHostRefusal({ message: `${file.name} has no contents.` });
381 if (bytes.byteLength > MAX_DOWNLOAD_BYTES) {
382 throw new McpAppHostRefusal({ message: `${file.name} is too large to save.` });
383 }
384 saveBlob(new Blob([bytes.slice()], { type: mimeType }), file.name);
385 }
386 },
387 onRequestTeardown: () => {
388 if (readLive().displayMode === "fullscreen") {
389 setDisplayMode("inline");
390 return;
391 }
392 // A replacement document may have mounted while this one wound down.
393 void host.teardown().then(() => {
394 if (hostRef.current === host) setClosed(true);
395 });
396 },
397 onSizeChanged: (size) => {
398 // Ignore fullscreen height when restoring inline sizing.
399 if (readLive().displayMode === "fullscreen") return;
400 if (size.height !== undefined) setHeight(clampMcpAppHeight(size.height));
401 },
402 });
403 hostRef.current = host;
404 const receive = (event: MessageEvent) => {
405 if (event.source !== null && event.source === target()) host.receive(event.data);
406 };
407 window.addEventListener("message", receive);
408 return () => {
409 window.removeEventListener("message", receive);
410 void host.teardown();
411 hostRef.current = null;
412 frameRef.current = null;
413 };
414 };
415
416 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pushes theme, width, display mode and tool definition to the MCP app host in the iframe
417 useEffect(() => {
418 hostRef.current?.updateHostContext();
419 // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Context changes trigger a resend.
420 }, [theme, width, displayMode, toolDefinition]);
421
422 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- sends the current tool call to the MCP app host, again for each new iframe document
423 useEffect(() => {
424 if (toolCall !== undefined) hostRef.current?.setToolCall(toolCall);
425 // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Each new document needs the call.
426 }, [toolCall, src, documentGeneration]);
427
428 if (closed) {
429 return (
430 <div className="flex items-center justify-between gap-2 rounded-lg border border-border px-3 py-2 text-muted-foreground text-xs">
431 <span>
432 <T>
433 The <Var>{app.server}</Var> app was closed
434 </T>
435 </span>
436 <Button
437 size="xs"
438 variant="ghost"
439 onClick={() => {
440 setSrc(null);
441 setDocumentGeneration((value) => value + 1);
442 setClosed(false);
443 }}
444 >
445 <T>Show app</T>
446 </Button>
447 </div>
448 );
449 }
450
451 const fullscreen = displayMode === "fullscreen";
452 return (
453 // Preserve inline row height during fullscreen to prevent timeline jumps.
454 <div style={{ height }}>
455 <div
456 ref={attachBox}
457 // Preserve the frame's parent to avoid reloads; the top layer escapes timeline containment.
458 {...(fullscreenSupported
459 ? { popover: "auto" as const, onToggle: onBoxToggle, onBlur: onBoxBlur }
460 : {})}
461 data-mcp-app-fullscreen={fullscreen ? "" : undefined}
462 className={cn(
463 "relative size-full overflow-hidden",
464 app.prefersBorder === true && !fullscreen && "rounded-lg border border-border",
465 fullscreen
466 ? "fixed inset-0 m-0 flex h-dvh max-h-none w-dvw max-w-none flex-col border-0 bg-background p-0"
467 : "block! static! m-0 border-0 bg-transparent p-0 text-inherit",
468 )}
469 // Keep the header clear of the native window controls.
470 style={fullscreen ? { paddingTop: nativeTitlebarHeight } : undefined}
471 >
472 {fullscreen ? (
473 <div
474 ref={attachFullscreenChrome}
475 className="flex h-10 shrink-0 items-center justify-between border-border border-b px-3 text-sm"
476 >
477 <span className="truncate">{app.server}</span>
478 <Button
479 aria-label={gt("Exit full screen")}
480 size="icon-sm"
481 variant="ghost"
482 onClick={() => setDisplayMode("inline")}
483 >
484 <Minimize2Icon className="size-4" />
485 </Button>
486 </div>
487 ) : null}
488 {navigatedAway ? (
489 <p className="flex size-full items-center justify-center text-muted-foreground text-xs">
490 <T>
491 The <Var>{app.server}</Var> app left its page and was stopped
492 </T>
493 </p>
494 ) : src !== null ? (
495 <iframe
496 ref={attachFrame}
497 src={src}
498 title={gt("{server} app", { server: app.server })}
499 // Omitting allow-same-origin isolates the app from the session.
500 sandbox="allow-scripts allow-forms"
501 allow={mcpAppAllowAttribute(app.permissions)}
502 onLoad={onFrameLoad}
503 className={cn("block w-full border-0", fullscreen ? "min-h-0 flex-1" : "h-full")}
504 style={{ colorScheme: theme.appearance }}
505 />
506 ) : asset._tag === "Failure" ? (
507 <p className="flex size-full items-center justify-center text-muted-foreground text-xs">
508 <T>
509 Unable to load the <Var>{app.server}</Var> app
510 </T>
511 </p>
512 ) : null}
513 </div>
514 </div>
515 );
516 }