navigator.modelContext using the <mcp-iframe> custom element.
Prerequisites
- The parent page must have
@mcp-b/global(or anynavigator.modelContextimplementation) installed. - The iframe page must also have
@mcp-b/globalinstalled and must register tools onnavigator.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.
| Child tool name | Element id | Parent tool name |
|---|---|---|
calculate | my-app | my-app_calculate |
search | widget | widget_search |
greet | (none) | iframe_greet |
^[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
| Attribute | Description | Default |
|---|---|---|
src | URL of the iframe page | (required) |
id | Used as the tool name prefix | "iframe" |
target-origin | Override the postMessage target origin | Inferred from src |
channel | Channel ID for the transport | mcp-iframe |
call-timeout | Timeout in ms for tool calls | 30000 |
prefix-separator | Separator between prefix and tool name | _ |
sandbox, allow, width, height) are mirrored to the internal iframe.
Listen for events
| Event | Detail | When |
|---|---|---|
mcp-iframe-ready | { tools, resources, prompts } | Connected to the iframe’s MCP server |
mcp-iframe-error | { error } | Connection failed |
mcp-iframe-tools-changed | { tools, resources, prompts } | After calling refresh() |
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
