Overview
onhostcontextchanged is a notification handler called whenever the host environment changes — for example, when the user toggles between light and dark themes, resizes the viewport, switches locale, or changes the display mode.
Before your callback is invoked, the updated fields are automatically merged into the internal context store. This means getHostContext() already returns the latest values by the time your handler runs. The params object contains only the fields that changed, not the full context.
Signature
Parameters
Partial context update containing only the fields that changed. See Core Types for the full
McpUiHostContext structure. Common fields include:The host’s current color scheme.
The current display mode (e.g.,
"default", "fullscreen").Display modes the host currently supports. Use this to show or hide UI controls like a fullscreen button.
The host’s locale (e.g.,
"en-US", "ja-JP").Usage
Respond to theme changes
Adapt to display mode and available modes
Combine with getHostContext for full state
Since the internal context is updated before your handler runs, you can read the complete context when you need fields beyond what changed:Related
- Event Handlers overview — all notification and request handlers
getHostContext()— read the full host context at any time- Core Types —
McpUiHostContexttype definition - Lifecycle — connection and initialization flow