// ***************************************************************************** // Copyright (C) 2026 Ericsson and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import * as React from '@theia/core/shared/react'; export interface McpAppFrameProps { html: string; title?: string; } /** Maximum height the iframe can grow to (px). Consistent with the mermaid renderer viewport cap. */ const MAX_HEIGHT = 800; const CSP_META = ''; const RESIZE_SCRIPT = ``; /** * Builds the `srcDoc` for the sandboxed iframe by injecting a CSP meta tag and a resize-reporting script. * Exported for testability. */ export function buildSrcDoc(html: string): string { let doc = html; // Inject CSP meta tag into if present (tolerant of attributes and casing), otherwise prepend it. if (/]/i.test(doc)) { doc = doc.replace(/]/i, match => `${match}${CSP_META}`); } else if (doc.includes(']*>)/, `$1${CSP_META}`); } else { doc = `${CSP_META}${doc}`; } // Inject resize script before if present, otherwise append it. if (doc.includes('')) { doc = doc.replace('', `${RESIZE_SCRIPT}`); } else { doc = `${doc}${RESIZE_SCRIPT}`; } return doc; } export const McpAppFrame: React.FC = ({ html, title }) => { // eslint-disable-next-line no-null/no-null const iframeRef = React.useRef(null); const [height, setHeight] = React.useState(200); React.useEffect(() => { const handler = (event: MessageEvent) => { if (iframeRef.current?.contentWindow === event.source && event.data?.type === 'mcp-app-resize' && typeof event.data.height === 'number') { const h = event.data.height; if (Number.isFinite(h) && h > 0) { setHeight(Math.min(h, MAX_HEIGHT)); } } }; window.addEventListener('message', handler); return () => window.removeEventListener('message', handler); }, []); const srcDoc = buildSrcDoc(html); return (
{title &&
{title}
}