You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/patterns.md
+84-10Lines changed: 84 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ This document covers common patterns and recipes for building MCP Apps.
8
8
9
9
## Tools that are private to Apps
10
10
11
-
Set {@link types!McpUiToolMeta.visibility `Tool._meta.ui.visibility`} to `["app"]` to make tools only callable by Apps (hidden from the model). This is useful for UI-driven actions like updating quantities, toggling settings, or other interactions that shouldn't appear in the model's tool list.
11
+
Set {@link types!McpUiToolMeta.visibility `Tool._meta.ui.visibility`} to `["app"]` to make tools only callable by Apps (hidden from the model). This is useful for UI-driven actions like updating server-side state, polling, or other interactions that shouldn't appear in the model's tool list.
_See [`examples/system-monitor-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/system-monitor-server) for a full implementation of this pattern._
35
+
> [!NOTE]
36
+
> For full examples that implement this pattern, see: [`examples/system-monitor-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/system-monitor-server) and [`examples/pdf-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/pdf-server).
37
+
38
+
## Polling for live data
39
+
40
+
For real-time dashboards or monitoring views, use an app-only tool (with `visibility: ["app"]`) that the App polls at regular intervals.
41
+
42
+
**Vanilla JS:**
43
+
44
+
<!-- prettier-ignore -->
45
+
```ts source="./patterns.tsx#pollingVanillaJs"
46
+
let intervalId:number|null=null;
47
+
48
+
asyncfunction poll() {
49
+
const result =awaitapp.callServerTool({
50
+
name: "poll-data",
51
+
arguments: {},
52
+
});
53
+
updateUI(result.structuredContent);
54
+
}
55
+
56
+
function startPolling() {
57
+
if (intervalId!==null) return;
58
+
poll();
59
+
intervalId=window.setInterval(poll, 2000);
60
+
}
61
+
62
+
function stopPolling() {
63
+
if (intervalId===null) return;
64
+
clearInterval(intervalId);
65
+
intervalId=null;
66
+
}
67
+
68
+
// Clean up when host tears down the view
69
+
app.onteardown=async () => {
70
+
stopPolling();
71
+
return {};
72
+
};
73
+
```
74
+
75
+
**React:**
76
+
77
+
<!-- prettier-ignore -->
78
+
```tsx source="./patterns.tsx#pollingReact"
79
+
useEffect(() => {
80
+
if (!app) return;
81
+
let cancelled =false;
82
+
83
+
asyncfunction poll() {
84
+
const result =awaitapp!.callServerTool({
85
+
name: "poll-data",
86
+
arguments: {},
87
+
});
88
+
if (!cancelled) setData(result.structuredContent);
89
+
}
90
+
91
+
poll();
92
+
const id =setInterval(poll, 2000);
93
+
return () => {
94
+
cancelled=true;
95
+
clearInterval(id);
96
+
};
97
+
}, [app]);
98
+
```
99
+
100
+
> [!NOTE]
101
+
> For a full example that implements this pattern, see: [`examples/system-monitor-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/system-monitor-server).
36
102
37
103
## Reading large amounts of data via chunked tool calls
_See [`examples/pdf-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/pdf-server) for a full implementation of this pattern._
225
+
> [!NOTE]
226
+
> For a full example that implements this pattern, see: [`examples/pdf-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/pdf-server).
160
227
161
228
## Giving errors back to the model
162
229
@@ -279,7 +346,8 @@ function MyApp() {
279
346
}
280
347
```
281
348
282
-
_See [`examples/basic-server-vanillajs/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-vanillajs) and [`examples/basic-server-react/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-react) for full implementations of this pattern._
349
+
> [!NOTE]
350
+
> For full examples that implement this pattern, see: [`examples/basic-server-vanillajs/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-vanillajs) and [`examples/basic-server-react/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-react).
283
351
284
352
## Entering / exiting fullscreen
285
353
@@ -329,7 +397,8 @@ In fullscreen mode, remove the container's border radius so content extends to t
329
397
}
330
398
```
331
399
332
-
_See [`examples/shadertoy-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/shadertoy-server) for a full implementation of this pattern._
400
+
> [!NOTE]
401
+
> For full examples that implement this pattern, see: [`examples/shadertoy-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/shadertoy-server), [`examples/pdf-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/pdf-server), and [`examples/map-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/map-server).
333
402
334
403
## Passing contextual information from the App to the model
_See [`examples/map-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/map-server) for a full implementation of this pattern._
424
+
> [!NOTE]
425
+
> For full examples that implement this pattern, see: [`examples/map-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/map-server) and [`examples/transcript-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/transcript-server).
356
426
357
427
## Sending large follow-up messages
358
428
@@ -377,7 +447,8 @@ await app.sendMessage({
377
447
});
378
448
```
379
449
380
-
_See [`examples/transcript-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/transcript-server) for a full implementation of this pattern._
450
+
> [!NOTE]
451
+
> For a full example that implements this pattern, see: [`examples/transcript-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/transcript-server).
_See [`examples/map-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/map-server) for a full implementation of this pattern._
517
+
> [!NOTE]
518
+
> 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).
_See [`examples/shadertoy-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/shadertoy-server) for a full implementation of this pattern._
546
+
> [!NOTE]
547
+
> For full examples that implement this pattern, see: [`examples/shadertoy-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/shadertoy-server) and [`examples/threejs-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/threejs-server).
> Partial arguments are "healed" JSON — the host closes unclosed brackets/braces to produce valid JSON. This means objects may be incomplete (e.g., the last item in an array may be truncated). Don't rely on partial data for critical operations; use it only for preview UI.
500
573
501
-
_See [`examples/threejs-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/threejs-server) for a full implementation of this pattern._
574
+
> [!NOTE]
575
+
> For full examples that implement this pattern, see: [`examples/shadertoy-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/shadertoy-server) and [`examples/threejs-server/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/threejs-server).
0 commit comments