Create a new PostMessageTransport.
Target window to send messages to (default: window.parent)
OptionaleventSource: MessageEventSourceOptional source validation. If specified, only messages from this source will be accepted. Guest UIs typically don't need this (they only receive from parent), but hosts should validate the iframe source.
OptionaloncloseCalled when the transport is closed.
Set this handler to be notified when close is called.
OptionalonerrorCalled when a message parsing error occurs.
This handler is invoked when a received message fails JSON-RPC schema validation. The error parameter contains details about the validation failure.
Error describing the validation failure
OptionalonmessageCalled when a valid JSON-RPC message is received.
This handler is invoked after message validation succeeds. The start method must be called before messages will be received.
The validated JSON-RPC message
Optionalextra: MessageExtraInfoOptional metadata about the message (unused in this transport)
OptionalsessionOptional session identifier for this transport connection.
Set by the MCP SDK to track the connection session. Not required for PostMessageTransport functionality.
OptionalsetCallback to set the negotiated protocol version.
The MCP SDK calls this during initialization to communicate the protocol version negotiated with the peer.
The negotiated protocol version string
Stop listening for messages and cleanup.
Removes the message event listener and calls the onclose callback if set.
Send a JSON-RPC message to the target window.
Messages are sent using postMessage with "*" origin, meaning they are visible to all frames. The receiver should validate the message source for security.
JSON-RPC message to send
Optionaloptions: TransportSendOptionsOptional send options (currently unused)
Begin listening for messages from the event source.
Registers a message event listener on the window. Must be called before messages can be received.
JSON-RPC transport using window.postMessage for iframe↔parent communication.
This transport enables bidirectional communication between MCP Apps running in iframes and their host applications using the browser's postMessage API. It implements the MCP SDK's Transport interface.
Security
The
eventSourceparameter provides origin validation by filtering messages from specific sources. Guest UIs typically don't need to specify this (they only communicate with their parent), but hosts should validate the iframe source for security.Usage
Guest UI (default):
Host (with source validation):
See