diff --git a/client/src/hooks/MCP/__tests__/useAppBridge.spec.tsx b/client/src/hooks/MCP/__tests__/useAppBridge.spec.tsx index f9309ef759..43e7fec640 100644 --- a/client/src/hooks/MCP/__tests__/useAppBridge.spec.tsx +++ b/client/src/hooks/MCP/__tests__/useAppBridge.spec.tsx @@ -274,6 +274,7 @@ describe('useAppBridge', () => { client = new QueryClient({ defaultOptions: { queries: { retry: false } } }); document.body.innerHTML = ''; document.documentElement.className = ''; + document.documentElement.removeAttribute('style'); }); describe('ordering', () => { @@ -1464,6 +1465,50 @@ describe('useAppBridge', () => { measure.mockRestore(); }); + it('supplies CSS colors on initialization and theme changes', async () => { + document.documentElement.style.setProperty('--surface-primary', '255 255 255'); + document.documentElement.style.setProperty('--text-primary', '33 33 33'); + document.documentElement.style.setProperty('--border-medium', '205 205 205 / 50%'); + document.documentElement.style.setProperty('--text-warning', '#f90'); + document.documentElement.style.setProperty('--surface-secondary', 'var(--missing)'); + mountBridge(makeResource(), client); + await flush(); + + expect(AppBridge.mock.calls.at(-1)?.[3]).toEqual({ + hostContext: expect.objectContaining({ + theme: 'light', + styles: { + variables: { + '--color-background-primary': 'rgb(255 255 255)', + '--color-text-primary': 'rgb(33 33 33)', + '--color-border-primary': 'rgb(205 205 205 / 50%)', + '--color-text-warning': '#f90', + }, + }, + }), + }); + + await act(async () => { + document.documentElement.style.setProperty('--surface-primary', '33 33 33'); + document.documentElement.style.setProperty('--text-primary', '255 255 255'); + document.documentElement.style.removeProperty('--border-medium'); + document.documentElement.classList.add('dark'); + await Promise.resolve(); + }); + expect(latest().hostContextChanges).toEqual([ + expect.objectContaining({ + theme: 'dark', + styles: { + variables: { + '--color-background-primary': 'rgb(33 33 33)', + '--color-text-primary': 'rgb(255 255 255)', + '--color-text-warning': '#f90', + }, + }, + }), + ]); + }); + it('pushes theme changes to a live bridge', async () => { mountBridge(makeResource(), client); await flush(); diff --git a/client/src/hooks/MCP/useAppBridge.ts b/client/src/hooks/MCP/useAppBridge.ts index 4ec76a8ce9..1ce01fa511 100644 --- a/client/src/hooks/MCP/useAppBridge.ts +++ b/client/src/hooks/MCP/useAppBridge.ts @@ -1,4 +1,5 @@ import { useEffect, useId, useRef } from 'react'; +import { readThemeColor } from '@librechat/client'; import { useQueryClient } from '@tanstack/react-query'; import { JSONRPCMessageSchema } from '@modelcontextprotocol/sdk/types.js'; import { QueryKeys, DEFAULT_MCP_APP_ACTION_PREVIEW_CHARS } from 'librechat-data-provider'; @@ -138,10 +139,9 @@ function readHostTheme(): 'light' | 'dark' { } function buildHostStyleVariables(): McpUiStyles { - const computed = getComputedStyle(document.documentElement); const variables: Partial> = {}; for (const [specVar, lcVar] of Object.entries(HOST_STYLE_VAR_MAP)) { - const value = computed.getPropertyValue(lcVar).trim(); + const value = readThemeColor(lcVar); if (value) { variables[specVar as McpUiStyleVariableKey] = value; }