@modelcontextprotocol/ext-apps 0.4.1 → 1.0.0

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.
@@ -2,34 +2,43 @@
2
2
  * Utilities for MCP servers to register tools and resources that display interactive UIs.
3
3
  *
4
4
  * Use these helpers instead of the base SDK's `registerTool` and `registerResource` when
5
- * your tool should render an {@link app!App} in the client. They handle UI metadata normalization
6
- * and provide sensible defaults for the MCP Apps MIME type ({@link RESOURCE_MIME_TYPE}).
5
+ * your tool should render an {@link app!App `App`} in the client. They handle UI metadata normalization
6
+ * and provide sensible defaults for the MCP Apps MIME type ({@link RESOURCE_MIME_TYPE `RESOURCE_MIME_TYPE`}).
7
7
  *
8
8
  * @module server-helpers
9
9
  *
10
10
  * @example
11
- * ```typescript
12
- * import { registerAppTool, registerAppResource, RESOURCE_MIME_TYPE } from '@modelcontextprotocol/ext-apps/server';
13
- *
14
- * // Register a tool that displays a widget
15
- * registerAppTool(server, "weather", {
16
- * description: "Get weather forecast",
17
- * _meta: { ui: { resourceUri: "ui://weather/widget.html" } },
18
- * }, handler);
11
+ * ```ts source="./index.examples.ts#index_overview"
12
+ * // Register a tool that displays a view
13
+ * registerAppTool(
14
+ * server,
15
+ * "weather",
16
+ * {
17
+ * description: "Get weather forecast",
18
+ * _meta: { ui: { resourceUri: "ui://weather/view.html" } },
19
+ * },
20
+ * toolCallback,
21
+ * );
19
22
  *
20
23
  * // Register the HTML resource the tool references
21
- * registerAppResource(server, "Weather Widget", "ui://weather/widget.html", {}, readCallback);
24
+ * registerAppResource(
25
+ * server,
26
+ * "Weather View",
27
+ * "ui://weather/view.html",
28
+ * {},
29
+ * readCallback,
30
+ * );
22
31
  * ```
23
32
  */
24
- import { RESOURCE_URI_META_KEY, RESOURCE_MIME_TYPE, McpUiResourceMeta, McpUiToolMeta } from "../app.js";
33
+ import { RESOURCE_URI_META_KEY, RESOURCE_MIME_TYPE, McpUiResourceMeta, McpUiToolMeta, McpUiClientCapabilities } from "../app.js";
25
34
  import type { McpServer, RegisteredTool, ResourceMetadata, ToolCallback, ReadResourceCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
26
35
  import type { AnySchema, ZodRawShapeCompat } from "@modelcontextprotocol/sdk/server/zod-compat.js";
27
- import type { ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
36
+ import type { ClientCapabilities, ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
28
37
  export { RESOURCE_URI_META_KEY, RESOURCE_MIME_TYPE };
29
38
  export type { ResourceMetadata, ToolCallback, ReadResourceCallback };
30
39
  /**
31
40
  * Base tool configuration matching the standard MCP server tool options.
32
- * Extended by {@link McpUiAppToolConfig} to add UI metadata requirements.
41
+ * Extended by {@link McpUiAppToolConfig `McpUiAppToolConfig`} to add UI metadata requirements.
33
42
  */
34
43
  export interface ToolConfig {
35
44
  title?: string;
@@ -42,12 +51,12 @@ export interface ToolConfig {
42
51
  /**
43
52
  * Configuration for tools that render an interactive UI.
44
53
  *
45
- * Extends {@link ToolConfig} with a required `_meta` field that specifies UI metadata.
54
+ * Extends {@link ToolConfig `ToolConfig`} with a required `_meta` field that specifies UI metadata.
46
55
  * The UI resource can be specified in two ways:
47
56
  * - `_meta.ui.resourceUri` (preferred)
48
57
  * - `_meta["ui/resourceUri"]` (deprecated, for backward compatibility)
49
58
  *
50
- * @see {@link registerAppTool} for the recommended way to register app tools
59
+ * @see {@link registerAppTool `registerAppTool`} for the recommended way to register app tools
51
60
  */
52
61
  export interface McpUiAppToolConfig extends ToolConfig {
53
62
  _meta: {
@@ -59,7 +68,7 @@ export interface McpUiAppToolConfig extends ToolConfig {
59
68
  * URI of the UI resource to display for this tool.
60
69
  * This is converted to `_meta["ui/resourceUri"]`.
61
70
  *
62
- * @example "ui://weather/widget.html"
71
+ * @example "ui://weather/view.html"
63
72
  *
64
73
  * @deprecated Use `_meta.ui.resourceUri` instead.
65
74
  */
@@ -67,12 +76,12 @@ export interface McpUiAppToolConfig extends ToolConfig {
67
76
  });
68
77
  }
69
78
  /**
70
- * MCP App Resource configuration for {@link registerAppResource}.
79
+ * MCP App Resource configuration for {@link registerAppResource `registerAppResource`}.
71
80
  *
72
81
  * Extends the base MCP SDK `ResourceMetadata` with optional UI metadata
73
82
  * for configuring security policies and rendering preferences.
74
83
  *
75
- * @see {@link registerAppResource} for usage
84
+ * @see {@link registerAppResource `registerAppResource`} for usage
76
85
  */
77
86
  export interface McpUiAppResourceConfig extends ResourceMetadata {
78
87
  /**
@@ -100,59 +109,69 @@ export interface McpUiAppResourceConfig extends ResourceMetadata {
100
109
  * @param cb - Tool handler function
101
110
  *
102
111
  * @example Basic usage
103
- * ```typescript
104
- * import { registerAppTool } from '@modelcontextprotocol/ext-apps/server';
105
- * import { z } from 'zod';
106
- *
107
- * registerAppTool(server, "get-weather", {
108
- * title: "Get Weather",
109
- * description: "Get current weather for a location",
110
- * inputSchema: { location: z.string() },
111
- * _meta: {
112
- * ui: { resourceUri: "ui://weather/widget.html" },
112
+ * ```ts source="./index.examples.ts#registerAppTool_basicUsage"
113
+ * registerAppTool(
114
+ * server,
115
+ * "get-weather",
116
+ * {
117
+ * title: "Get Weather",
118
+ * description: "Get current weather for a location",
119
+ * inputSchema: { location: z.string() },
120
+ * _meta: {
121
+ * ui: { resourceUri: "ui://weather/view.html" },
122
+ * },
123
+ * },
124
+ * async (args) => {
125
+ * const weather = await fetchWeather(args.location);
126
+ * return { content: [{ type: "text", text: JSON.stringify(weather) }] };
113
127
  * },
114
- * }, async (args) => {
115
- * const weather = await fetchWeather(args.location);
116
- * return { content: [{ type: "text", text: JSON.stringify(weather) }] };
117
- * });
128
+ * );
118
129
  * ```
119
130
  *
120
- * @example Tool visibility - create app-only tools for UI actions
121
- * ```typescript
122
- * import { registerAppTool } from '@modelcontextprotocol/ext-apps/server';
123
- * import { z } from 'zod';
124
- *
125
- * // Main tool - visible to both model and app (default)
126
- * registerAppTool(server, "show-cart", {
127
- * description: "Display the user's shopping cart",
128
- * _meta: {
129
- * ui: {
130
- * resourceUri: "ui://shop/cart.html",
131
- * visibility: ["model", "app"],
131
+ * @example Tool visible to model but not callable by UI
132
+ * ```ts source="./index.examples.ts#registerAppTool_modelOnlyVisibility"
133
+ * registerAppTool(
134
+ * server,
135
+ * "show-cart",
136
+ * {
137
+ * description: "Display the user's shopping cart",
138
+ * _meta: {
139
+ * ui: {
140
+ * resourceUri: "ui://shop/cart.html",
141
+ * visibility: ["model"],
142
+ * },
132
143
  * },
133
144
  * },
134
- * }, async () => {
135
- * const cart = await getCart();
136
- * return { content: [{ type: "text", text: JSON.stringify(cart) }] };
137
- * });
138
- *
139
- * // App-only tool - hidden from the model, only callable by the UI
140
- * registerAppTool(server, "update-quantity", {
141
- * description: "Update item quantity in cart",
142
- * inputSchema: { itemId: z.string(), quantity: z.number() },
143
- * _meta: {
144
- * ui: {
145
- * resourceUri: "ui://shop/cart.html",
146
- * visibility: ["app"],
145
+ * async () => {
146
+ * const cart = await getCart();
147
+ * return { content: [{ type: "text", text: JSON.stringify(cart) }] };
148
+ * },
149
+ * );
150
+ * ```
151
+ *
152
+ * @example Tool hidden from model, only callable by UI
153
+ * ```ts source="./index.examples.ts#registerAppTool_appOnlyVisibility"
154
+ * registerAppTool(
155
+ * server,
156
+ * "update-quantity",
157
+ * {
158
+ * description: "Update item quantity in cart",
159
+ * inputSchema: { itemId: z.string(), quantity: z.number() },
160
+ * _meta: {
161
+ * ui: {
162
+ * resourceUri: "ui://shop/cart.html",
163
+ * visibility: ["app"],
164
+ * },
147
165
  * },
148
166
  * },
149
- * }, async ({ itemId, quantity }) => {
150
- * const cart = await updateCartItem(itemId, quantity);
151
- * return { content: [{ type: "text", text: JSON.stringify(cart) }] };
152
- * });
167
+ * async ({ itemId, quantity }) => {
168
+ * const cart = await updateCartItem(itemId, quantity);
169
+ * return { content: [{ type: "text", text: JSON.stringify(cart) }] };
170
+ * },
171
+ * );
153
172
  * ```
154
173
  *
155
- * @see {@link registerAppResource} to register the HTML resource referenced by the tool
174
+ * @see {@link registerAppResource `registerAppResource`} to register the HTML resource referenced by the tool
156
175
  */
157
176
  export declare function registerAppTool<OutputArgs extends ZodRawShapeCompat | AnySchema, InputArgs extends undefined | ZodRawShapeCompat | AnySchema = undefined>(server: Pick<McpServer, "registerTool">, name: string, config: McpUiAppToolConfig & {
158
177
  inputSchema?: InputArgs;
@@ -162,7 +181,7 @@ export declare function registerAppTool<OutputArgs extends ZodRawShapeCompat | A
162
181
  * Register an app resource with the MCP server.
163
182
  *
164
183
  * This is a convenience wrapper around `server.registerResource` that:
165
- * - Defaults the MIME type to {@link RESOURCE_MIME_TYPE} (`"text/html;profile=mcp-app"`)
184
+ * - Defaults the MIME type to {@link RESOURCE_MIME_TYPE `RESOURCE_MIME_TYPE`} (`"text/html;profile=mcp-app"`)
166
185
  * - Provides a cleaner API matching the SDK's callback signature
167
186
  *
168
187
  * @param server - The MCP server instance
@@ -172,35 +191,97 @@ export declare function registerAppTool<OutputArgs extends ZodRawShapeCompat | A
172
191
  * @param readCallback - Callback that returns the resource contents
173
192
  *
174
193
  * @example Basic usage
175
- * ```typescript
176
- * import { registerAppResource } from '@modelcontextprotocol/ext-apps/server';
177
- *
178
- * registerAppResource(server, "Weather Widget", "ui://weather/widget.html", {
179
- * description: "Interactive weather display",
180
- * }, async () => ({
181
- * contents: [{
182
- * uri: "ui://weather/widget.html",
183
- * mimeType: RESOURCE_MIME_TYPE,
184
- * text: await fs.readFile("dist/widget.html", "utf-8"),
185
- * }],
186
- * }));
194
+ * ```ts source="./index.examples.ts#registerAppResource_basicUsage"
195
+ * registerAppResource(
196
+ * server,
197
+ * "Weather View",
198
+ * "ui://weather/view.html",
199
+ * {
200
+ * description: "Interactive weather display",
201
+ * },
202
+ * async () => ({
203
+ * contents: [
204
+ * {
205
+ * uri: "ui://weather/view.html",
206
+ * mimeType: RESOURCE_MIME_TYPE,
207
+ * text: await fs.readFile("dist/view.html", "utf-8"),
208
+ * },
209
+ * ],
210
+ * }),
211
+ * );
187
212
  * ```
188
213
  *
189
214
  * @example With CSP configuration for external domains
190
- * ```typescript
191
- * registerAppResource(server, "Music Player", "ui://music/player.html", {
192
- * description: "Audio player with external soundfonts",
193
- * _meta: {
194
- * ui: {
195
- * csp: {
196
- * connectDomains: ["https://api.example.com"], // For fetch/WebSocket
197
- * resourceDomains: ["https://cdn.example.com"], // For scripts/styles/images
198
- * },
199
- * },
215
+ * ```ts source="./index.examples.ts#registerAppResource_withCsp"
216
+ * registerAppResource(
217
+ * server,
218
+ * "Music Player",
219
+ * "ui://music/player.html",
220
+ * {
221
+ * description: "Audio player with external soundfonts",
200
222
  * },
201
- * }, readCallback);
223
+ * async () => ({
224
+ * contents: [
225
+ * {
226
+ * uri: "ui://music/player.html",
227
+ * mimeType: RESOURCE_MIME_TYPE,
228
+ * text: musicPlayerHtml,
229
+ * _meta: {
230
+ * ui: {
231
+ * csp: {
232
+ * resourceDomains: ["https://cdn.example.com"], // For scripts/styles/images
233
+ * connectDomains: ["https://api.example.com"], // For fetch/WebSocket
234
+ * },
235
+ * },
236
+ * },
237
+ * },
238
+ * ],
239
+ * }),
240
+ * );
202
241
  * ```
203
242
  *
204
- * @see {@link registerAppTool} to register tools that reference this resource
243
+ * @see {@link registerAppTool `registerAppTool`} to register tools that reference this resource
205
244
  */
206
245
  export declare function registerAppResource(server: Pick<McpServer, "registerResource">, name: string, uri: string, config: McpUiAppResourceConfig, readCallback: ReadResourceCallback): void;
246
+ /**
247
+ * Extension identifier for MCP Apps capability negotiation.
248
+ *
249
+ * Used as the key in `extensions` to advertise MCP Apps support.
250
+ */
251
+ export declare const EXTENSION_ID = "io.modelcontextprotocol/ui";
252
+ /**
253
+ * Get MCP Apps capability settings from client capabilities.
254
+ *
255
+ * This helper retrieves the capability object from the `extensions` field
256
+ * where MCP Apps advertises its support.
257
+ *
258
+ * Note: The `clientCapabilities` parameter extends the SDK's `ClientCapabilities`
259
+ * type with an `extensions` field (pending SEP-1724). Once `extensions` is added
260
+ * to the SDK, this can use `ClientCapabilities` directly.
261
+ *
262
+ * @param clientCapabilities - The client capabilities from the initialize response
263
+ * @returns The MCP Apps capability settings, or `undefined` if not supported
264
+ *
265
+ * @example Check for MCP Apps support in server initialization
266
+ * ```typescript
267
+ * import { getUiCapability, RESOURCE_MIME_TYPE, registerAppTool } from "@modelcontextprotocol/ext-apps/server";
268
+ *
269
+ * server.oninitialized = ({ clientCapabilities }) => {
270
+ * const uiCap = getUiCapability(clientCapabilities);
271
+ * if (uiCap?.mimeTypes?.includes(RESOURCE_MIME_TYPE)) {
272
+ * registerAppTool(server, "weather", {
273
+ * description: "Get weather with interactive dashboard",
274
+ * _meta: { ui: { resourceUri: "ui://weather/dashboard" } },
275
+ * }, weatherHandler);
276
+ * } else {
277
+ * // Register text-only fallback
278
+ * server.registerTool("weather", {
279
+ * description: "Get weather as text",
280
+ * }, textWeatherHandler);
281
+ * }
282
+ * };
283
+ * ```
284
+ */
285
+ export declare function getUiCapability(clientCapabilities: (ClientCapabilities & {
286
+ extensions?: Record<string, unknown>;
287
+ }) | null | undefined): McpUiClientCapabilities | undefined;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Type-checked examples for {@link registerAppTool `registerAppTool`} and {@link registerAppResource `registerAppResource`}.
3
+ *
4
+ * These examples are included in the API documentation via `@includeCode` tags.
5
+ * Each function's region markers define the code snippet that appears in the docs.
6
+ *
7
+ * @module
8
+ */
9
+ export {};