{"record":{"id":"b5f8245a15e7475f","repo":"different-ai/openwork","slug":"mcp-app-sandbox-resource-assignment-failed","errorCode":"MCP_APP_SANDBOX_RESOURCE_ASSIGNMENT_FAILED","errorMessage":"The sandbox could not assign the HTML resource to its isolated document.","messagePattern":"The sandbox could not assign the HTML resource to its isolated document\\.","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"apps/server/src/mcp-app-sandbox.ts","lineNumber":117,"sourceCode":"  });\n  document.body.appendChild(inner);\n  window.addEventListener(\"message\", (event) => {\n    if (event.source === window.parent) {\n      if (event.origin !== hostOrigin) return;\n      if (event.data?.method === \"ui/notifications/sandbox-resource-ready\") {\n        const html = event.data?.params?.html;\n        const sandbox = event.data?.params?.sandbox;\n        if (typeof sandbox === \"string\" && /^(?:allow-scripts|allow-same-origin|\\s)+$/.test(sandbox)) inner.setAttribute(\"sandbox\", sandbox);\n        if (typeof html !== \"string\") {\n          notifyHost(\"ui/notifications/sandbox-diagnostic\", { code: \"MCP_APP_SANDBOX_RESOURCE_INVALID\", message: \"The sandbox received an invalid HTML resource payload.\" });\n          return;\n        }\n        try {\n          resourceAssigned = true;\n          inner.srcdoc = html;\n          notifyHost(\"ui/notifications/sandbox-resource-accepted\");\n        } catch {\n          notifyHost(\"ui/notifications/sandbox-diagnostic\", { code: \"MCP_APP_SANDBOX_RESOURCE_ASSIGNMENT_FAILED\", message: \"The sandbox could not assign the HTML resource to its isolated document.\" });\n        }\n        return;\n      }\n      inner.contentWindow?.postMessage(event.data, \"*\");\n      return;\n    }\n    if (event.source === inner.contentWindow && event.origin === ownOrigin) {\n      window.parent.postMessage(event.data, hostTargetOrigin);\n    }\n  });\n  window.parent.postMessage({ jsonrpc: \"2.0\", method: \"ui/notifications/sandbox-proxy-ready\", params: {} }, hostTargetOrigin);\n})();\n`;\n\nexport const MCP_APP_SANDBOX_PROXY_HTML = \"<!doctype html><html><head><meta charset=\\\"utf-8\\\"><meta name=\\\"viewport\\\" content=\\\"width=device-width,initial-scale=1\\\"><link rel=\\\"stylesheet\\\" href=\\\"/mcp-apps/sandbox.css\\\"><title>MCP App sandbox</title></head><body><script src=\\\"/mcp-apps/sandbox.js\\\"></script></body></html>\";\nexport const MCP_APP_SANDBOX_PROXY_CSS = \"html,body{width:100%;height:100%;margin:0;overflow:hidden;background:transparent}\";\n","sourceCodeStart":99,"sourceCodeEnd":134,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/apps/server/src/mcp-app-sandbox.ts#L99-L134","documentation":"Assigning the HTML string to inner.srcdoc threw a JavaScript exception inside the sandbox's try block. The sandbox catches it and reports diagnostic MCP_APP_SANDBOX_RESOURCE_ASSIGNMENT_FAILED, meaning the isolated document could not receive the resource (assignment itself failed, distinct from a later load error).","triggerScenarios":"inner.srcdoc = html throwing — typically because the iframe was detached/removed from the DOM, the element is in a bad state, or a browser restriction blocks srcdoc assignment at that moment.","commonSituations":"Host navigated/reloaded and removed the iframe between posting the message and the sandbox handler running; the iframe element replaced during hot reload in dev; DOM teardown racing the postMessage handler.","solutions":["Confirm document.body still contains `inner` when the message arrives; re-append or recreate the iframe if it was removed.","Guard against teardown races: ignore sandbox-resource-ready messages after the sandbox is disposed.","Retry the srcdoc assignment once on the next tick in case of a transient DOM state.","In dev, disable hot-reload DOM replacement for the sandbox container or remount the whole sandbox after reload."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (!document.body.contains(inner)) { /* remount the sandbox before posting the resource */ }","typeGuard":null,"tryCatchPattern":"window.addEventListener(\"message\", (e) => {\n  if (e.data?.code === \"MCP_APP_SANDBOX_RESOURCE_ASSIGNMENT_FAILED\") {\n    remountSandbox(); // recreate iframe, then re-post the resource\n  }\n});","preventionTips":["Dispose the sandbox listener before removing the iframe to avoid teardown races","Check iframe connectivity (document.body.contains) before assigning srcdoc","Avoid hot-reload replacing the sandbox container without a full remount"],"tags":["iframe","sandbox","dom","race-condition","mcp-apps"],"backgroundTag":"dom-assignment-failed","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}