:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
/**
* Sandbox proxy HTML for MCP Apps.
*
* This is the intermediate layer in the double-iframe architecture:
*
* Host (renderer) → Outer iframe (sandbox proxy) → Inner iframe (MCP App)
*
* The host sandboxes the outer iframe without allow-same-origin, so both frames
* get opaque origins: the app cannot reach the host's DOM, storage or cookies,
* nor another app's frame.
*
* The HTML string itself is portable browser JavaScript with no host APIs; the
* protocol that serves it is the host-specific seam.
*
* @see https://modelcontextprotocol.io/specification/2025-03-26/extensions/mcp-apps
*/
export const sandboxProxyHtml: string = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
iframe {
border: none;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<script>
function log(...args) {
console.log("[mcp-sandbox-proxy]", ...args);
}
function init() {
"use strict";
log("Proxy starting", { origin: location.origin, href: location.href });
var inner = document.createElement("iframe");
inner.style.cssText = "width:100%; height:100%; border:none;";
inner.setAttribute("sandbox", "allow-scripts allow-forms");
document.body.appendChild(inner);
// contentWindow survives navigation, so an app that calls
// location.replace() would keep the bridge. Only the first load of our
// srcdoc may use it; any later one is the app navigating away.
var appLoaded = false;
var bridgeClosed = false;
function onInnerLoad() {
if (!appLoaded) {
appLoaded = true;
return;
}
bridgeClosed = true;
log("Inner frame navigated, bridge closed");
}
// Build Permission Policy allow attribute from permissions object.
// Maps McpUiResourcePermissions keys to Permission Policy feature names.
// Uses "*" suffix for cross-origin permission delegation.
function buildAllowAttribute(permissions) {
if (!permissions || typeof permissions !== "object") return "";
var mapping = {
camera: "camera",
microphone: "microphone",
geolocation: "geolocation",
clipboardWrite: "clipboard-write"
};
var features = Object.keys(permissions)
.filter(function (key) { return mapping[key] && permissions[key] != null; })
.map(function (key) { return mapping[key] + " *"; });
return features.join("; ");
}
// Handle messages from the host (parent)
function handleParentMessage(data) {
if (data && data.method === "ui/notifications/sandbox-resource-ready") {
var params = data.params;
sent = true; // Stop retrying sandbox-proxy-ready
log("Received sandbox-resource-ready", {
hasHtml: !!(params && params.html),
htmlLength: params && params.html ? params.html.length : 0,
hasPermissions: !!(params && params.permissions),
hasCsp: !!(params && params.csp)
});
if (params && typeof params.html === "string") {
// Set allow attribute for Permission Policy features
var allowValue = buildAllowAttribute(params.permissions);
if (allowValue) {
inner.setAttribute("allow", allowValue);
}
log("Setting inner iframe srcdoc", {
htmlLength: params.html.length
});
inner.addEventListener("load", onInnerLoad);
inner.setAttribute("srcdoc", params.html);
log("HTML handed to inner iframe");
}
} else {
// Forward all other messages to inner iframe
log("Forwarding host -> inner", {
method: data.method,
id: data.id,
hasInner: !!(inner && inner.contentWindow)
});
// An opaque origin serializes to "null", which postMessage rejects.
if (!bridgeClosed && inner && inner.contentWindow) {
inner.contentWindow.postMessage(data, "*");
}
}
}
// Listen for messages from host (parent) and inner iframe
window.addEventListener("message", function (event) {
var data = event.data;
if (!data || typeof data !== "object") return;
if (event.source === window.parent) {
log("Message from host", {
method: data.method,
id: data.id,
origin: event.origin
});
handleParentMessage(data);
} else if (event.source === inner.contentWindow) {
if (bridgeClosed) {
log("Dropping message from navigated inner frame", {
method: data.method
});
return;
}
log("Relaying inner -> host", {
method: data.method,
id: data.id,
origin: event.origin
});
// Relay messages from inner iframe back to host
window.parent.postMessage(data, "*");
} else {
log("Message from unknown source", {
method: data.method,
origin: event.origin,
isParent: event.source === window.parent,
isInner: event.source === inner.contentWindow
});
}
});
// Notify host that proxy is ready to receive HTML.
// Retry a few times because the host's useEffect listener may not be
// registered yet when src= loads faster than React's effect cycle.
var readyMsg = {
jsonrpc: "2.0",
method: "ui/notifications/sandbox-proxy-ready",
params: {}
};
var sent = false;
function sendReady() {
if (sent) return;
log("Sending sandbox-proxy-ready to host");
window.parent.postMessage(readyMsg, "*");
}
// The host removes its listener on first receipt, so retries are harmless
sendReady();
setTimeout(sendReady, 50);
setTimeout(sendReady, 150);
};
// Fire and forget, similar to IIFE
init();
</script>
</body>
</html>`;