Skip to content

Conversation

@antonpk1
Copy link
Collaborator

@antonpk1 antonpk1 commented Feb 3, 2026

Adds a simpler alternative to the viewUUID pattern in the "Persisting view state" section.

Instead of generating a UUID server-side and passing it via _meta.viewUUID, developers can use app.getHostContext()?.toolInfo.id — the JSON-RPC tool call ID — as the localStorage key directly.

When to use this over viewUUID:

  • No server-side changes needed
  • toolInfo.id is stable across page reloads (same tool call = same ID)
  • Simpler setup for basic state persistence

Adds both the patterns.md documentation and the type-checked patterns.tsx code sample.

Add a simpler alternative to the viewUUID pattern for persisting
view state. Uses hostContext.toolInfo.id (the JSON-RPC tool call ID)
as the localStorage key, avoiding server-side UUID generation.
/**
* Example: Persisting view state using tool call ID (no server-side UUID needed)
*/
function persistViewStateWithToolCallId(app: App) {
// Use the tool call ID as a stable localStorage key — no server-side UUID needed
const toolCallId = String(app.getHostContext()?.toolInfo?.id ?? "default");

function saveState<T>(state: T): void {
try { localStorage.setItem(toolCallId, JSON.stringify(state)); } catch {}
}

function loadState<T>(): T | null {
@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 3, 2026

Open in StackBlitz

@modelcontextprotocol/ext-apps

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/ext-apps@427

@modelcontextprotocol/server-basic-react

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-basic-react@427

@modelcontextprotocol/server-basic-vanillajs

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-basic-vanillajs@427

@modelcontextprotocol/server-budget-allocator

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-budget-allocator@427

@modelcontextprotocol/server-cohort-heatmap

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-cohort-heatmap@427

@modelcontextprotocol/server-customer-segmentation

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-customer-segmentation@427

@modelcontextprotocol/server-map

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-map@427

@modelcontextprotocol/server-pdf

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-pdf@427

@modelcontextprotocol/server-scenario-modeler

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-scenario-modeler@427

@modelcontextprotocol/server-shadertoy

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-shadertoy@427

@modelcontextprotocol/server-sheet-music

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-sheet-music@427

@modelcontextprotocol/server-system-monitor

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-system-monitor@427

@modelcontextprotocol/server-threejs

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-threejs@427

@modelcontextprotocol/server-transcript

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-transcript@427

@modelcontextprotocol/server-video-resource

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-video-resource@427

@modelcontextprotocol/server-wiki-explorer

npm i https://pkg.pr.new/modelcontextprotocol/ext-apps/@modelcontextprotocol/server-wiki-explorer@427

commit: 8c69306

@jonathanhefner
Copy link
Member

When to use this over viewUUID:

* No server-side changes needed
* `toolInfo.id` is stable across page reloads (same tool call = same ID)
* Simpler setup for basic state persistence

When would viewUUID be preferred?

const toolCallId = String(app.getHostContext()?.toolInfo?.id ?? "default");

Is there a risk that apps will break on some platforms if they rely on the presence of app.getHostContext()?.toolInfo?.id?

@connor4312
Copy link

Imo using localstorage for state persistence is not a good recommendation. Ido opened #417 which I think will be the way to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants