From 00498d3febf7b45755c348318ef11d9573ab0fae Mon Sep 17 00:00:00 2001 From: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Date: Fri, 16 Oct 2026 21:31:07 -0700 Subject: [PATCH] perf(web): the MCP app frame compiles without manual memoization Co-Authored-By: Claude Opus 5.5 --- apps/web/src/components/chat/McpAppFrame.tsx | 40 +++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/apps/web/src/components/chat/McpAppFrame.tsx b/apps/web/src/components/chat/McpAppFrame.tsx index 7a385a6faba97..cf35d44d0d5a3 100644 --- a/apps/web/src/components/chat/McpAppFrame.tsx +++ b/apps/web/src/components/chat/McpAppFrame.tsx @@ -19,7 +19,7 @@ import { type McpAppReference, } from "@t3tools/shared/mcpApp"; import { Minimize2Icon } from "lucide-react"; -import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; +import { useEffect, useLayoutEffect, useRef, useState } from "react"; import { flushSync } from "react-dom"; import { useAssetUrlRefresh, useAssetUrlState } from "~/assets/assetUrls"; @@ -60,6 +60,18 @@ function saveBlob(blob: Blob, name: string) { setTimeout(() => URL.revokeObjectURL(url), 60_000); } +// The host gets the tool call pushed by an effect keyed on it, so it stays +// stable across the command hooks that follow. +function useToolCall( + storedItem: NonNullable["data"]>["item"] | undefined, +) { + "use memo"; + if (storedItem?.type !== "dynamic_tool") return undefined; + const output = storedItem.output as { readonly result?: unknown } | undefined; + const result = output?.result as McpAppCallToolResult | undefined; + return { arguments: storedItem.input, result }; +} + export function McpAppFrame(props: { readonly environmentId: EnvironmentId; readonly threadId: ThreadId; @@ -91,17 +103,14 @@ export function McpAppFrame(props: { // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Closing and reopening replaces the box. }, [closed]); - const resource = useMemo( - () => ({ - _tag: "attachment" as const, - attachmentId: app.attachmentId, - fileName: mcpAppFileName(app), - mimeType: "text/html", - disposition: "inline" as const, - renderIntent: "mcp-app" as const, - }), - [app], - ); + const resource = { + _tag: "attachment" as const, + attachmentId: app.attachmentId, + fileName: mcpAppFileName(app), + mimeType: "text/html", + disposition: "inline" as const, + renderIntent: "mcp-app" as const, + }; const asset = useAssetUrlState(props.environmentId, resource); const refreshAsset = useAssetUrlRefresh(props.environmentId, resource); // The frame keeps its first URL: a re-minted one would reload the app. A @@ -133,12 +142,7 @@ export function McpAppFrame(props: { revision: props.revision, }); const storedItem = detail.data?.item; - const toolCall = useMemo(() => { - if (storedItem?.type !== "dynamic_tool") return undefined; - const output = storedItem.output as { readonly result?: unknown } | undefined; - const result = output?.result as McpAppCallToolResult | undefined; - return { arguments: storedItem.input, result }; - }, [storedItem]); + const toolCall = useToolCall(storedItem); const callTool = useAtomCommand(mcpAppEnvironment.callTool, { reportFailure: false }); const toolInfo = useAtomCommand(mcpAppEnvironment.toolInfo, { reportFailure: false });