different-ai/openwork · warning

MCP_APP_SANDBOX_DOCUMENT_ERROR

MCP_APP_SANDBOX_DOCUMENT_ERROR

Error message

The sandbox iframe reported a document load error.

What it means

The MCP app sandbox iframe fired its native `error` event after an HTML resource had already been assigned (resourceAssigned === true). The sandbox notifies the host via a sandbox-diagnostic with code MCP_APP_SANDBOX_DOCUMENT_ERROR, meaning the srcdoc document failed at load time (e.g. blocked script, CSP violation, malformed HTML triggering a resource error).

Source

Thrown at apps/server/src/mcp-app-sandbox.ts:98

  const inner = document.createElement("iframe");
  inner.title = "MCP App view";
  inner.style.cssText = "display:block;width:100%;height:100%;border:0;background:transparent";
  inner.setAttribute("sandbox", "allow-scripts allow-same-origin");
  let resourceAssigned = false;
  inner.addEventListener("load", () => {
    if (!resourceAssigned) return;
    let readyState = null;
    let hasHtmlRoot = null;
    let scriptCount = null;
    try {
      readyState = inner.contentDocument?.readyState || null;
      hasHtmlRoot = Boolean(inner.contentDocument?.documentElement);
      scriptCount = inner.contentDocument?.scripts.length ?? null;
    } catch {}
    notifyHost("ui/notifications/sandbox-resource-loaded", { readyState, hasHtmlRoot, scriptCount });
  });
  inner.addEventListener("error", () => {
    if (resourceAssigned) notifyHost("ui/notifications/sandbox-diagnostic", { code: "MCP_APP_SANDBOX_DOCUMENT_ERROR", message: "The sandbox iframe reported a document load error." });
  });
  document.body.appendChild(inner);
  window.addEventListener("message", (event) => {
    if (event.source === window.parent) {
      if (event.origin !== hostOrigin) return;
      if (event.data?.method === "ui/notifications/sandbox-resource-ready") {
        const html = event.data?.params?.html;
        const sandbox = event.data?.params?.sandbox;
        if (typeof sandbox === "string" && /^(?:allow-scripts|allow-same-origin|\s)+$/.test(sandbox)) inner.setAttribute("sandbox", sandbox);
        if (typeof html !== "string") {
          notifyHost("ui/notifications/sandbox-diagnostic", { code: "MCP_APP_SANDBOX_RESOURCE_INVALID", message: "The sandbox received an invalid HTML resource payload." });
          return;
        }
        try {
          resourceAssigned = true;
          inner.srcdoc = html;
          notifyHost("ui/notifications/sandbox-resource-accepted");
        } catch {

View on GitHub (pinned to 2b7df46e8a)

Solutions

  1. Check the sandbox attribute includes allow-scripts when the app HTML executes JavaScript.
  2. Open the iframe/webview devtools console for the underlying CSP or script error details.
  3. Remove or inline external resource references that violate the sandbox's opaque origin.
  4. If the error is benign (e.g. favicon 404 surfacing as an error), filter it in the host's sandbox-diagnostic handler.

Example fix

// before
inner.setAttribute("sandbox", "allow-same-origin");
// after
inner.setAttribute("sandbox", "allow-scripts allow-same-origin");
Defensive patterns

Strategy: validation

Validate before calling

const needsScripts = /<script/i.test(appHtml);
const sandboxAttr = inner.getAttribute("sandbox") ?? "";
if (needsScripts && !sandboxAttr.includes("allow-scripts")) {
  inner.setAttribute("sandbox", (sandboxAttr + " allow-scripts").trim());
}

Try / catch

window.addEventListener("message", (e) => {
  if (e.data?.code === "MCP_APP_SANDBOX_DOCUMENT_ERROR") {
    console.warn("sandbox doc load error — check iframe console/CSP");
  }
});

Prevention

When it happens

Trigger: Setting inner.srcdoc to HTML whose load raises an iframe error event — scripts blocked by the sandbox attribute, CSP refusing inline scripts, or the document referencing resources that fail hard enough to error the frame.

Common situations: Sandbox attr lacking allow-scripts while the HTML includes <script>; server CSP (frame-src/CSP headers) conflicting with srcdoc; app HTML expecting same-origin assets unavailable in the sandboxed origin.

Related errors


AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01). Data as JSON: /api/errors/26ff53c9e7826150. Report an issue: GitHub.