Updating viewport type and documentation

This commit is contained in:
martinalong 2025-12-16 21:52:39 -08:00
commit 93b9da9e7a
No known key found for this signature in database
6 changed files with 342 additions and 180 deletions

View file

@ -461,13 +461,18 @@ interface HostContext {
displayMode?: "inline" | "fullscreen" | "pip"; displayMode?: "inline" | "fullscreen" | "pip";
/** Display modes the host supports */ /** Display modes the host supports */
availableDisplayModes?: string[]; availableDisplayModes?: string[];
/** Current and maximum dimensions available to the UI */ /**
viewport?: { * Viewport dimensions available to the UI.
width: number; *
height: number; * The viewport has two independent dimension pairs:
maxHeight?: number; * - Height: Either `height` (fixed) or `maxHeight` (flexible), never both
maxWidth?: number; * - Width: Either `width` (fixed) or `maxWidth` (flexible), never both
}; *
* Fixed dimensions (height/width): The host controls the size. Set height: 100% (recommended) or use the pixel value directly.
* Flexible dimensions (maxHeight/maxWidth or undefined): The app controls the size, up to the max if specified.
*/
viewport?: ({ height: number } | { maxHeight?: number }) &
({ width: number } | { maxWidth?: number });
/** User's language/region preference (BCP 47, e.g., "en-US") */ /** User's language/region preference (BCP 47, e.g., "en-US") */
locale?: string; locale?: string;
/** User's timezone (IANA, e.g., "America/New_York") */ /** User's timezone (IANA, e.g., "America/New_York") */
@ -516,12 +521,76 @@ Example:
} }
}, },
"displayMode": "inline", "displayMode": "inline",
"viewport": { "width": 400, "height": 300 } "viewport": { "width": 400, "maxHeight": 600 }
} }
} }
} }
``` ```
### Viewport and Sizing
The `viewport` field in `HostContext` communicates sizing constraints between host and app. Each dimension (height and width) operates independently and can be either **fixed** or **flexible**.
#### Viewport Modes
| Mode | Viewport Field | Meaning |
|------|---------------|---------|
| Fixed | `height` or `width` | Host controls the size. App should fill the available space. |
| Flexible | `maxHeight` or `maxWidth` | App controls the size, up to the specified maximum. |
| Unbounded | Field omitted | App controls the size with no limit. |
These modes can be combined independently. For example, a host might specify a fixed width but flexible height, allowing the app to grow vertically based on content.
#### App Behavior
Apps should check the viewport configuration and apply appropriate CSS:
```typescript
// In the app's initialization
const viewport = hostContext.viewport;
if (viewport) {
// Handle height
if ("height" in viewport) {
// Fixed height: fill the container
document.body.style.height = "100%";
} else if (viewport.maxHeight) {
// Flexible with max: let content determine size, up to max
document.body.style.maxHeight = `${viewport.maxHeight}px`;
}
// If neither, height is unbounded
// Handle width
if ("width" in viewport) {
// Fixed width: fill the container
document.body.style.width = "100%";
} else if (viewport.maxWidth) {
// Flexible with max: let content determine size, up to max
document.body.style.maxWidth = `${viewport.maxWidth}px`;
}
// If neither, width is unbounded
}
```
#### Host Behavior
When using flexible dimensions (no fixed `height` or `width`), hosts MUST listen for `ui/notifications/size-changed` notifications from the app and update the iframe dimensions accordingly:
```typescript
// Host listens for size changes from the app
bridge.onsizechange = ({ width, height }) => {
// Update iframe to match app's content size
if (width != null) {
iframe.style.width = `${width}px`;
}
if (height != null) {
iframe.style.height = `${height}px`;
}
};
```
Apps using the SDK automatically send size-changed notifications via ResizeObserver when `autoResize` is enabled (the default). The notifications are debounced and only sent when dimensions actually change.
### Theming ### Theming
Hosts can optionally pass CSS custom properties via `HostContext.styles.variables` for visual cohesion with the host environment. Hosts can optionally pass CSS custom properties via `HostContext.styles.variables` for visual cohesion with the host environment.

View file

@ -113,7 +113,7 @@ describe("App <-> AppBridge integration", () => {
const testHostContext = { const testHostContext = {
theme: "dark" as const, theme: "dark" as const,
locale: "en-US", locale: "en-US",
viewport: { width: 800, height: 600 }, viewport: { width: 800, maxHeight: 600 },
}; };
const newBridge = new AppBridge( const newBridge = new AppBridge(
createMockClient() as Client, createMockClient() as Client,
@ -337,7 +337,7 @@ describe("App <-> AppBridge integration", () => {
const initialContext = { const initialContext = {
theme: "light" as const, theme: "light" as const,
locale: "en-US", locale: "en-US",
viewport: { width: 800, height: 600 }, viewport: { width: 800, maxHeight: 600 },
}; };
const newBridge = new AppBridge( const newBridge = new AppBridge(
createMockClient() as Client, createMockClient() as Client,
@ -356,7 +356,7 @@ describe("App <-> AppBridge integration", () => {
// Send another partial update: only viewport changes // Send another partial update: only viewport changes
newBridge.sendHostContextChange({ newBridge.sendHostContextChange({
viewport: { width: 1024, height: 768 }, viewport: { width: 1024, maxHeight: 768 },
}); });
await flush(); await flush();
@ -367,7 +367,7 @@ describe("App <-> AppBridge integration", () => {
const context = newApp.getHostContext(); const context = newApp.getHostContext();
expect(context?.theme).toBe("dark"); expect(context?.theme).toBe("dark");
expect(context?.locale).toBe("en-US"); expect(context?.locale).toBe("en-US");
expect(context?.viewport).toEqual({ width: 1024, height: 768 }); expect(context?.viewport).toEqual({ width: 1024, maxHeight: 768 });
await newAppTransport.close(); await newAppTransport.close();
await newBridgeTransport.close(); await newBridgeTransport.close();

View file

@ -1008,7 +1008,7 @@ export class AppBridge extends Protocol<
* ```typescript * ```typescript
* bridge.setHostContext({ * bridge.setHostContext({
* theme: "dark", * theme: "dark",
* viewport: { width: 800, height: 600 } * viewport: { width: 800, maxHeight: 600 }
* }); * });
* ``` * ```
* *

View file

@ -629,29 +629,56 @@
} }
}, },
"viewport": { "viewport": {
"description": "Current and maximum dimensions available to the UI.", "description": "Viewport dimensions available to the UI.\n\nThe viewport has two independent dimension pairs:\n- Height: Either `height` (fixed) or `maxHeight` (flexible), never both\n- Width: Either `width` (fixed) or `maxWidth` (flexible), never both\n\nFixed dimensions (height/width): The host controls the size. Set height: 100% (recommended) or use the pixel value directly.\nFlexible dimensions (maxHeight/maxWidth or undefined): The app controls the size, up to the max if specified. If undefined, there is no limit.",
"allOf": [
{
"anyOf": [
{
"type": "object", "type": "object",
"properties": { "properties": {
"width": {
"description": "Current viewport width in pixels.",
"type": "number"
},
"height": { "height": {
"description": "Current viewport height in pixels.",
"type": "number"
},
"maxHeight": {
"description": "Maximum available height in pixels (if constrained).",
"type": "number"
},
"maxWidth": {
"description": "Maximum available width in pixels (if constrained).",
"type": "number" "type": "number"
} }
}, },
"required": ["width", "height"], "required": ["height"],
"additionalProperties": false "additionalProperties": false
}, },
{
"type": "object",
"properties": {
"maxHeight": {
"type": "number"
}
},
"additionalProperties": false
}
]
},
{
"anyOf": [
{
"type": "object",
"properties": {
"width": {
"type": "number"
}
},
"required": ["width"],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"maxWidth": {
"type": "number"
}
},
"additionalProperties": false
}
]
}
]
},
"locale": { "locale": {
"description": "User's language and region preference in BCP 47 format.", "description": "User's language and region preference in BCP 47 format.",
"type": "string" "type": "string"
@ -1255,29 +1282,56 @@
} }
}, },
"viewport": { "viewport": {
"description": "Current and maximum dimensions available to the UI.", "description": "Viewport dimensions available to the UI.\n\nThe viewport has two independent dimension pairs:\n- Height: Either `height` (fixed) or `maxHeight` (flexible), never both\n- Width: Either `width` (fixed) or `maxWidth` (flexible), never both\n\nFixed dimensions (height/width): The host controls the size. Set height: 100% (recommended) or use the pixel value directly.\nFlexible dimensions (maxHeight/maxWidth or undefined): The app controls the size, up to the max if specified. If undefined, there is no limit.",
"allOf": [
{
"anyOf": [
{
"type": "object", "type": "object",
"properties": { "properties": {
"width": {
"description": "Current viewport width in pixels.",
"type": "number"
},
"height": { "height": {
"description": "Current viewport height in pixels.",
"type": "number"
},
"maxHeight": {
"description": "Maximum available height in pixels (if constrained).",
"type": "number"
},
"maxWidth": {
"description": "Maximum available width in pixels (if constrained).",
"type": "number" "type": "number"
} }
}, },
"required": ["width", "height"], "required": ["height"],
"additionalProperties": false "additionalProperties": false
}, },
{
"type": "object",
"properties": {
"maxHeight": {
"type": "number"
}
},
"additionalProperties": false
}
]
},
{
"anyOf": [
{
"type": "object",
"properties": {
"width": {
"type": "number"
}
},
"required": ["width"],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"maxWidth": {
"type": "number"
}
},
"additionalProperties": false
}
]
}
]
},
"locale": { "locale": {
"description": "User's language and region preference in BCP 47 format.", "description": "User's language and region preference in BCP 47 format.",
"type": "string" "type": "string"
@ -2414,29 +2468,56 @@
} }
}, },
"viewport": { "viewport": {
"description": "Current and maximum dimensions available to the UI.", "description": "Viewport dimensions available to the UI.\n\nThe viewport has two independent dimension pairs:\n- Height: Either `height` (fixed) or `maxHeight` (flexible), never both\n- Width: Either `width` (fixed) or `maxWidth` (flexible), never both\n\nFixed dimensions (height/width): The host controls the size. Set height: 100% (recommended) or use the pixel value directly.\nFlexible dimensions (maxHeight/maxWidth or undefined): The app controls the size, up to the max if specified. If undefined, there is no limit.",
"allOf": [
{
"anyOf": [
{
"type": "object", "type": "object",
"properties": { "properties": {
"width": {
"description": "Current viewport width in pixels.",
"type": "number"
},
"height": { "height": {
"description": "Current viewport height in pixels.",
"type": "number"
},
"maxHeight": {
"description": "Maximum available height in pixels (if constrained).",
"type": "number"
},
"maxWidth": {
"description": "Maximum available width in pixels (if constrained).",
"type": "number" "type": "number"
} }
}, },
"required": ["width", "height"], "required": ["height"],
"additionalProperties": false "additionalProperties": false
}, },
{
"type": "object",
"properties": {
"maxHeight": {
"type": "number"
}
},
"additionalProperties": false
}
]
},
{
"anyOf": [
{
"type": "object",
"properties": {
"width": {
"type": "number"
}
},
"required": ["width"],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"maxWidth": {
"type": "number"
}
},
"additionalProperties": false
}
]
}
]
},
"locale": { "locale": {
"description": "User's language and region preference in BCP 47 format.", "description": "User's language and region preference in BCP 47 format.",
"type": "string" "type": "string"

View file

@ -532,8 +532,7 @@ export const McpUiToolResultNotificationSchema = z.object({
/** /**
* @description Rich context about the host environment provided to Guest UIs. * @description Rich context about the host environment provided to Guest UIs.
*/ */
export const McpUiHostContextSchema = z export const McpUiHostContextSchema = z.looseObject({
.object({
/** @description Metadata of the tool call that instantiated this App. */ /** @description Metadata of the tool call that instantiated this App. */
toolInfo: z toolInfo: z
.object({ .object({
@ -563,26 +562,39 @@ export const McpUiHostContextSchema = z
.array(z.string()) .array(z.string())
.optional() .optional()
.describe("Display modes the host supports."), .describe("Display modes the host supports."),
/** @description Current and maximum dimensions available to the UI. */ /**
* @description Viewport dimensions available to the UI.
*
* The viewport has two independent dimension pairs:
* - Height: Either `height` (fixed) or `maxHeight` (flexible), never both
* - Width: Either `width` (fixed) or `maxWidth` (flexible), never both
*
* Fixed dimensions (height/width): The host controls the size. Set height: 100% (recommended) or use the pixel value directly.
* Flexible dimensions (maxHeight/maxWidth or undefined): The app controls the size, up to the max if specified. If undefined, there is no limit.
*/
viewport: z viewport: z
.object({ .union([
/** @description Current viewport width in pixels. */ z.object({
width: z.number().describe("Current viewport width in pixels."), height: z.number(),
/** @description Current viewport height in pixels. */ }),
height: z.number().describe("Current viewport height in pixels."), z.object({
/** @description Maximum available height in pixels (if constrained). */ maxHeight: z.number().optional(),
maxHeight: z }),
.number() ])
.and(
z.union([
z.object({
width: z.number(),
}),
z.object({
maxWidth: z.number().optional(),
}),
]),
)
.optional() .optional()
.describe("Maximum available height in pixels (if constrained)."), .describe(
/** @description Maximum available width in pixels (if constrained). */ "Viewport dimensions available to the UI.\n\nThe viewport has two independent dimension pairs:\n- Height: Either `height` (fixed) or `maxHeight` (flexible), never both\n- Width: Either `width` (fixed) or `maxWidth` (flexible), never both\n\nFixed dimensions (height/width): The host controls the size. Set height: 100% (recommended) or use the pixel value directly.\nFlexible dimensions (maxHeight/maxWidth or undefined): The app controls the size, up to the max if specified. If undefined, there is no limit.",
maxWidth: z ),
.number()
.optional()
.describe("Maximum available width in pixels (if constrained)."),
})
.optional()
.describe("Current and maximum dimensions available to the UI."),
/** @description User's language and region preference in BCP 47 format. */ /** @description User's language and region preference in BCP 47 format. */
locale: z locale: z
.string() .string()
@ -627,8 +639,7 @@ export const McpUiHostContextSchema = z
}) })
.optional() .optional()
.describe("Mobile safe area boundaries in pixels."), .describe("Mobile safe area boundaries in pixels."),
}) });
.passthrough();
/** /**
* @description Notification that host context has changed (Host -> Guest UI). * @description Notification that host context has changed (Host -> Guest UI).

View file

@ -324,17 +324,18 @@ export interface McpUiHostContext {
displayMode?: McpUiDisplayMode; displayMode?: McpUiDisplayMode;
/** @description Display modes the host supports. */ /** @description Display modes the host supports. */
availableDisplayModes?: string[]; availableDisplayModes?: string[];
/** @description Current and maximum dimensions available to the UI. */ /**
viewport?: { * @description Viewport dimensions available to the UI.
/** @description Current viewport width in pixels. */ *
width: number; * The viewport has two independent dimension pairs:
/** @description Current viewport height in pixels. */ * - Height: Either `height` (fixed) or `maxHeight` (flexible), never both
height: number; * - Width: Either `width` (fixed) or `maxWidth` (flexible), never both
/** @description Maximum available height in pixels (if constrained). */ *
maxHeight?: number; * Fixed dimensions (height/width): The host controls the size. Set height: 100% (recommended) or use the pixel value directly.
/** @description Maximum available width in pixels (if constrained). */ * Flexible dimensions (maxHeight/maxWidth or undefined): The app controls the size, up to the max if specified. If undefined, there is no limit.
maxWidth?: number; */
}; viewport?: ({ height: number } | { maxHeight?: number }) &
({ width: number } | { maxWidth?: number });
/** @description User's language and region preference in BCP 47 format. */ /** @description User's language and region preference in BCP 47 format. */
locale?: string; locale?: string;
/** @description User's timezone in IANA format. */ /** @description User's timezone in IANA format. */