Skip to content

Commit e93f8a0

Browse files
Merge pull request modelcontextprotocol#397 from jonathanhefner/patterns-guide-server-side-view-state
Add server-side persistence guidance to view state pattern
2 parents ef46849 + e4f1a56 commit e93f8a0

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

docs/patterns.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ await app.sendMessage({
499499
500500
## Persisting view state
501501

502-
To persist view state across conversation reloads (e.g., current page in a PDF viewer, camera position in a map), use [`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) with a stable identifier provided by the server.
502+
For recoverable view state (e.g., current page in a PDF viewer, camera position in a map), use [`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) with a stable identifier provided by the server.
503503

504504
**Server-side**: Tool handler generates a unique `viewUUID` and returns it in `CallToolResult._meta.viewUUID`:
505505

@@ -561,8 +561,10 @@ app.ontoolresult = (result) => {
561561
// e.g., saveState({ currentPage: 5 });
562562
```
563563

564+
For state that represents user effort (e.g., saved bookmarks, annotations, custom configurations), consider persisting it server-side using [app-only tools](#tools-that-are-private-to-apps) instead. Pass the `viewUUID` to the app-only tool to scope the saved data to that view instance.
565+
564566
> [!NOTE]
565-
> For full examples that implement this pattern, see: [`examples/pdf-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/pdf-server) (persists current page) and [`examples/map-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/map-server) (persists camera position).
567+
> For full examples using `localStorage`, see: [`examples/pdf-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/pdf-server) (persists current page) and [`examples/map-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/map-server) (persists camera position).
566568
567569
## Pausing computation-heavy views when offscreen
568570

0 commit comments

Comments
 (0)