Fix onhostcontextchanged handler invocation in examples

`App.onhostcontextchanged` is a setter-only property with no getter.
TypeScript doesn't prevent reading from setter-only properties, so
`app.onhostcontextchanged?.(ctx)` compiles but silently returns
`undefined` at runtime.

Extract the handler into a named `handleHostContextChanged()` function
and call it directly after `connect()`, matching the pattern used in
`basic-server-vanillajs` and other examples.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Jonathan Hefner 2026-01-25 13:38:22 -06:00
commit def96b392a
No known key found for this signature in database
GPG key ID: 0DA40AFD21043691

View file

@ -68,7 +68,7 @@ app.onteardown = async () => {
app.onerror = log.error;
app.onhostcontextchanged = (ctx: McpUiHostContext) => {
function handleHostContextChanged(ctx: McpUiHostContext) {
if (ctx.safeAreaInsets) {
mainEl.style.paddingTop = `${ctx.safeAreaInsets.top}px`;
mainEl.style.paddingRight = `${ctx.safeAreaInsets.right}px`;
@ -78,7 +78,9 @@ app.onhostcontextchanged = (ctx: McpUiHostContext) => {
if (ctx.theme) {
applyDocumentTheme(ctx.theme);
}
};
}
app.onhostcontextchanged = handleHostContextChanged;
// ============================================================================
// Audio Capture
@ -546,6 +548,6 @@ app.connect().then(() => {
log.info("Connected to host");
const ctx = app.getHostContext();
if (ctx) {
app.onhostcontextchanged?.(ctx);
handleHostContextChanged(ctx);
}
});