fa-mcp-sdk 0.4.73 → 0.4.76
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli-template/.claude/skills/mcp-app-add-to-server/SKILL.md +427 -0
- package/cli-template/.claude/skills/mcp-app-create/SKILL.md +222 -0
- package/cli-template/FA-MCP-SDK-DOC/08-agent-tester-and-headless-api.md +681 -659
- package/cli-template/README.md +193 -191
- package/cli-template/package.json +1 -1
- package/cli-template/readme-docs/SKILLS.md +85 -0
- package/package.json +1 -1
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mcp-app-add-to-server
|
|
3
|
+
description: This skill should be used when the user asks to "add an app to my MCP server", "add UI to my MCP server", "add a view to my MCP tool", "enrich MCP tools with UI", "add interactive UI to existing server", "add MCP Apps to my server", or needs to add interactive UI capabilities to an existing MCP server that already has tools. Provides guidance for analyzing existing tools and adding MCP Apps UI resources.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Add UI to MCP Server
|
|
7
|
+
|
|
8
|
+
Enrich an existing MCP server's tools with interactive UIs using the MCP Apps SDK (`@modelcontextprotocol/ext-apps`).
|
|
9
|
+
|
|
10
|
+
## How It Works
|
|
11
|
+
|
|
12
|
+
Existing tools get paired with HTML resources that render inline in the host's conversation. The tool continues to work for text-only clients — UI is an enhancement, not a replacement. Each tool that benefits from UI gets linked to a resource via `_meta.ui.resourceUri`, and the host renders that resource in a sandboxed iframe when the tool is called.
|
|
13
|
+
|
|
14
|
+
## Getting Reference Code
|
|
15
|
+
|
|
16
|
+
Clone the MCP Apps SDK repository (`@modelcontextprotocol/ext-apps`) for working examples and API documentation:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
git clone --branch "v$(npm view @modelcontextprotocol/ext-apps version)" --depth 1 https://github.com/modelcontextprotocol/ext-apps.git ./mcp-ext-apps
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
> Add `mcp-ext-apps/` to the project's `.gitignore` — it's a reference clone, not part of the project.
|
|
23
|
+
|
|
24
|
+
### Protocol Specification
|
|
25
|
+
|
|
26
|
+
The formal MCP Apps spec lives in the same repository — read it for authoritative protocol
|
|
27
|
+
semantics that source files and examples only illustrate:
|
|
28
|
+
|
|
29
|
+
| File | Contents |
|
|
30
|
+
|------|----------|
|
|
31
|
+
| `./mcp-ext-apps/specification/2026-01-26/apps.mdx` | **SEP-1865** (Stable, 2026-01-26) — `ui://` URI scheme, `_meta.ui` contract, iframe sandboxing, host↔UI JSON-RPC messages, security model |
|
|
32
|
+
|
|
33
|
+
**Where to look first:**
|
|
34
|
+
|
|
35
|
+
- For **protocol questions** (wire format, required `_meta` keys, mandatory host/server behaviors,
|
|
36
|
+
message semantics) — consult `./mcp-ext-apps/specification/2026-01-26/apps.mdx`. The spec is the source of
|
|
37
|
+
truth for what MUST happen.
|
|
38
|
+
- For **TypeScript API questions** (which helper to call, handler signatures, idiomatic usage) —
|
|
39
|
+
consult `@modelcontextprotocol/ext-apps` sources under `./mcp-ext-apps/src/` and its examples.
|
|
40
|
+
The package is the source of truth for *how* to invoke the protocol from code.
|
|
41
|
+
|
|
42
|
+
### API Reference (Source Files)
|
|
43
|
+
|
|
44
|
+
Read JSDoc documentation directly from `./mcp-ext-apps/src/`:
|
|
45
|
+
|
|
46
|
+
| File | Contents |
|
|
47
|
+
|------|----------|
|
|
48
|
+
| `src/app.ts` | `App` class, handlers (`ontoolinput`, `ontoolresult`, `onhostcontextchanged`, `onteardown`), lifecycle |
|
|
49
|
+
| `src/server/index.ts` | `registerAppTool`, `registerAppResource`, `getUiCapability`, tool visibility options |
|
|
50
|
+
| `src/spec.types.ts` | All type definitions: `McpUiHostContext`, CSS variable keys, display modes |
|
|
51
|
+
| `src/styles.ts` | `applyDocumentTheme`, `applyHostStyleVariables`, `applyHostFonts` |
|
|
52
|
+
| `src/react/useApp.tsx` | `useApp` hook for React apps |
|
|
53
|
+
| `src/react/useHostStyles.ts` | `useHostStyles`, `useHostStyleVariables`, `useHostFonts` hooks |
|
|
54
|
+
|
|
55
|
+
### Key Examples (Mixed Tool Patterns)
|
|
56
|
+
|
|
57
|
+
These examples demonstrate servers with both App-enhanced and plain tools — the exact pattern you're adding:
|
|
58
|
+
|
|
59
|
+
| Example | Pattern |
|
|
60
|
+
|---------|---------|
|
|
61
|
+
| `examples/map-server/` | `show-map` (App tool) + `geocode` (plain tool) |
|
|
62
|
+
| `examples/pdf-server/` | `display_pdf` (App tool) + `list_pdfs` (plain tool) + `read_pdf_bytes` (app-only tool) |
|
|
63
|
+
| `examples/system-monitor-server/` | `get-system-info` (App tool) + `poll-system-stats` (app-only polling tool) |
|
|
64
|
+
|
|
65
|
+
### Domain-Specific Examples
|
|
66
|
+
|
|
67
|
+
When the new UI matches one of these domains, consult the corresponding example for patterns
|
|
68
|
+
the basic templates and mixed examples don't cover. All paths are under `./mcp-ext-apps/`:
|
|
69
|
+
|
|
70
|
+
| Domain | Example | What it shows |
|
|
71
|
+
|---|---|---|
|
|
72
|
+
| Charts / dashboards | `examples/scenario-modeler-server/` | Chart.js with structured React (`hooks/`, `lib/`, `components/`); multi-scenario comparison |
|
|
73
|
+
| Analytics drill-down | `examples/cohort-heatmap-server/` | Heatmap with hover tooltips and click drilldown (React) |
|
|
74
|
+
| 3D visualization | `examples/threejs-server/` | Three.js + streaming tool input into canvas, OrbitControls, post-processing |
|
|
75
|
+
| WebGL / shaders | `examples/shadertoy-server/` | GLSL live compilation, fullscreen mode, `vendor/` pattern for custom JS libs |
|
|
76
|
+
| Graph visualization | `examples/wiki-explorer-server/` | 3D force-directed graph (`force-graph`), web scraping with `cheerio` |
|
|
77
|
+
| Audio / music | `examples/sheet-music-server/` | ABC notation → SVG render + MIDI synthesis (`abcjs`) |
|
|
78
|
+
| Streaming + audio | `examples/say-server/` | `ontoolinputpartial` + async audio queue + multi-view lock (**Python FastMCP**) |
|
|
79
|
+
| Browser APIs | `examples/transcript-server/` | Web Speech API for live transcription |
|
|
80
|
+
| Binary / media resources | `examples/video-resource-server/` | Base64 video blobs, `ResourceTemplate`, large-payload limits |
|
|
81
|
+
| SDK surface reference | `examples/debug-server/` | All content types in one place — PNG/WAV blobs, structured output, stateful counter, resource downloads |
|
|
82
|
+
|
|
83
|
+
### Framework Templates
|
|
84
|
+
|
|
85
|
+
Learn and adapt from `./mcp-ext-apps/examples/basic-server-{framework}/`:
|
|
86
|
+
|
|
87
|
+
| Template | Key Files |
|
|
88
|
+
|----------|-----------|
|
|
89
|
+
| `basic-server-vanillajs/` | `server.ts`, `src/mcp-app.ts`, `mcp-app.html` |
|
|
90
|
+
| `basic-server-react/` | `server.ts`, `src/mcp-app.tsx` (uses `useApp` hook) |
|
|
91
|
+
| `basic-server-vue/` | `server.ts`, `src/App.vue` |
|
|
92
|
+
| `basic-server-svelte/` | `server.ts`, `src/App.svelte` |
|
|
93
|
+
| `basic-server-preact/` | `server.ts`, `src/mcp-app.tsx` |
|
|
94
|
+
| `basic-server-solid/` | `server.ts`, `src/mcp-app.tsx` |
|
|
95
|
+
|
|
96
|
+
## Step 1: Analyze Existing Tools
|
|
97
|
+
|
|
98
|
+
Before writing any code, analyze the server's existing tools and determine which ones benefit from UI.
|
|
99
|
+
|
|
100
|
+
1. Read the server source and list all registered tools
|
|
101
|
+
2. For each tool, assess whether it would benefit from UI (returns data that could be visualized, involves user interaction, etc.) vs. is fine as text-only (simple lookups, utility functions)
|
|
102
|
+
3. Identify tools that could become **app-only helpers** (data the UI needs to poll/fetch but the model doesn't need to call directly)
|
|
103
|
+
4. Present the analysis to the user and confirm which tools to enhance
|
|
104
|
+
|
|
105
|
+
### Decision Framework
|
|
106
|
+
|
|
107
|
+
| Tool output type | UI benefit | Example |
|
|
108
|
+
|---|---|---|
|
|
109
|
+
| Structured data / lists / tables | High — interactive table, search, filtering | List of items, search results |
|
|
110
|
+
| Metrics / numbers over time | High — charts, gauges, dashboards | System stats, analytics |
|
|
111
|
+
| Media / rich content | High — viewer, player, renderer | Maps, PDFs, images, video |
|
|
112
|
+
| Simple text / confirmations | Low — text is fine | "File created", "Setting updated" |
|
|
113
|
+
| Data for other tools | Consider app-only | Polling endpoints, chunk loaders |
|
|
114
|
+
|
|
115
|
+
## Step 2: Add Dependencies
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
npm install @modelcontextprotocol/ext-apps
|
|
119
|
+
npm install -D vite vite-plugin-singlefile
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Plus framework-specific dependencies if needed (e.g., `react`, `react-dom`, `@vitejs/plugin-react` for React).
|
|
123
|
+
|
|
124
|
+
Use `npm install` to add dependencies rather than manually writing version numbers. This lets npm resolve the latest compatible versions. Never specify version numbers from memory.
|
|
125
|
+
|
|
126
|
+
## Step 3: Set Up the Build Pipeline
|
|
127
|
+
|
|
128
|
+
### Vite Configuration
|
|
129
|
+
|
|
130
|
+
Create `vite.config.ts` with `vite-plugin-singlefile` to bundle the UI into a single HTML file:
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
import { defineConfig } from "vite";
|
|
134
|
+
import { viteSingleFile } from "vite-plugin-singlefile";
|
|
135
|
+
|
|
136
|
+
export default defineConfig({
|
|
137
|
+
plugins: [viteSingleFile()],
|
|
138
|
+
build: {
|
|
139
|
+
outDir: "dist",
|
|
140
|
+
rollupOptions: {
|
|
141
|
+
input: "mcp-app.html", // one per UI, or one shared entry
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### HTML Entry Point
|
|
148
|
+
|
|
149
|
+
Create `mcp-app.html` (or one per distinct UI if tools need different views):
|
|
150
|
+
|
|
151
|
+
```html
|
|
152
|
+
<!doctype html>
|
|
153
|
+
<html lang="en">
|
|
154
|
+
<head>
|
|
155
|
+
<meta charset="UTF-8" />
|
|
156
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
157
|
+
<title>MCP App</title>
|
|
158
|
+
</head>
|
|
159
|
+
<body>
|
|
160
|
+
<div id="root"></div>
|
|
161
|
+
<script type="module" src="./src/mcp-app.ts"></script>
|
|
162
|
+
</body>
|
|
163
|
+
</html>
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Build Scripts
|
|
167
|
+
|
|
168
|
+
Add build scripts to `package.json`. The UI must be built before the server code bundles it:
|
|
169
|
+
|
|
170
|
+
```json
|
|
171
|
+
{
|
|
172
|
+
"scripts": {
|
|
173
|
+
"build:ui": "vite build",
|
|
174
|
+
"build:server": "tsc",
|
|
175
|
+
"build": "npm run build:ui && npm run build:server",
|
|
176
|
+
"serve": "tsx server.ts"
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Step 4: Convert Tools to App Tools
|
|
182
|
+
|
|
183
|
+
Transform plain MCP tools into App tools with UI.
|
|
184
|
+
|
|
185
|
+
**Before** (plain MCP tool):
|
|
186
|
+
```typescript
|
|
187
|
+
server.tool("my-tool", { param: z.string() }, async (args) => {
|
|
188
|
+
const data = await fetchData(args.param);
|
|
189
|
+
return { content: [{ type: "text", text: JSON.stringify(data) }] };
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
**After** (App tool with UI):
|
|
194
|
+
```typescript
|
|
195
|
+
import { registerAppTool, registerAppResource, RESOURCE_MIME_TYPE } from "@modelcontextprotocol/ext-apps/server";
|
|
196
|
+
|
|
197
|
+
const resourceUri = "ui://my-tool/mcp-app.html";
|
|
198
|
+
|
|
199
|
+
registerAppTool(server, "my-tool", {
|
|
200
|
+
description: "Shows data with an interactive UI",
|
|
201
|
+
inputSchema: { param: z.string() },
|
|
202
|
+
_meta: { ui: { resourceUri } },
|
|
203
|
+
}, async (args) => {
|
|
204
|
+
const data = await fetchData(args.param);
|
|
205
|
+
return {
|
|
206
|
+
content: [{ type: "text", text: JSON.stringify(data) }], // text fallback for non-UI hosts
|
|
207
|
+
structuredContent: { data }, // structured data for the UI
|
|
208
|
+
};
|
|
209
|
+
});
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Key guidance:
|
|
213
|
+
- **Always keep the `content` array** with a text fallback for text-only clients
|
|
214
|
+
- Add `structuredContent` for data the UI needs to render
|
|
215
|
+
- Link the tool to its resource via `_meta.ui.resourceUri`
|
|
216
|
+
- Leave tools that don't benefit from UI unchanged — they stay as plain tools
|
|
217
|
+
|
|
218
|
+
## Step 5: Register Resources
|
|
219
|
+
|
|
220
|
+
Register the HTML resource so the host can fetch it:
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
import fs from "node:fs/promises";
|
|
224
|
+
import path from "node:path";
|
|
225
|
+
|
|
226
|
+
const resourceUri = "ui://my-tool/mcp-app.html";
|
|
227
|
+
|
|
228
|
+
registerAppResource(server, {
|
|
229
|
+
uri: resourceUri,
|
|
230
|
+
name: "My Tool UI",
|
|
231
|
+
mimeType: RESOURCE_MIME_TYPE,
|
|
232
|
+
}, async () => {
|
|
233
|
+
const html = await fs.readFile(
|
|
234
|
+
path.resolve(import.meta.dirname, "dist", "mcp-app.html"),
|
|
235
|
+
"utf-8",
|
|
236
|
+
);
|
|
237
|
+
return { contents: [{ uri: resourceUri, mimeType: RESOURCE_MIME_TYPE, text: html }] };
|
|
238
|
+
});
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
If multiple tools share the same UI, they can reference the same `resourceUri` and the same resource registration.
|
|
242
|
+
|
|
243
|
+
## Step 6: Build the UI
|
|
244
|
+
|
|
245
|
+
### Handler Registration
|
|
246
|
+
|
|
247
|
+
Register ALL handlers BEFORE calling `app.connect()`:
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
import { App, PostMessageTransport, applyDocumentTheme, applyHostStyleVariables, applyHostFonts } from "@modelcontextprotocol/ext-apps";
|
|
251
|
+
|
|
252
|
+
const app = new App({ name: "My App", version: "1.0.0" });
|
|
253
|
+
|
|
254
|
+
app.ontoolinput = (params) => {
|
|
255
|
+
// Render the UI using params.arguments and/or params.structuredContent
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
app.ontoolresult = (result) => {
|
|
259
|
+
// Update UI with final tool result
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
app.onhostcontextchanged = (ctx) => {
|
|
263
|
+
if (ctx.theme) applyDocumentTheme(ctx.theme);
|
|
264
|
+
if (ctx.styles?.variables) applyHostStyleVariables(ctx.styles.variables);
|
|
265
|
+
if (ctx.styles?.css?.fonts) applyHostFonts(ctx.styles.css.fonts);
|
|
266
|
+
if (ctx.safeAreaInsets) {
|
|
267
|
+
const { top, right, bottom, left } = ctx.safeAreaInsets;
|
|
268
|
+
document.body.style.padding = `${top}px ${right}px ${bottom}px ${left}px`;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
app.onteardown = async () => {
|
|
273
|
+
return {};
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
await app.connect(new PostMessageTransport());
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### Host Styling
|
|
280
|
+
|
|
281
|
+
Use host CSS variables for theme integration:
|
|
282
|
+
|
|
283
|
+
```css
|
|
284
|
+
.container {
|
|
285
|
+
background: var(--color-background-secondary);
|
|
286
|
+
color: var(--color-text-primary);
|
|
287
|
+
font-family: var(--font-sans);
|
|
288
|
+
border-radius: var(--border-radius-md);
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
Key variable groups: `--color-background-*`, `--color-text-*`, `--color-border-*`, `--font-sans`, `--font-mono`, `--font-text-*-size`, `--font-heading-*-size`, `--border-radius-*`. See `src/spec.types.ts` for the full list.
|
|
293
|
+
|
|
294
|
+
For React apps, use the `useApp` and `useHostStyles` hooks instead — see `basic-server-react/` for the pattern.
|
|
295
|
+
|
|
296
|
+
## Optional Enhancements
|
|
297
|
+
|
|
298
|
+
### App-Only Helper Tools
|
|
299
|
+
|
|
300
|
+
Tools the UI calls but the model doesn't need to invoke directly (polling, pagination, chunk loading):
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
registerAppTool(server, "poll-data", {
|
|
304
|
+
description: "Polls latest data for the UI",
|
|
305
|
+
_meta: { ui: { resourceUri, visibility: ["app"] } },
|
|
306
|
+
}, async () => {
|
|
307
|
+
const data = await getLatestData();
|
|
308
|
+
return { content: [{ type: "text", text: JSON.stringify(data) }] };
|
|
309
|
+
});
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
The UI calls these via `app.callServerTool("poll-data", {})`.
|
|
313
|
+
|
|
314
|
+
### CSP Configuration
|
|
315
|
+
|
|
316
|
+
If the UI needs to load external resources (fonts, APIs, CDNs), declare the domains:
|
|
317
|
+
|
|
318
|
+
```typescript
|
|
319
|
+
registerAppResource(server, {
|
|
320
|
+
uri: resourceUri,
|
|
321
|
+
name: "My Tool UI",
|
|
322
|
+
mimeType: RESOURCE_MIME_TYPE,
|
|
323
|
+
_meta: {
|
|
324
|
+
ui: {
|
|
325
|
+
connectDomains: ["api.example.com"], // fetch/XHR targets
|
|
326
|
+
resourceDomains: ["cdn.example.com"], // scripts, styles, images
|
|
327
|
+
frameDomains: ["embed.example.com"], // nested iframes
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
}, async () => { /* ... */ });
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
### Streaming Partial Input
|
|
334
|
+
|
|
335
|
+
For large tool inputs, show progress during LLM generation:
|
|
336
|
+
|
|
337
|
+
```typescript
|
|
338
|
+
app.ontoolinputpartial = (params) => {
|
|
339
|
+
const args = params.arguments; // Healed partial JSON - always valid
|
|
340
|
+
// Render preview with partial data
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
app.ontoolinput = (params) => {
|
|
344
|
+
// Final complete input - switch to full render
|
|
345
|
+
};
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
### Graceful Degradation with `getUiCapability()`
|
|
349
|
+
|
|
350
|
+
Conditionally register App tools only when the client supports UI, falling back to text-only tools:
|
|
351
|
+
|
|
352
|
+
```typescript
|
|
353
|
+
import { getUiCapability, registerAppTool, RESOURCE_MIME_TYPE } from "@modelcontextprotocol/ext-apps/server";
|
|
354
|
+
|
|
355
|
+
server.server.oninitialized = () => {
|
|
356
|
+
const clientCapabilities = server.server.getClientCapabilities();
|
|
357
|
+
const uiCap = getUiCapability(clientCapabilities);
|
|
358
|
+
|
|
359
|
+
if (uiCap?.mimeTypes?.includes(RESOURCE_MIME_TYPE)) {
|
|
360
|
+
// Client supports UI — register App tool
|
|
361
|
+
registerAppTool(server, "my-tool", {
|
|
362
|
+
description: "Shows data with interactive UI",
|
|
363
|
+
_meta: { ui: { resourceUri } },
|
|
364
|
+
}, appToolHandler);
|
|
365
|
+
} else {
|
|
366
|
+
// Text-only client — register plain tool
|
|
367
|
+
server.tool("my-tool", "Shows data", { param: z.string() }, plainToolHandler);
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
### Fullscreen Mode
|
|
373
|
+
|
|
374
|
+
Allow the UI to expand to fullscreen:
|
|
375
|
+
|
|
376
|
+
```typescript
|
|
377
|
+
app.onhostcontextchanged = (ctx) => {
|
|
378
|
+
if (ctx.availableDisplayModes?.includes("fullscreen")) {
|
|
379
|
+
fullscreenBtn.style.display = "block";
|
|
380
|
+
}
|
|
381
|
+
if (ctx.displayMode) {
|
|
382
|
+
container.classList.toggle("fullscreen", ctx.displayMode === "fullscreen");
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
async function toggleFullscreen() {
|
|
387
|
+
const newMode = currentMode === "fullscreen" ? "inline" : "fullscreen";
|
|
388
|
+
const result = await app.requestDisplayMode({ mode: newMode });
|
|
389
|
+
currentMode = result.mode;
|
|
390
|
+
}
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
## Common Mistakes to Avoid
|
|
394
|
+
|
|
395
|
+
1. **Forgetting text `content` fallback** — Always include `content` array with text for non-UI hosts
|
|
396
|
+
2. **Registering handlers after `connect()`** — Register ALL handlers BEFORE calling `app.connect()`
|
|
397
|
+
3. **Missing `vite-plugin-singlefile`** — Without it, assets won't load in the sandboxed iframe
|
|
398
|
+
4. **Forgetting resource registration** — The tool references a `resourceUri` that must have a matching resource
|
|
399
|
+
5. **Hardcoding styles** — Use host CSS variables (`var(--color-*)`) for theme integration
|
|
400
|
+
6. **Not handling safe area insets** — Always apply `ctx.safeAreaInsets` in `onhostcontextchanged`
|
|
401
|
+
|
|
402
|
+
## Testing
|
|
403
|
+
|
|
404
|
+
### Using basic-host
|
|
405
|
+
|
|
406
|
+
Test the enhanced server with the basic-host example:
|
|
407
|
+
|
|
408
|
+
```bash
|
|
409
|
+
# Terminal 1: Build and run your server
|
|
410
|
+
npm run build && npm run serve
|
|
411
|
+
|
|
412
|
+
# Terminal 2: Run basic-host (from cloned repo)
|
|
413
|
+
cd ./mcp-ext-apps/examples/basic-host
|
|
414
|
+
npm install
|
|
415
|
+
SERVERS='["http://localhost:3001/mcp"]' npm run start
|
|
416
|
+
# Open http://localhost:8080
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
Configure `SERVERS` with a JSON array of your server URLs (default: `http://localhost:3001/mcp`).
|
|
420
|
+
|
|
421
|
+
### Verify
|
|
422
|
+
|
|
423
|
+
1. Plain tools still work and return text output
|
|
424
|
+
2. App tools render their UI in the iframe
|
|
425
|
+
3. `ontoolinput` handler fires with tool arguments
|
|
426
|
+
4. `ontoolresult` handler fires with tool result
|
|
427
|
+
5. Host styling (theme, fonts, colors) applies correctly
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mcp-app-create
|
|
3
|
+
description: This skill should be used when the user asks to "create an MCP App", "add a UI to an MCP tool", "build an interactive MCP View", "scaffold an MCP App", or needs guidance on MCP Apps SDK patterns, UI-resource registration, MCP App lifecycle, or host integration. Provides comprehensive guidance for building MCP Apps with interactive UIs.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Create MCP App
|
|
7
|
+
|
|
8
|
+
Build interactive UIs that run inside MCP-enabled hosts like Claude Desktop. An MCP App combines an MCP tool with an HTML resource to display rich, interactive content.
|
|
9
|
+
|
|
10
|
+
## Core Concept: Tool + Resource
|
|
11
|
+
|
|
12
|
+
Every MCP App requires two parts linked together:
|
|
13
|
+
|
|
14
|
+
1. **Tool** - Called by the LLM/host, returns data
|
|
15
|
+
2. **Resource** - Serves the bundled HTML UI that displays the data
|
|
16
|
+
|
|
17
|
+
The tool's `_meta.ui.resourceUri` references the resource's URI.
|
|
18
|
+
|
|
19
|
+
Host calls tool → Host renders resource UI → Server returns result → UI receives result.
|
|
20
|
+
|
|
21
|
+
## Quick Start Decision Tree
|
|
22
|
+
|
|
23
|
+
### Framework Selection
|
|
24
|
+
|
|
25
|
+
| Framework | `@modelcontextprotocol/ext-apps` Support | Best For |
|
|
26
|
+
|-----------|-------------|----------|
|
|
27
|
+
| React | `useApp` hook provided | Teams familiar with React |
|
|
28
|
+
| Vanilla JS | Manual lifecycle | Simple apps, no build complexity |
|
|
29
|
+
| Vue/Svelte/Preact/Solid | Manual lifecycle | Framework preference |
|
|
30
|
+
|
|
31
|
+
### Project Context
|
|
32
|
+
|
|
33
|
+
**Adding to existing MCP server:**
|
|
34
|
+
- Import `registerAppTool`, `registerAppResource` from `@modelcontextprotocol/ext-apps`
|
|
35
|
+
- Add tool registration with `_meta.ui.resourceUri`
|
|
36
|
+
- Add resource registration serving bundled HTML
|
|
37
|
+
|
|
38
|
+
**Creating new MCP server:**
|
|
39
|
+
- Set up server with transport (stdio or HTTP)
|
|
40
|
+
- Register tools and resources
|
|
41
|
+
- Configure build system with `vite-plugin-singlefile`
|
|
42
|
+
|
|
43
|
+
## Getting Reference Code
|
|
44
|
+
|
|
45
|
+
Clone the MCP Apps SDK repository (`@modelcontextprotocol/ext-apps`) for working examples and API documentation:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
git clone --branch "v$(npm view @modelcontextprotocol/ext-apps version)" --depth 1 https://github.com/modelcontextprotocol/ext-apps.git ./mcp-ext-apps
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
> Add `mcp-ext-apps/` to the project's `.gitignore` — it's a reference clone, not part of the project.
|
|
52
|
+
|
|
53
|
+
### Protocol Specification
|
|
54
|
+
|
|
55
|
+
The formal MCP Apps spec lives in the same repository — read it for authoritative protocol
|
|
56
|
+
semantics that source files and examples only illustrate:
|
|
57
|
+
|
|
58
|
+
| File | Contents |
|
|
59
|
+
|------|----------|
|
|
60
|
+
| `./mcp-ext-apps/specification/2026-01-26/apps.mdx` | **SEP-1865** (Stable, 2026-01-26) — `ui://` URI scheme, `_meta.ui` contract, iframe sandboxing, host↔UI JSON-RPC messages, security model |
|
|
61
|
+
|
|
62
|
+
**Where to look first:**
|
|
63
|
+
|
|
64
|
+
- For **protocol questions** (wire format, required `_meta` keys, mandatory host/server behaviors,
|
|
65
|
+
message semantics) — consult `./mcp-ext-apps/specification/2026-01-26/apps.mdx`. The spec is the source of
|
|
66
|
+
truth for what MUST happen.
|
|
67
|
+
- For **TypeScript API questions** (which helper to call, handler signatures, idiomatic usage) —
|
|
68
|
+
consult `@modelcontextprotocol/ext-apps` sources under `./mcp-ext-apps/src/` and its examples.
|
|
69
|
+
The package is the source of truth for *how* to invoke the protocol from code.
|
|
70
|
+
|
|
71
|
+
### Framework Templates
|
|
72
|
+
|
|
73
|
+
Learn and adapt from `./mcp-ext-apps/examples/basic-server-{framework}/`:
|
|
74
|
+
|
|
75
|
+
| Template | Key Files |
|
|
76
|
+
|----------|-----------|
|
|
77
|
+
| `basic-server-vanillajs/` | `server.ts`, `src/mcp-app.ts`, `mcp-app.html` |
|
|
78
|
+
| `basic-server-react/` | `server.ts`, `src/mcp-app.tsx` (uses `useApp` hook) |
|
|
79
|
+
| `basic-server-vue/` | `server.ts`, `src/App.vue` |
|
|
80
|
+
| `basic-server-svelte/` | `server.ts`, `src/App.svelte` |
|
|
81
|
+
| `basic-server-preact/` | `server.ts`, `src/mcp-app.tsx` |
|
|
82
|
+
| `basic-server-solid/` | `server.ts`, `src/mcp-app.tsx` |
|
|
83
|
+
|
|
84
|
+
Each template includes:
|
|
85
|
+
- `server.ts` with `registerAppTool` and `registerAppResource`
|
|
86
|
+
- `main.ts` entry point with HTTP and stdio transport setup
|
|
87
|
+
- Client-side app (e.g., `src/mcp-app.ts`, `src/mcp-app.tsx`) with lifecycle handlers
|
|
88
|
+
- `src/global.css` with global styles and host style variable fallbacks
|
|
89
|
+
- `vite.config.ts` using `vite-plugin-singlefile`
|
|
90
|
+
- `package.json` with `npm run` scripts and required dependencies
|
|
91
|
+
- `.gitignore` excluding `node_modules/` and `dist/`
|
|
92
|
+
|
|
93
|
+
### Domain-Specific Examples
|
|
94
|
+
|
|
95
|
+
When the App's UI matches one of these domains, consult the corresponding example for patterns
|
|
96
|
+
the basic templates don't cover. All paths are under `./mcp-ext-apps/`:
|
|
97
|
+
|
|
98
|
+
| Domain | Example | What it shows |
|
|
99
|
+
|---|---|---|
|
|
100
|
+
| Charts / dashboards | `examples/scenario-modeler-server/` | Chart.js with structured React (`hooks/`, `lib/`, `components/`); multi-scenario comparison |
|
|
101
|
+
| Analytics drill-down | `examples/cohort-heatmap-server/` | Heatmap with hover tooltips and click drilldown (React) |
|
|
102
|
+
| 3D visualization | `examples/threejs-server/` | Three.js + streaming tool input into canvas, OrbitControls, post-processing |
|
|
103
|
+
| WebGL / shaders | `examples/shadertoy-server/` | GLSL live compilation, fullscreen mode, `vendor/` pattern for custom JS libs |
|
|
104
|
+
| Graph visualization | `examples/wiki-explorer-server/` | 3D force-directed graph (`force-graph`), web scraping with `cheerio` |
|
|
105
|
+
| Audio / music | `examples/sheet-music-server/` | ABC notation → SVG render + MIDI synthesis (`abcjs`) |
|
|
106
|
+
| Streaming + audio | `examples/say-server/` | `ontoolinputpartial` + async audio queue + multi-view lock (**Python FastMCP**) |
|
|
107
|
+
| Browser APIs | `examples/transcript-server/` | Web Speech API for live transcription |
|
|
108
|
+
| Binary / media resources | `examples/video-resource-server/` | Base64 video blobs, `ResourceTemplate`, large-payload limits |
|
|
109
|
+
| SDK surface reference | `examples/debug-server/` | All content types in one place — PNG/WAV blobs, structured output, stateful counter, resource downloads |
|
|
110
|
+
|
|
111
|
+
### API Reference (Source Files)
|
|
112
|
+
|
|
113
|
+
Read JSDoc documentation directly from `./mcp-ext-apps/src/`:
|
|
114
|
+
|
|
115
|
+
| File | Contents |
|
|
116
|
+
|------|----------|
|
|
117
|
+
| `src/app.ts` | `App` class, handlers (`ontoolinput`, `ontoolresult`, `onhostcontextchanged`, `onteardown`, etc.), lifecycle |
|
|
118
|
+
| `src/server/index.ts` | `registerAppTool`, `registerAppResource`, helper functions |
|
|
119
|
+
| `src/spec.types.ts` | All type definitions: `McpUiHostContext`, `McpUiStyleVariableKey` (CSS variable names), `McpUiResourceCsp` (CSP configuration), etc. |
|
|
120
|
+
| `src/styles.ts` | `applyDocumentTheme`, `applyHostStyleVariables`, `applyHostFonts` |
|
|
121
|
+
| `src/react/useApp.tsx` | `useApp` hook for React apps |
|
|
122
|
+
|
|
123
|
+
### Advanced Patterns
|
|
124
|
+
|
|
125
|
+
See `./mcp-ext-apps/docs/patterns.md` for detailed recipes:
|
|
126
|
+
|
|
127
|
+
- **App-only tools** — `visibility: ["app"]`, hiding tools from model
|
|
128
|
+
- **Polling** — real-time dashboards, interval management
|
|
129
|
+
- **Chunked responses** — large files, pagination, base64 encoding
|
|
130
|
+
- **Error handling** — `isError`, informing model of failures
|
|
131
|
+
- **Binary resources** — audio/video/etc via `resources/read`, blob field
|
|
132
|
+
- **Network requests** — assets, fetch, CSP, `_meta.ui.csp`, CORS, `_meta.ui.domain`
|
|
133
|
+
- **Host context** — theme, styling, fonts, safe area insets
|
|
134
|
+
- **Fullscreen mode** — `requestDisplayMode`, display mode changes
|
|
135
|
+
- **Model context** — `updateModelContext`, `sendMessage`, keeping model informed
|
|
136
|
+
- **View state** — `viewUUID`, localStorage, state recovery
|
|
137
|
+
- **Visibility-based pause** — IntersectionObserver, pausing animations/WebGL
|
|
138
|
+
- **Streaming input** — `ontoolinputpartial`, progressive rendering
|
|
139
|
+
|
|
140
|
+
### Reference Host Implementation
|
|
141
|
+
|
|
142
|
+
`./mcp-ext-apps/examples/basic-host/` shows one way an MCP Apps-capable host could be implemented. Real-world hosts like Claude Desktop are more sophisticated—use basic-host for local testing and protocol understanding, not as a guarantee of host behavior.
|
|
143
|
+
|
|
144
|
+
## Critical Implementation Notes
|
|
145
|
+
|
|
146
|
+
### Adding Dependencies
|
|
147
|
+
|
|
148
|
+
**Always** use `npm install` to add dependencies rather than manually writing version numbers:
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
npm install @modelcontextprotocol/ext-apps @modelcontextprotocol/sdk zod express cors
|
|
152
|
+
npm install -D typescript vite vite-plugin-singlefile concurrently cross-env @types/node @types/express @types/cors
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
This lets npm resolve the latest compatible versions. **Never** specify version numbers from memory.
|
|
156
|
+
|
|
157
|
+
### TypeScript Server Execution
|
|
158
|
+
|
|
159
|
+
Unless the user has specified otherwise, use `tsx` for running TypeScript server files. For example:
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
npm install -D tsx
|
|
163
|
+
|
|
164
|
+
npm pkg set scripts.dev="cross-env NODE_ENV=development concurrently 'cross-env INPUT=mcp-app.html vite build --watch' 'tsx --watch main.ts'"
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
> [!NOTE]
|
|
168
|
+
> The `@modelcontextprotocol/ext-apps` examples use `bun` but generated projects should default to `tsx` for broader compatibility.
|
|
169
|
+
|
|
170
|
+
### Handler Registration Order
|
|
171
|
+
|
|
172
|
+
Register ALL handlers BEFORE calling `app.connect()`:
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
const app = new App({ name: "My App", version: "1.0.0" });
|
|
176
|
+
|
|
177
|
+
// Register handlers first
|
|
178
|
+
app.ontoolinput = (params) => { /* handle input */ };
|
|
179
|
+
app.ontoolresult = (result) => { /* handle result */ };
|
|
180
|
+
app.onhostcontextchanged = (ctx) => { /* handle context */ };
|
|
181
|
+
app.onteardown = async () => { return {}; };
|
|
182
|
+
// etc.
|
|
183
|
+
|
|
184
|
+
// Then connect
|
|
185
|
+
await app.connect();
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Common Mistakes to Avoid
|
|
189
|
+
|
|
190
|
+
1. **No text fallback** - Always provide `content` array for non-UI hosts
|
|
191
|
+
2. **Missing CSP configuration** - MCP Apps HTML is served as an MCP resource with no same-origin server; ALL network requests—even to `localhost`—require a CSP configuration
|
|
192
|
+
3. **CSP or CORS config in wrong _meta object** - `_meta.ui.csp` and `_meta.ui.domain` go in the `contents[]` objects returned by `registerAppResource()`'s read callback, not in `registerAppResource()`'s config object
|
|
193
|
+
4. **Handlers after app.connect()** - Register ALL handlers BEFORE calling `app.connect()`
|
|
194
|
+
5. **No streaming for large inputs** - Use `ontoolinputpartial` to show progress during input generation
|
|
195
|
+
|
|
196
|
+
## Testing
|
|
197
|
+
|
|
198
|
+
### Using basic-host
|
|
199
|
+
|
|
200
|
+
Test MCP Apps locally with the basic-host example:
|
|
201
|
+
|
|
202
|
+
```bash
|
|
203
|
+
# Terminal 1: Build and run your server
|
|
204
|
+
npm run build && npm run serve
|
|
205
|
+
|
|
206
|
+
# Terminal 2: Run basic-host (from cloned repo)
|
|
207
|
+
cd ./mcp-ext-apps/examples/basic-host
|
|
208
|
+
npm install
|
|
209
|
+
SERVERS='["http://localhost:3001/mcp"]' npm run start
|
|
210
|
+
# Open http://localhost:8080
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Configure `SERVERS` with a JSON array of your server URLs (default: `http://localhost:3001/mcp`).
|
|
214
|
+
|
|
215
|
+
### Debug with sendLog
|
|
216
|
+
|
|
217
|
+
Send debug logs to the host application (rather than just the iframe's dev console):
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
await app.sendLog({ level: "info", data: "Debug message" });
|
|
221
|
+
await app.sendLog({ level: "error", data: { error: err.message } });
|
|
222
|
+
```
|