Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions apps/mobile/src/features/threads/McpAppWebView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ export function ThreadMcpApp(props: {
if (presentedFullscreen) {
return (
<View
style={{ height, marginBottom: ROW_BOTTOM_MARGIN }}
style={{ height, minHeight: 80, marginBottom: ROW_BOTTOM_MARGIN }}
className="items-center justify-center rounded-lg border border-border"
>
<Text className="text-sm text-foreground-muted">
Expand All @@ -507,7 +507,7 @@ export function ThreadMcpApp(props: {
if (closed) {
return (
<View
style={{ height, marginBottom: ROW_BOTTOM_MARGIN }}
style={{ height, minHeight: 80, marginBottom: ROW_BOTTOM_MARGIN }}
className="items-center justify-center gap-2 rounded-lg border border-border"
>
<Text className="text-sm text-foreground-muted">The {app.server} app was closed</Text>
Expand All @@ -526,7 +526,11 @@ export function ThreadMcpApp(props: {
}

return (
<View style={fullscreen ? { flex: 1 } : { height, marginBottom: ROW_BOTTOM_MARGIN }}>
<View
style={
fullscreen ? { flex: 1 } : { height, marginBottom: height > 0 ? ROW_BOTTOM_MARGIN : 0 }
}
>
{navigatedAway ? (
<View className="flex-1 items-center justify-center">
<Text className="text-sm text-foreground-muted">
Expand Down
5 changes: 4 additions & 1 deletion apps/web/src/components/chat/McpAppFrame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,10 @@ export function McpAppFrame(props: {
data-mcp-app-fullscreen={fullscreen ? "" : undefined}
className={cn(
"relative size-full overflow-hidden",
app.prefersBorder === true && !fullscreen && "rounded-lg border border-border",
app.prefersBorder === true &&
!fullscreen &&
height > 0 &&
"rounded-lg border border-border",
fullscreen
? "fixed inset-0 m-0 flex h-dvh max-h-none w-dvw max-w-none flex-col border-0 bg-background p-0"
: "block! static! m-0 border-0 bg-transparent p-0 text-inherit",
Expand Down
40 changes: 38 additions & 2 deletions packages/shared/src/mcpApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
injectMcpAppCsp,
injectMcpAppThemeBootstrap,
injectMcpAppCookieBootstrap,
clampMcpAppHeight,
MCP_APP_COLOR_SCHEME_PARAM,
mcpAppContentSecurityPolicy,
mcpAppToolCallableByApp,
Expand Down Expand Up @@ -252,11 +253,15 @@ describe("mcpAppFromToolItem", () => {

describe("MCP App cookie bootstrap", () => {
const vendor = `<script>document.cookie.split(";");document.cookie="analytics=1";document.root="rendered";</script>`;
const execute = (html: string, document: object) => {
const execute = (html: string, document: object, self: object = {}) => {
for (const script of html.matchAll(/<script>([\s\S]*?)<\/script>/g)) {
NodeVM.runInNewContext(script[1]!, { document });
NodeVM.runInNewContext(script[1]!, { document, self });
}
};
const blockedCookieStore = (error: unknown) => ({
getAll: () => Promise.reject(error),
addEventListener: () => {},
});

it("lets cookie-reading startup code render in an opaque document without granting storage", () => {
const blocked = new DOMException("Opaque origin", "SecurityError");
Expand Down Expand Up @@ -290,6 +295,21 @@ describe("MCP App cookie bootstrap", () => {
expect(() => Reflect.get(prototype, "cookie")).toThrow(blocked);
});

it("gives the Cookie Store API an empty jar in an opaque document", async () => {
const blocked = new DOMException("Opaque origin", "SecurityError");
const native = blockedCookieStore(blocked);
const self: { origin: string; cookieStore: CookieStore } = Object.create({
cookieStore: native,
});
self.origin = "null";
execute(injectMcpAppCookieBootstrap(vendor), { cookie: "", root: "" }, self);
await expect(self.cookieStore.getAll()).resolves.toEqual([]);
await expect(self.cookieStore.get("session")).resolves.toBeNull();
await expect(self.cookieStore.set("session", "secret")).resolves.toBeUndefined();
await expect(self.cookieStore.getAll()).resolves.toEqual([]);
expect(() => self.cookieStore.addEventListener("change", () => {})).not.toThrow();
});

it("leaves other cookie failures unchanged", () => {
const failure = new TypeError("Unexpected cookie failure");
const document = Object.defineProperty({}, "cookie", {
Expand All @@ -300,4 +320,20 @@ describe("MCP App cookie bootstrap", () => {
expect(() => execute(injectMcpAppCookieBootstrap(vendor), document)).toThrow(failure);
expect(() => Reflect.get(document, "cookie")).toThrow(failure);
});

it("keeps a real origin's Cookie Store", () => {
const native = blockedCookieStore(new TypeError("Unexpected cookie failure"));
const self = { origin: "https://app.example", cookieStore: native };
execute(injectMcpAppCookieBootstrap(vendor), { cookie: "", root: "" }, self);
expect(self.cookieStore).toBe(native);
});
});

describe("clampMcpAppHeight", () => {
it("collapses an app that reports it has nothing to show", () => {
expect(clampMcpAppHeight(0)).toBe(0);
expect(clampMcpAppHeight(-5)).toBe(0);
expect(clampMcpAppHeight(12)).toBe(80);
expect(clampMcpAppHeight(Number.NaN)).toBe(320);
});
});
6 changes: 4 additions & 2 deletions packages/shared/src/mcpApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ export function injectMcpAppThemeBootstrap(html: string): string {
: `<!doctype html>${script}${html}`;
}

/** Opaque documents throw on cookie reads; analytics startup often expects an empty string instead. */
/** Opaque documents reject cookie access; analytics startup often expects an empty jar instead. */
export function injectMcpAppCookieBootstrap(html: string): string {
const bootstrap = `<script>(function(){try{void document.cookie;}catch(error){if(error&&error.name==="SecurityError"){Object.defineProperty(document,"cookie",{configurable:true,get:function(){return "";},set:function(){}});}}})();</script>`;
const bootstrap = `<script>(function(){try{void document.cookie;}catch(error){if(error&&error.name==="SecurityError"){Object.defineProperty(document,"cookie",{configurable:true,get:function(){return "";},set:function(){}});}}if(self.origin==="null"&&"cookieStore" in self){var none=function(){return Promise.resolve();};Object.defineProperty(self,"cookieStore",{configurable:true,value:{get:function(){return Promise.resolve(null);},getAll:function(){return Promise.resolve([]);},set:none,delete:none,addEventListener:function(){},removeEventListener:function(){},dispatchEvent:function(){return true;},onchange:null}});}})();</script>`;
// Preserve captured CSP ahead of executable code, including this bootstrap.
const prefix =
/^(?:\uFEFF?\s*<!doctype[^>]*>\s*)?(?:<meta http-equiv="Content-Security-Policy" content="[^"]*">)?/i.exec(
Expand All @@ -262,6 +262,8 @@ export function mcpAppAllowAttribute(permissions: McpAppPermissions | undefined)

export function clampMcpAppHeight(height: number): number {
if (!Number.isFinite(height)) return MCP_APP_DEFAULT_HEIGHT;
// Apps report 0 when a result has nothing to render; keep them mounted but invisible.
if (height <= 0) return 0;
return Math.round(Math.min(MCP_APP_MAX_HEIGHT, Math.max(MCP_APP_MIN_HEIGHT, height)));
}

Expand Down
Loading