document.modelContext using the <mcp-iframe> custom element.
Prerequisites
- The parent page must have
@mcp-b/global(or anydocument.modelContextimplementation) installed. - The iframe page must also have
@mcp-b/globalinstalled and must register tools ondocument.modelContext.
Install the package
Add the custom element to the parent page
Replace a standard<iframe> with <mcp-iframe>. Set the id attribute to control the tool name prefix.
calculate in the iframe appears on the parent as my-app_calculate. This namespacing prevents collisions when multiple iframes register tools with the same name.
Understand namespacing
The prefix is built from the element’sid (or name attribute, or "iframe" as fallback) plus a separator character.
Tool and prompt names must match the MCP pattern
^[a-zA-Z0-9_-]{1,128}$. If the id contains invalid characters, they are sanitized to underscores automatically.
To change the separator character, set the prefix-separator attribute:
Configure the element
Standard iframe attributes (
sandbox, allow, width, height) are mirrored to the internal iframe.
Listen for events
Refresh tools after dynamic changes
If the iframe registers new tools after the initial connection, callrefresh() to re-sync:
Bridge multiple iframes
Use a distinctid for each <mcp-iframe> to keep tool names unique:
calc_add, calc_multiply, search_query, and search_filter.
Verify tools surface in the parent
Use the browser console to inspect registered tools:@mcp-b/global installed on the parent, you can also list tools through the standard API:
Handle cross-origin iframes
For iframes served from a different domain, settarget-origin explicitly:
@mcp-b/global is loaded with appropriate origin settings.
Related pages
- mcp-iframe Reference for the full API surface
- Transports Reference for the underlying
IframeParentTransportandIframeChildTransport - Transports and Bridges for how iframe bridging fits into the architecture
