@@ -15,6 +15,7 @@ import {
1515 mcpAppFileName ,
1616 type McpAppReference ,
1717} from "@t3tools/shared/mcpApp" ;
18+ import * as Predicate from "effect/Predicate" ;
1819import Constants from "expo-constants" ;
1920import { useEffect , useMemo , useRef , useState } from "react" ;
2021import { ActivityIndicator , Alert , Platform , View } from "react-native" ;
@@ -45,15 +46,24 @@ export function mcpAppRowHeight() {
4546// `event.source` of host replies are a real window, which the MCP Apps SDK
4647// requires. The outer page only relays: app → React Native, and host
4748// replies (injected as `__t3McpAppReceive(...)`) → app.
48- function outerDocument ( src : string , allow : string ) {
49+ //
50+ // Android exposes `ReactNativeWebView` to every frame, so a frame nested in
51+ // the app could post to React Native directly. The outer page therefore wraps
52+ // what it relays with a secret only it holds, and React Native drops anything
53+ // else. The bridge belongs to the captured document: once the app frame loads
54+ // a second time (it navigated itself), the outer page stops relaying and says
55+ // so, rather than handing a document T3 never served the bridge.
56+ function outerDocument ( src : string , allow : string , secret : string ) {
4957 const attribute = ( value : string ) =>
5058 value . replace ( / & / g, "&" ) . replace ( / " / g, """ ) . replace ( / < / g, "<" ) ;
5159 return `<!doctype html><html><head><meta name="viewport" content="width=device-width, initial-scale=1">
5260<style>html,body{margin:0;height:100%;background:transparent}iframe{border:0;display:block;width:100%;height:100%}</style></head>
5361<body><iframe id="app" sandbox="allow-scripts allow-forms" allow="${ attribute ( allow ) } " src="${ attribute ( src ) } "></iframe>
54- <script>(function(){var frame=document.getElementById("app");
55- window.addEventListener("message",function(e){if(e.source===frame.contentWindow)window.ReactNativeWebView.postMessage(JSON.stringify(e.data));});
56- window.__t3McpAppReceive=function(m){frame.contentWindow&&frame.contentWindow.postMessage(m,"*");};})();</script></body></html>` ;
62+ <script>(function(){var frame=document.getElementById("app"),secret=${ JSON . stringify ( secret ) } ,loads=0,live=true;
63+ var send=function(m){window.ReactNativeWebView.postMessage(JSON.stringify({secret:secret,message:m}));};
64+ frame.addEventListener("load",function(){loads+=1;if(loads>1&&live){live=false;send({t3:"navigated"});}});
65+ window.addEventListener("message",function(e){if(live&&e.source===frame.contentWindow)send(e.data);});
66+ window.__t3McpAppReceive=function(m){live&&frame.contentWindow&&frame.contentWindow.postMessage(m,"*");};})();</script></body></html>` ;
5767}
5868
5969const commandFailure = ( result : {
@@ -76,7 +86,10 @@ const confirm = (title: string, message: string, action: string) =>
7686/** A captured MCP App in the thread feed, hosted in a WebView over the MCP Apps bridge. */
7787export function ThreadMcpApp ( props : {
7888 readonly environmentId : EnvironmentId ;
89+ /** The thread that produced the app, which its requests run against. */
7990 readonly threadId : ThreadId ;
91+ /** The thread on screen, which approved app messages are sent to. */
92+ readonly conversationThreadId : ThreadId ;
8093 readonly itemId : TurnItemId ;
8194 readonly revision : string ;
8295 readonly app : McpAppReference ;
@@ -111,6 +124,9 @@ export function ThreadMcpApp(props: {
111124 const [ uri , setUri ] = useState < string | null > ( null ) ;
112125 if ( uri === null && asset . _tag === "Success" ) setUri ( asset . url ) ;
113126 const [ loaded , setLoaded ] = useState ( false ) ;
127+ const [ navigatedAway , setNavigatedAway ] = useState ( false ) ;
128+ // Minted per view, so only this view's outer page can speak for its app.
129+ const [ secret ] = useState ( uuidv4 ) ;
114130
115131 // The feed omits tool input and output; the app needs both.
116132 const detail = useEnvironmentQuery (
@@ -208,7 +224,7 @@ export function ThreadMcpApp(props: {
208224 // connection; the thread's own settings fill in when it sends.
209225 await enqueueThreadOutboxMessage ( {
210226 environmentId : latest . current . props . environmentId ,
211- threadId : latest . current . props . threadId ,
227+ threadId : latest . current . props . conversationThreadId ,
212228 messageId : MessageId . make ( uuidv4 ( ) ) ,
213229 commandId : CommandId . make ( uuidv4 ( ) ) ,
214230 text,
@@ -241,13 +257,21 @@ export function ThreadMcpApp(props: {
241257
242258 const source = useMemo (
243259 ( ) =>
244- uri === null ? null : { html : outerDocument ( uri , mcpAppAllowAttribute ( app . permissions ) ) } ,
245- [ uri , app . permissions ] ,
260+ uri === null
261+ ? null
262+ : { html : outerDocument ( uri , mcpAppAllowAttribute ( app . permissions ) , secret ) } ,
263+ [ uri , app . permissions , secret ] ,
246264 ) ;
247265
248266 return (
249267 < View style = { { height : MCP_APP_ROW_HEIGHT , marginBottom : ROW_BOTTOM_MARGIN } } >
250- { uri !== null ? (
268+ { navigatedAway ? (
269+ < View className = "flex-1 items-center justify-center" >
270+ < Text className = "text-sm text-foreground-muted" >
271+ The { app . server } app left its page and was stopped
272+ </ Text >
273+ </ View >
274+ ) : uri !== null ? (
251275 < WebView < object >
252276 ref = { webView }
253277 source = { source ! }
@@ -262,11 +286,20 @@ export function ThreadMcpApp(props: {
262286 setSupportMultipleWindows = { false }
263287 onLoadEnd = { ( ) => setLoaded ( true ) }
264288 onMessage = { ( event : WebViewMessageEvent ) => {
289+ let envelope : unknown ;
265290 try {
266- hostRef . current ?. receive ( JSON . parse ( event . nativeEvent . data ) ) ;
291+ envelope = JSON . parse ( event . nativeEvent . data ) ;
267292 } catch {
268- // Not JSON: not a bridge message.
293+ return ; // Not JSON: not a bridge message.
294+ }
295+ if ( ! Predicate . isObject ( envelope ) || envelope . secret !== secret ) return ;
296+ const message = envelope . message ;
297+ if ( Predicate . isObject ( message ) && message . t3 === "navigated" ) {
298+ hostRef . current ?. dispose ( ) ;
299+ setNavigatedAway ( true ) ;
300+ return ;
269301 }
302+ hostRef . current ?. receive ( message ) ;
270303 } }
271304 />
272305 ) : asset . _tag === "Failure" ? (
0 commit comments