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
Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -89,6 +89,7 @@ It accepts the following props:
89
89
-**`style`**: Optional custom styles for the iframe
90
90
-**`iframeProps`**: Optional props passed to the iframe element
91
91
-**`iframeRenderData`**: Optional `Record<string, unknown>` to pass data to the iframe upon rendering. This enables advanced use cases where the parent application needs to provide initial state or configuration to the sandboxed iframe content.
92
+
-**`autoResizeIframe`**: Optional `boolean | { width?: boolean; height?: boolean }` to automatically resize the iframe to the size of the content.
92
93
-**`remoteDomProps`**: Optional props for the internal `<RemoteDOMResourceRenderer>`
93
94
-**`library`**: Optional component library for Remote DOM resources (defaults to `basicComponentLibrary`)
94
95
-**`remoteElements`**: remote element definitions for Remote DOM resources.
@@ -38,6 +40,7 @@ The component accepts the following props:
38
40
-**`style`**: (Optional) Custom styles for the iframe.
39
41
-**`proxy`**: (Optional) A URL to a proxy script. This is useful for hosts with a strict Content Security Policy (CSP). When provided, external URLs will be rendered in a nested iframe hosted at this URL. For example, if `proxy` is `https://my-proxy.com/`, the final URL will be `https://my-proxy.com/?url=<encoded_original_url>`. For your convenience, mcp-ui hosts a proxy script at `https://proxy.mcpui.dev`, which you can use as a the prop value without any setup (see `examples/external-url-demo`).
40
42
-**`iframeProps`**: (Optional) Custom props for the iframe.
43
+
-**`autoResizeIframe`**: (Optional) When enabled, the iframe will automatically resize based on messages from the iframe's content. This prop can be a boolean (to enable both width and height resizing) or an object (`{width?: boolean, height?: boolean}`) to control dimensions independently.
41
44
42
45
## How It Works
43
46
@@ -67,6 +70,38 @@ By default, the iframe stretches to 100% width and is at least 200px tall. You c
67
70
68
71
See [Client SDK Usage & Examples](./usage-examples.md) for examples using the recommended `<UIResourceRenderer />` component.
69
72
73
+
## Auto-Resizing the Iframe
74
+
75
+
To make the iframe auto-resize, two things need to happen:
76
+
1. The `autoResizeIframe` prop must be set in `htmlProps`when rendering `<UIResourceRenderer />`).
77
+
2. The content inside the iframe must send a `ui-size-change` message to the parent window when its size changes.
78
+
79
+
The payload of the message should be an object with `width` and/or `height` properties.
80
+
81
+
### Example Iframe Implementation
82
+
83
+
Here is an example of how you can use a `ResizeObserver` within your iframe's content to notify the host application of size changes:
This will observe the root `<html>` element and send a message whenever its height changes. The `<HTMLResourceRenderer />` will catch this message and adjust the iframe's height accordingly. You can also include `width` in the payload if you need to resize the width.
104
+
70
105
## Security Notes
71
106
72
107
-**`sandbox` attribute**: Restricts what the iframe can do. `allow-scripts` is required for JS execution. In the external URL content type, `allow-same-origin` is needed for external apps. Other than these inclusions, all other capabilities are restricted (e.g., no parent access, top-level navigations, modals, forms, etc.)
-**`iframeProps`**: Optional props passed to iframe elements (for HTML/URL resources)
52
52
-**`ref`**: Optional React ref to access the underlying iframe element
53
53
-**`iframeRenderData`**: Optional `Record<string, unknown>` to pass data to the iframe upon rendering. This enables advanced use cases where the parent application needs to provide initial state or configuration to the sandboxed iframe content.
54
+
-**`autoResizeIframe`**: Optional `boolean | { width?: boolean; height?: boolean }` to automatically resize the iframe to the size of the content.
54
55
-**`remoteDomProps`**: Optional props for the `<RemoteDOMResourceRenderer>`
55
56
-**`library`**: Optional component library for Remote DOM resources (defaults to `basicComponentLibrary`)
56
57
-**`remoteElements`**: Optional remote element definitions for Remote DOM resources. REQUIRED for Remote DOM snippets.
@@ -196,6 +197,49 @@ if (urlParams.get('waitForRenderData') === 'true') {
196
197
}
197
198
```
198
199
200
+
### Automatically Resizing the Iframe
201
+
202
+
The `autoResizeIframe` prop allows you to automatically resize the iframe to the size of the content.
203
+
204
+
```tsx
205
+
<UIResourceRenderer
206
+
resource={mcpResource.resource}
207
+
htmlProps={{
208
+
autoResizeIframe: true,
209
+
}}
210
+
onUIAction={handleUIAction}
211
+
/>
212
+
```
213
+
214
+
The `autoResizeIframe` prop can be a boolean or an object with the following properties:
215
+
216
+
-**`width`**: Optional boolean to automatically resize the iframe's width to the size of the content.
217
+
-**`height`**: Optional boolean to automatically resize the iframe's height to the size of the content.
218
+
219
+
If `autoResizeIframe` is a boolean, the iframe will be resized to the size of the content.
220
+
221
+
Inside the iframe, you can listen for the `ui-size-change` message and resize the iframe to the size of the content.
0 commit comments