Skip to content

Commit ccc7b2d

Browse files
fix(web): MCP App cards no longer flash white in dark mode while the app loads (#33352)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 4ca1039 commit ccc7b2d

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎apps/web/src/components/chat/McpAppFrame.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
1313
import {
1414
clampMcpAppHeight,
15+
MCP_APP_COLOR_SCHEME_PARAM,
1516
MCP_APP_DEFAULT_HEIGHT,
1617
MCP_APP_MAX_HEIGHT,
1718
mcpAppAllowAttribute,
@@ -125,7 +126,12 @@ export function McpAppFrame(props: {
125126
// The frame keeps its first URL: a re-minted one would reload the app.
126127
const [src, setSrc] = useState<string | null>(null);
127128
const asset = useLongLivedAssetUrl(src === null ? props.environmentId : null, resource);
128-
if (src === null && asset._tag === "Success") setSrc(asset.url);
129+
if (src === null && asset._tag === "Success") {
130+
// The app paints before `ui/initialize`, so it reads the initial scheme from its URL.
131+
const url = new URL(asset.url);
132+
url.searchParams.set(MCP_APP_COLOR_SCHEME_PARAM, theme.appearance);
133+
setSrc(url.toString());
134+
}
129135

130136
const detail = useTurnItemDetail({
131137
environmentId: props.environmentId,

‎packages/shared/src/mcpApp.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
injectMcpAppCsp,
66
injectMcpAppThemeBootstrap,
77
injectMcpAppCookieBootstrap,
8+
MCP_APP_COLOR_SCHEME_PARAM,
89
mcpAppContentSecurityPolicy,
910
mcpAppToolCallableByApp,
1011
mcpAppToolResourceUri,
@@ -101,6 +102,28 @@ describe("injectMcpAppThemeBootstrap", () => {
101102
send(initialized);
102103
expect(style.colorScheme).toBe("dark");
103104
});
105+
106+
it("applies only a valid scheme from the frame URL before any message arrives", () => {
107+
const html = injectMcpAppThemeBootstrap("<p>app</p>");
108+
const script = html.slice(html.indexOf("<script>") + 8, html.indexOf("</script>"));
109+
const initialScheme = (search: string) => {
110+
const head: Array<{ name?: string; content?: string }> = [];
111+
NodeVM.runInNewContext(script, {
112+
URLSearchParams,
113+
location: { search },
114+
window: { parent: {}, addEventListener: () => {} },
115+
document: {
116+
createElement: () => ({}),
117+
head: { append: (element: (typeof head)[number]) => head.push(element) },
118+
},
119+
});
120+
return head.find((element) => element.name === "color-scheme")?.content ?? "normal";
121+
};
122+
expect(initialScheme(`?a=1&${MCP_APP_COLOR_SCHEME_PARAM}=dark`)).toBe("dark");
123+
expect(initialScheme(`?${MCP_APP_COLOR_SCHEME_PARAM}=light`)).toBe("light");
124+
expect(initialScheme(`?${MCP_APP_COLOR_SCHEME_PARAM}=normal`)).toBe("normal");
125+
expect(initialScheme("")).toBe("normal");
126+
});
104127
});
105128

106129
describe("readMcpAppCsp", () => {

‎packages/shared/src/mcpApp.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export const MCP_APP_MIME_TYPE = "text/html;profile=mcp-app";
55
export const MCP_APP_EXTENSION_ID = "io.modelcontextprotocol/ui";
66
export const MCP_APP_RESOURCE_SCHEME = "ui://";
77
export const MCP_APP_OUTPUT_KEY = "t3McpApp";
8+
/** Frame URL query parameter carrying the host's color scheme before `ui/initialize`. */
9+
export const MCP_APP_COLOR_SCHEME_PARAM = "t3-color-scheme";
810

911
const MCP_APP_MIN_HEIGHT = 80;
1012
export const MCP_APP_DEFAULT_HEIGHT = 320;
@@ -201,6 +203,16 @@ export function injectMcpAppCsp(html: string, csp: McpAppCsp | undefined): strin
201203
export function injectMcpAppThemeBootstrap(html: string): string {
202204
const script = `<script>
203205
(() => {
206+
try {
207+
const initial = new URLSearchParams(location.search).get(${JSON.stringify(MCP_APP_COLOR_SCHEME_PARAM)});
208+
if (initial === "light" || initial === "dark") {
209+
// A meta tag, unlike an inline style, survives the app's own <html style> attribute.
210+
const meta = document.createElement("meta");
211+
meta.name = "color-scheme";
212+
meta.content = initial;
213+
document.head.append(meta);
214+
}
215+
} catch {}
204216
window.addEventListener("message", (event) => {
205217
if (event.source !== window.parent) return;
206218
const message = event.data;

0 commit comments

Comments
 (0)