-
Notifications
You must be signed in to change notification settings - Fork 135
feat: add sandbox flags negotiation #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||
| import { RESOURCE_MIME_TYPE, getToolUiResourceUri, type McpUiSandboxProxyReadyNotification, AppBridge, PostMessageTransport, type McpUiResourceCsp, type McpUiResourcePermissions, buildAllowAttribute, type McpUiUpdateModelContextRequest, type McpUiMessageRequest } from "@modelcontextprotocol/ext-apps/app-bridge"; | ||||||
| import { RESOURCE_MIME_TYPE, getToolUiResourceUri, type McpUiSandboxProxyReadyNotification, AppBridge, PostMessageTransport, type McpUiResourceCsp, type McpUiResourcePermissions, type McpUiResourceSandbox, buildAllowAttribute, buildSandboxAttribute, type McpUiUpdateModelContextRequest, type McpUiMessageRequest } from "@modelcontextprotocol/ext-apps/app-bridge"; | ||||||
| import { Client } from "@modelcontextprotocol/sdk/client/index.js"; | ||||||
| import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js"; | ||||||
| import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; | ||||||
|
|
@@ -66,6 +66,7 @@ interface UiResourceData { | |||||
| html: string; | ||||||
| csp?: McpUiResourceCsp; | ||||||
| permissions?: McpUiResourcePermissions; | ||||||
| sandbox?: McpUiResourceSandbox; | ||||||
| } | ||||||
|
|
||||||
| export interface ToolCallInfo { | ||||||
|
|
@@ -136,20 +137,23 @@ async function getUiResource(serverInfo: ServerInfo, uri: string): Promise<UiRes | |||||
| const contentMeta = (content as any)._meta || (content as any).meta; | ||||||
| const csp = contentMeta?.ui?.csp; | ||||||
| const permissions = contentMeta?.ui?.permissions; | ||||||
| const sandbox = contentMeta?.ui?.sandbox; | ||||||
|
|
||||||
| return { html, csp, permissions }; | ||||||
| return { html, csp, permissions, sandbox }; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| export function loadSandboxProxy( | ||||||
| iframe: HTMLIFrameElement, | ||||||
| csp?: McpUiResourceCsp, | ||||||
| permissions?: McpUiResourcePermissions, | ||||||
| sandbox?: McpUiResourceSandbox, | ||||||
|
||||||
| ): Promise<boolean> { | ||||||
| // Prevent reload | ||||||
| if (iframe.src) return Promise.resolve(false); | ||||||
|
|
||||||
| iframe.setAttribute("sandbox", "allow-scripts allow-same-origin allow-forms"); | ||||||
| // Set sandbox attribute on outer iframe (must match inner iframe capabilities) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Let me know if you think this is helpful documentation... I suggest it in case some host wants to reserve the ability to have conditionally more restrictive Views (inner iframes) depending on some factors, so this documentation might help them understand the platform requirements / limits here. |
||||||
| iframe.setAttribute("sandbox", buildSandboxAttribute(sandbox)); | ||||||
idosal marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| // Set Permission Policy allow attribute based on requested permissions | ||||||
| const allowAttribute = buildAllowAttribute(permissions); | ||||||
|
|
@@ -199,10 +203,10 @@ export async function initializeApp( | |||||
| new PostMessageTransport(iframe.contentWindow!, iframe.contentWindow!), | ||||||
| ); | ||||||
|
|
||||||
| // Load inner iframe HTML with CSP and permissions metadata | ||||||
| const { html, csp, permissions } = await appResourcePromise; | ||||||
| log.info("Sending UI resource HTML to MCP App", csp ? `(CSP: ${JSON.stringify(csp)})` : "", permissions ? `(Permissions: ${JSON.stringify(permissions)})` : ""); | ||||||
| await appBridge.sendSandboxResourceReady({ html, csp, permissions }); | ||||||
| // Load inner iframe HTML with CSP, permissions, and sandbox metadata | ||||||
| const { html, csp, permissions, sandbox } = await appResourcePromise; | ||||||
| log.info("Sending UI resource HTML to MCP App", csp ? `(CSP: ${JSON.stringify(csp)})` : "", permissions ? `(Permissions: ${JSON.stringify(permissions)})` : "", sandbox ? `(Sandbox: ${JSON.stringify(sandbox)})` : ""); | ||||||
| await appBridge.sendSandboxResourceReady({ html, csp, permissions, sandbox }); | ||||||
|
|
||||||
| // Wait for inner iframe to be ready | ||||||
| log.info("Waiting for MCP App to initialize..."); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import type { McpUiSandboxProxyReadyNotification, McpUiSandboxResourceReadyNotification } from "../../../dist/src/types"; | ||
| import { buildAllowAttribute } from "../../../dist/src/app-bridge"; | ||
| import { buildAllowAttribute, buildSandboxAttribute } from "../../../dist/src/app-bridge"; | ||
|
|
||
| const ALLOWED_REFERRER_PATTERN = /^http:\/\/(localhost|127\.0\.0\.1)(:|\/|$)/; | ||
|
|
||
|
|
@@ -43,7 +43,7 @@ try { | |
| // origins. | ||
| const inner = document.createElement("iframe"); | ||
| inner.style = "width:100%; height:100%; border:none;"; | ||
| inner.setAttribute("sandbox", "allow-scripts allow-same-origin allow-forms"); | ||
| inner.setAttribute("sandbox", "allow-scripts allow-same-origin"); | ||
| // Note: allow attribute is set later when receiving sandbox-resource-ready notification | ||
| // based on the permissions requested by the app | ||
| document.body.appendChild(inner); | ||
|
|
@@ -85,9 +85,12 @@ window.addEventListener("message", async (event) => { | |
|
|
||
| if (event.data && event.data.method === RESOURCE_READY_NOTIFICATION) { | ||
| const { html, sandbox, permissions } = event.data.params; | ||
| if (typeof sandbox === "string") { | ||
| inner.setAttribute("sandbox", sandbox); | ||
| } | ||
| // sandbox can be a string (raw override) or object (structured flags) | ||
| const sandboxAttr = typeof sandbox === "string" | ||
| ? sandbox | ||
| : buildSandboxAttribute(sandbox); | ||
| console.log("[Sandbox] Setting sandbox attribute:", sandboxAttr); | ||
| inner.setAttribute("sandbox", sandboxAttr); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This concerns me because See #158 (comment). |
||
| // Set Permission Policy allow attribute if permissions are requested | ||
| const allowAttribute = buildAllowAttribute(permissions); | ||
| if (allowAttribute) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.