document.modelContext. With MCP-B extensions on
the parent, it also republishes resources and prompts. Tool and prompt names
include the element’s id; resource URIs use an mcp-iframe: wrapper URI.
Installation
Install mcp-iframe
Child server requirement
The iframe must run an MCP server connected toIframeChildTransport. Merely
installing a document.modelContext implementation in the child does not expose
an MCP endpoint to the parent element.
@mcp-b/global supplies the server and selects IframeChildTransport
automatically when it runs in an iframe. Configure the parent origins before
importing it:
Child runtime configuration
IframeChildTransport requires a non-empty allowedOrigins list. The global
runtime defaults it to ['*'] when omitted; use exact parent origins in
production. A manually created MCP server can instead connect to
IframeChildTransport directly.
Native cross-document WebMCP does not require this package. It uses iframe
allow="tools",
child-side exposedTo, and parent-side getTools({fromOrigins}). Those browser controls are
independent of the MCP transport’s target-origin and allowedOrigins.Minimal example
Entry points
Attributes
Standard iframe attributes (
sandbox, allow, width, height, loading, referrerpolicy, name, srcdoc, allowfullscreen, credentialless) are mirrored to the internal <iframe> element.
For cross-origin children that use the native WebMCP API, set allow="tools"
to delegate that browser feature. The attribute does not establish or authorize
the MCP-B bridge.
Sandboxed iframes without allow-same-origin, and sources whose URL origin is null, have opaque origins. Set target-origin="*" for those sources. Other sources use the explicit attribute or the origin inferred from src.
Events
The
tools and prompts arrays contain parent-side names. The resources array contains parent-side wrapper URIs for static resources and resource templates.
Name prefixing
Tools and prompts from the iframe are registered on the parent as{id}{separator}{name}:
The parent model context performs final tool-name validation. A rejected registration emits
mcp-iframe-error and disconnects the element without leaving a partial registration set.
Bridged tools retain their title and strict WebMCP readOnlyHint annotation.
Resource URI wrapping
Resource URIs use themcp-iframe: scheme instead of name concatenation. A resource config://settings in an iframe with id="my-app" becomes mcp-iframe:?source=my-app_&uri=config%3A%2F%2Fsettings on the parent.
Resource templates use the same wrapper. Template expressions remain expressions, so config://users/{userId} can be listed and resolved through the parent.
Properties
Methods
If fetching or parent registration fails,
refresh() rejects after emitting mcp-iframe-error and disconnecting the element. It also rejects if the iframe connection changes before the refresh completes.
Programmatic registration
To register the custom element with a different tag name:Register a custom element name
/element entry point does not register <mcp-iframe>. The package root does.
Lifecycle
- On
connectedCallback, the element creates an internal<iframe>and mirrors attributes. - When the iframe fires its
loadevent, the element creates anIframeParentTransportand an MCPClient, then connects. - The element subscribes to each
list_changednotification advertised by the child. - Tools, resources, resource templates, and prompts are fetched without using cached list responses.
- Each item is registered on the parent’s
document.modelContext. Tool and prompt names use the element prefix, while resources use wrapper URIs. - The
mcp-iframe-readyevent fires. Later child list changes replace the parent registrations in order and firemcp-iframe-items-changed. - On
disconnectedCallback, notification handlers and registrations are removed, and the client and transport are closed.
src, srcdoc, target-origin, or channel while connected triggers a reconnection cycle.
Related
- Bridge tools across iframes for a step-by-step guide
- @mcp-b/transports for the underlying
IframeParentTransportandIframeChildTransport - @mcp-b/global for child transport configuration
- WebMCP standard API for native cross-document discovery
- Transports and bridges for the architectural context
