mcp-use TypeScript API
    Preparing search index...

    Function useViewState

    • Persist model-visible state for the current mounted view.

      Every component in one view runtime reads and updates the same state object. ChatGPT restores the object from window.openai.widgetState.modelContent. Other MCP Apps hosts keep it for the iframe lifetime and receive it through ui/update-model-context.

      The state must be a JSON-serializable object and cannot contain the reserved _uiContext key used by ModelContext.

      Type Parameters

      • T extends Record<string, unknown>

      Parameters

      • defaultState: T | (() => T)

        Initial object, or a lazy initializer evaluated once for this hook instance. Restored ChatGPT state takes precedence.

      Returns readonly [T, (state: SetStateAction<T>) => void]

      A readonly [state, setState] tuple with useState ergonomics.

      const [state, setState] = useViewState({ count: 0 });
      setState((previous) => ({ ...previous, count: previous.count + 1 }));