Documentation Index
Fetch the complete documentation index at: https://docs.mcp-use.com/llms.txt
Use this file to discover all available pages before exploring further.
ModelContext and modelContext let your widget push contextual descriptions to the AI model so it can reason about what the user is currently seeing — without requiring an explicit tool call or storing it in developer-managed state.
Both APIs feed into a single hierarchical context string that is sent to the host via
ui/update-model-context (MCP Apps) or setWidgetState (ChatGPT Apps SDK) whenever the node tree changes. Updates are batched automatically.Import
<ModelContext> component
Declarative React component that registers a context annotation for as long as it is mounted. Participates in a parent-child tree based on nesting in JSX.
Props
| Prop | Type | Required | Description |
|---|---|---|---|
content | string | Yes | The description the model should see. |
children | ReactNode | No | Optional. If provided, this component becomes a scope boundary — any nested <ModelContext> components become children in the tree. |
Basic usage
Nesting
<ModelContext> components nest hierarchically. A component wrapped in another’s children becomes a child node in the serialized tree:
Siblings
Multiple<ModelContext> at the same level in the component tree are siblings — they produce a flat list at that depth:
Dynamic content
The component re-registers automatically whencontent changes. Use this to reflect interactive state:
Empty children
Children are optional. A self-closing tag registers a leaf annotation:modelContext API
Module-level imperative API. Works anywhere — event handlers, plain functions, outside React. Entries registered via this API are always root-level nodes (no parent in the tree).
Methods
| Method | Description |
|---|---|
modelContext.set(key, content) | Register or update a named context entry. The key is a stable identifier — calling set with the same key overwrites the previous value. |
modelContext.remove(key) | Remove a previously registered entry by key. |
modelContext.clear() | Remove all entries (both component-based and imperative). |
Usage
modelContext.set from a useEffect when you want lifecycle-aware imperative updates:
Unlike
<ModelContext>, entries set via modelContext.set() are not automatically cleaned up when a component unmounts. Always call modelContext.remove(key) in cleanup logic, or use <ModelContext> when you need automatic lifecycle management.How the tree is serialized
All mounted<ModelContext> nodes and modelContext.set() entries are collected in a global registry. On every change, the tree is serialized into an indented markdown-like string:
- MCP Apps:
ui/update-model-contextwithstructuredContent.__model_context - ChatGPT Apps SDK:
setWidgetStatewith__model_contextmerged into the state object
queueMicrotask, so rapid mount/unmount cycles within a single render produce a single host notification.
Integration with useWidget state
<ModelContext> and setState from useWidget are independent. Use them for different purposes:
| API | Purpose |
|---|---|
setState | Explicit developer-managed state (cart items, form values, filters). The developer decides what to include. |
<ModelContext> / modelContext.set() | Declarative annotations about what the user is currently seeing. Set it and forget it — the framework handles the rest. |
__model_context key is automatically filtered from the state value returned by useWidget, so it never appears in your code.
Full example
Related
useWidget()— Access widget props and state<McpUseProvider />— Widget root provider