Skip to content

Commit 6c8379f

Browse files
Merge pull request modelcontextprotocol#376 from jonathanhefner/fix-code-snippets-sync
Fix `extractRegion()` matching region names that are substrings of others
2 parents f90e0bd + 1215bf3 commit 6c8379f

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

docs/patterns.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,46 @@ return {
345345

346346
<!-- prettier-ignore -->
347347
```tsx source="./patterns.tsx#persistData"
348-
// In your tool callback, include viewUUID in the result metadata.
349-
return {
350-
content: [{ type: "text", text: `Displaying PDF viewer for "${title}"` }],
351-
structuredContent: { url, title, pageCount, initialPage: 1 },
352-
_meta: {
353-
viewUUID: randomUUID(),
354-
},
348+
// Store the viewUUID received from the server
349+
let viewUUID: string | undefined;
350+
351+
// Helper to save state to localStorage
352+
function saveState<T>(state: T): void {
353+
if (!viewUUID) return;
354+
try {
355+
localStorage.setItem(viewUUID, JSON.stringify(state));
356+
} catch (err) {
357+
console.error("Failed to save view state:", err);
358+
}
359+
}
360+
361+
// Helper to load state from localStorage
362+
function loadState<T>(): T | null {
363+
if (!viewUUID) return null;
364+
try {
365+
const saved = localStorage.getItem(viewUUID);
366+
return saved ? (JSON.parse(saved) as T) : null;
367+
} catch (err) {
368+
console.error("Failed to load view state:", err);
369+
return null;
370+
}
371+
}
372+
373+
// Receive viewUUID from the tool result
374+
app.ontoolresult = (result) => {
375+
viewUUID = result._meta?.viewUUID
376+
? String(result._meta.viewUUID)
377+
: undefined;
378+
379+
// Restore any previously saved state
380+
const savedState = loadState<{ currentPage: number }>();
381+
if (savedState) {
382+
// Apply restored state to your UI...
383+
}
355384
};
385+
386+
// Call saveState() whenever your view state changes
387+
// e.g., saveState({ currentPage: 5 });
356388
```
357389

358390
_See [`examples/map-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/map-server) for a full implementation of this pattern._

scripts/sync-snippets.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,9 @@ function extractRegion(
254254
);
255255
}
256256

257-
const regionStart = `//#region ${regionName}`;
258-
const regionEnd = `//#endregion ${regionName}`;
257+
const lineEnding = exampleContent.includes("\r\n") ? "\r\n" : "\n";
258+
const regionStart = `//#region ${regionName}${lineEnding}`;
259+
const regionEnd = `//#endregion ${regionName}${lineEnding}`;
259260

260261
const startIndex = exampleContent.indexOf(regionStart);
261262
if (startIndex === -1) {

0 commit comments

Comments
 (0)