Skip to content

App class does not store hostContext from initialize response #129

@KushagraAgarwal525

Description

@KushagraAgarwal525

Summary

The App class receives hostContext in the McpUiInitializeResult response but does not store it internally, making initial host context inaccessible to apps after connection.

Current Behavior

The McpUiInitializeResult interface includes hostContext:
https://github.com/modelcontextprotocol/ext-apps/blob/main/src/spec.types.ts#L313-L327

export interface McpUiInitializeResult {
  protocolVersion: string;
  hostInfo: Implementation;
  hostCapabilities: McpUiHostCapabilities;
  hostContext: McpUiHostContext;  // Present in response
}

However, the connect method only stores hostCapabilities and hostInfo:
https://github.com/modelcontextprotocol/ext-apps/blob/main/src/app.ts#L879-L880

this._hostCapabilities = result.hostCapabilities;
this._hostInfo = result.hostInfo;
// hostContext is not stored

Impact

Apps cannot access initial host context after connection. This creates several issues:

  1. toolInfo is permanently inaccessible - The toolInfo field (containing the tool definition and request ID that triggered this app) is only sent in the initial response, never via the host-context-changed notification.
  2. Theme/layout flash - Apps may render with incorrect theme or layout initially before receiving the first host-context-changed notification.
  3. Missing initial viewport/locale - Apps cannot synchronously access viewport dimensions, locale, or timezone during first render.

Expected Behavior

The App class should store and expose initial hostContext:

class App extends Protocol<...> {
  private _hostContext?: McpUiHostContext;
  
  getHostContext(): McpUiHostContext | undefined {
    return this._hostContext;
  }
  
  async connect(transport: Transport, options?: RequestOptions): Promise<void> {
    // ...
    this._hostCapabilities = result.hostCapabilities;
    this._hostInfo = result.hostInfo;
    this._hostContext = result.hostContext;  // Add this
    // ...
  }
}

Proposed API

Add a getHostContext method parallel to existing getHostCapabilities and getHostVersion:

const app = new App({ name: "MyApp", version: "1.0.0" }, {});
await app.connect(transport);
const context = app.getHostContext();
console.log(context?.toolInfo?.tool.name);  // Which tool spawned this app
console.log(context?.theme);                 // Initial theme

Additional Consideration

The host-context-changed handler should merge updates into the stored context, so getHostContext always returns the current state:

app.onhostcontextchanged = (params) => {
  // Handler fires
};
// getHostContext returns merged initial + updates
const currentContext = app.getHostContext();

This would provide a unified way to access context state without apps needing to manually track and merge partial updates.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions