The best way to control Claude Code, Codex and more from any computer or phone
fix(web,mobile): a late MCP app teardown no longer closes the app that replaced it (#30387)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
seth-dixon committed
Nov 14, 2026 at 19:29 UTC
6c7a87cbcbf4ed031e23a70dbc34db30003c0b23
3 files changed
+91
-7
apps/mobile/src/features/threads/McpAppWebView.tsx
+12
-6
@@ -264,6 +264,8 @@ export function ThreadMcpApp(props: {
264
);
265
}, [host, detailAtom]);
266
const userActive = useRef(false);
267
+ // Teardown completions check this so a replaced document's late callback is ignored.
268
+ const currentHost = useRef<McpAppHost | null>(null);
269
270
// Each WebView document gets its own host: the view is keyed by document,
271
// restart generation and URL, and a changed app re-attaches this callback.
@@ -399,6 +401,8 @@ export function ThreadMcpApp(props: {
401
}
402
if (mode === "fullscreen" && current.displayMode !== "fullscreen") {
403
await documentHost.teardown();
404
+ // A crash restart or reload during the wait already replaced this document.
405
+ if (currentHost.current !== documentHost) return "inline";
406
// Approval arrival or navigation during the wait requires reloading inline.
407
const navigation = latest.current.navigation;
408
if (latest.current.awaitingUser || !navigation.isFocused()) {
@@ -417,7 +421,7 @@ export function ThreadMcpApp(props: {
421
}
422
if (mode === "inline" && current.displayMode === "fullscreen") {
423
await documentHost.teardown();
420
- current.onExitFullscreen?.();
424
+ if (currentHost.current === documentHost) current.onExitFullscreen?.();
425
return "inline";
426
}
427
return current.displayMode ?? "inline";
@@ -460,11 +464,11 @@ export function ThreadMcpApp(props: {
464
},
465
onRequestTeardown: () => {
466
const current = latest.current.props;
463
- if (current.displayMode === "fullscreen") {
464
- void documentHost.teardown().then(() => current.onExitFullscreen?.());
465
- } else {
466
- void documentHost.teardown().then(() => setClosed(true));
467
- }
467
+ void documentHost.teardown().then(() => {
468
+ if (currentHost.current !== documentHost) return;
469
+ if (current.displayMode === "fullscreen") current.onExitFullscreen?.();
470
+ else setClosed(true);
471
+ });
472
},
473
onSizeChanged: (size) => {
474
if (latest.current.props.displayMode === "fullscreen") return;
@@ -473,7 +477,9 @@ export function ThreadMcpApp(props: {
477
},
478
});
479
setHost(documentHost);
480
+ currentHost.current = documentHost;
481
return () => {
482
+ if (currentHost.current === documentHost) currentHost.current = null;
483
// An unmount cannot wait: the request goes out before the view does.
484
void documentHost.teardown();
485
setHost((current) => (current === documentHost ? null : current));
apps/web/src/components/chat/McpAppFrame.browser.test.tsx
new
+75
@@ -0,0 +1,75 @@
1
+import { EnvironmentId, ThreadId, TurnItemId } from "@t3tools/contracts";
2
+import { describe, expect, it, vi } from "vite-plus/test";
3
+import { userEvent } from "vite-plus/test/browser";
4
+
5
+import "~/index.css";
6
+import { act, fakeTimers, render } from "../../../test/render";
7
+
8
+// Asks to close twice when told to, and answers only the first teardown, so
9
+// the second one is left to finish on the host's timeout.
10
+const APP_HTML = `<script>
11
+let answered = false;
12
+addEventListener("message", (event) => {
13
+ const message = event.data;
14
+ if (message === "close") {
15
+ parent.postMessage({ jsonrpc: "2.0", method: "ui/notifications/request-teardown" }, "*");
16
+ parent.postMessage({ jsonrpc: "2.0", method: "ui/notifications/request-teardown" }, "*");
17
+ } else if (message?.method === "ui/resource-teardown" && !answered) {
18
+ answered = true;
19
+ parent.postMessage({ jsonrpc: "2.0", id: message.id, result: {} }, "*");
20
+ }
21
+});
22
+parent.postMessage({ jsonrpc: "2.0", method: "ui/notifications/initialized" }, "*");
23
+</script>`;
24
+
25
+vi.mock(import("~/assets/longLivedAssetUrls"), () => ({
26
+ useLongLivedAssetUrl: () => ({
27
+ _tag: "Success",
28
+ url: `data:text/html,${encodeURIComponent(APP_HTML)}`,
29
+ expiresAt: Date.now() + 60 * 60_000,
30
+ }),
31
+}));
32
+
33
+import { McpAppFrame } from "./McpAppFrame";
34
+
35
+const nextInitializedFrom = () =>
36
+ new Promise<MessageEventSource>((resolve) => {
37
+ const listen = (event: MessageEvent) => {
38
+ if (event.source === null || event.data?.method !== "ui/notifications/initialized") return;
39
+ window.removeEventListener("message", listen);
40
+ resolve(event.source);
41
+ };
42
+ window.addEventListener("message", listen);
43
+ });
44
+
45
+describe("McpAppFrame", () => {
46
+ it("keeps a reopened app when the old document's second teardown finishes late", async () => {
47
+ const firstInitialized = nextInitializedFrom();
48
+ await using screen = await render(
49
+ <McpAppFrame
50
+ environmentId={EnvironmentId.make("environment")}
51
+ threadId={ThreadId.make("thread")}
52
+ conversationThreadId={ThreadId.make("thread")}
53
+ itemId={TurnItemId.make("item")}
54
+ revision="1"
55
+ app={{
56
+ attachmentId: "attachment",
57
+ server: "Demo",
58
+ tool: "show",
59
+ resourceUri: "ui://demo/app",
60
+ }}
61
+ onSendMessage={undefined}
62
+ />,
63
+ );
64
+ const firstApp = await firstInitialized;
65
+ using _timers = fakeTimers();
66
+
67
+ (firstApp as Window).postMessage("close", "*");
68
+ await userEvent.click(screen.getByRole("button", { name: "Show app" }));
69
+ await expect.element(screen.getByTitle("Demo app")).toBeInTheDocument();
70
+
71
+ await act(() => vi.advanceTimersByTimeAsync(2_000));
72
+ expect(screen.getByRole("button", { name: "Show app" }).query()).toBeNull();
73
+ await expect.element(screen.getByTitle("Demo app")).toBeInTheDocument();
74
+ });
75
+});
apps/web/src/components/chat/McpAppFrame.tsx
+4
-1
@@ -380,7 +380,10 @@ export function McpAppFrame(props: {
380
setDisplayMode("inline");
381
return;
382
}
383
- void host.teardown().then(() => setClosed(true));
383
+ // A replacement document may have mounted while this one wound down.
384
+ void host.teardown().then(() => {
385
+ if (hostRef.current === host) setClosed(true);
386
+ });
387
},
388
onSizeChanged: (size) => {
389
// Ignore fullscreen height when restoring inline sizing.