You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(transport): add forHostIframe helper and improve init timeout message
- Add `PostMessageTransport.forHostIframe(iframe)` static method that
validates the iframe is connected and returns a transport bound to
its contentWindow. This makes the correct host construction order
(connect before srcdoc) a one-liner.
- Improve the View-side timeout message when `ui/initialize` fails:
"no response within Ns — host may have loaded the View before
connecting its transport"
- Add "Host Construction Order" section to quickstart docs explaining
the correct sequence: appendChild → transport → connect → srcdoc
Fixes#542
Made-with: Cursor
Copy file name to clipboardExpand all lines: docs/quickstart.md
+49Lines changed: 49 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -476,6 +476,55 @@ Open http://localhost:8080 in your browser:
476
476
477
477
You've built your first MCP App!
478
478
479
+
## Host Construction Order
480
+
481
+
When building your own host (instead of using an existing MCP client), the order of operations matters. The host must start listening for messages **before** the View begins executing, otherwise the View's `ui/initialize` request will be lost.
482
+
483
+
### Correct Order
484
+
485
+
1.**Create and attach the iframe** to the document
486
+
2.**Create the transport** using `PostMessageTransport.forHostIframe(iframe)`
487
+
3.**Connect the bridge** with `await bridge.connect(transport)`
488
+
4.**Then** set `iframe.srcdoc` or `iframe.src` to load the View
// NOW load the content — ui/initialize will be received
508
+
iframe.srcdoc=htmlContent;
509
+
```
510
+
511
+
The `iframe.contentWindow` reference is available as soon as the iframe is in the DOM (it points to the initial `about:blank` document). You do **not** need to wait for `onload` to create the transport.
const transport =PostMessageTransport.forHostIframe(iframe);
519
+
awaitbridge.connect(transport); // Too late — message was already lost
520
+
```
521
+
522
+
If you see a timeout error like:
523
+
524
+
> `ui/initialize: no response within 60s — host may have loaded the View before connecting its transport`
525
+
526
+
This is the likely cause. Reorder your code to connect the transport first.
527
+
479
528
## Next Steps
480
529
481
530
-**Continue learning**: The [`basic-server-vanillajs`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-vanillajs) example builds on this quickstart with host communication, theming, and lifecycle handlers
0 commit comments