@modelcontextprotocol/ext-apps 1.0.0 → 1.1.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.
@@ -97,8 +97,8 @@ export interface AppState {
97
97
  * app.onerror = (error) => {
98
98
  * console.log("Error:", error);
99
99
  * };
100
- * app.onhostcontextchanged = (params) => {
101
- * setHostContext((prev) => ({ ...prev, ...params }));
100
+ * app.onhostcontextchanged = (ctx) => {
101
+ * setHostContext((prev) => ({ ...prev, ...ctx }));
102
102
  * };
103
103
  * },
104
104
  * });
@@ -31,11 +31,11 @@
31
31
  * ```
32
32
  */
33
33
  import { RESOURCE_URI_META_KEY, RESOURCE_MIME_TYPE, McpUiResourceMeta, McpUiToolMeta, McpUiClientCapabilities } from "../app.js";
34
- import type { McpServer, RegisteredTool, ResourceMetadata, ToolCallback, ReadResourceCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
34
+ import type { McpServer, RegisteredTool, ResourceMetadata, ToolCallback, ReadResourceCallback as _ReadResourceCallback, RegisteredResource } from "@modelcontextprotocol/sdk/server/mcp.js";
35
35
  import type { AnySchema, ZodRawShapeCompat } from "@modelcontextprotocol/sdk/server/zod-compat.js";
36
- import type { ClientCapabilities, ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
36
+ import type { ClientCapabilities, ReadResourceResult, ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
37
37
  export { RESOURCE_URI_META_KEY, RESOURCE_MIME_TYPE };
38
- export type { ResourceMetadata, ToolCallback, ReadResourceCallback };
38
+ export type { ResourceMetadata, ToolCallback };
39
39
  /**
40
40
  * Base tool configuration matching the standard MCP server tool options.
41
41
  * Extended by {@link McpUiAppToolConfig `McpUiAppToolConfig`} to add UI metadata requirements.
@@ -81,12 +81,19 @@ export interface McpUiAppToolConfig extends ToolConfig {
81
81
  * Extends the base MCP SDK `ResourceMetadata` with optional UI metadata
82
82
  * for configuring security policies and rendering preferences.
83
83
  *
84
+ * The `_meta.ui` field here is included in the `resources/list` response and serves as
85
+ * a static default for hosts to review at connection time. When the `resources/read`
86
+ * content item also includes `_meta.ui`, the content-item value takes precedence.
87
+ *
84
88
  * @see {@link registerAppResource `registerAppResource`} for usage
85
89
  */
86
90
  export interface McpUiAppResourceConfig extends ResourceMetadata {
87
91
  /**
88
92
  * Optional UI metadata for the resource.
89
- * Used to configure security policies (CSP) and rendering preferences.
93
+ *
94
+ * This appears on the resource entry in `resources/list` and acts as a listing-level
95
+ * fallback. Individual content items returned by `resources/read` may include their
96
+ * own `_meta.ui` which takes precedence over this value.
90
97
  */
91
98
  _meta?: {
92
99
  /**
@@ -177,6 +184,14 @@ export declare function registerAppTool<OutputArgs extends ZodRawShapeCompat | A
177
184
  inputSchema?: InputArgs;
178
185
  outputSchema?: OutputArgs;
179
186
  }, cb: ToolCallback<InputArgs>): RegisteredTool;
187
+ export type McpUiReadResourceResult = ReadResourceResult & {
188
+ _meta?: {
189
+ ui?: McpUiResourceMeta;
190
+ [key: string]: unknown;
191
+ };
192
+ };
193
+ export type McpUiReadResourceCallback = (uri: URL, extra: Parameters<_ReadResourceCallback>[1]) => McpUiReadResourceResult | Promise<McpUiReadResourceResult>;
194
+ export type ReadResourceCallback = McpUiReadResourceCallback;
180
195
  /**
181
196
  * Register an app resource with the MCP server.
182
197
  *
@@ -211,7 +226,7 @@ export declare function registerAppTool<OutputArgs extends ZodRawShapeCompat | A
211
226
  * );
212
227
  * ```
213
228
  *
214
- * @example With CSP configuration for external domains
229
+ * @example With CSP configuration for network access
215
230
  * ```ts source="./index.examples.ts#registerAppResource_withCsp"
216
231
  * registerAppResource(
217
232
  * server,
@@ -240,9 +255,57 @@ export declare function registerAppTool<OutputArgs extends ZodRawShapeCompat | A
240
255
  * );
241
256
  * ```
242
257
  *
258
+ * @example With stable origin for external API CORS allowlists
259
+ * ```ts source="./index.examples.ts#registerAppResource_withDomain"
260
+ * // Computes a stable origin from an MCP server URL for hosting in Claude.
261
+ * function computeAppDomainForClaude(mcpServerUrl: string): string {
262
+ * const hash = crypto
263
+ * .createHash("sha256")
264
+ * .update(mcpServerUrl)
265
+ * .digest("hex")
266
+ * .slice(0, 32);
267
+ * return `${hash}.claudemcpcontent.com`;
268
+ * }
269
+ *
270
+ * const APP_DOMAIN = computeAppDomainForClaude("https://example.com/mcp");
271
+ *
272
+ * registerAppResource(
273
+ * server,
274
+ * "Company Dashboard",
275
+ * "ui://dashboard/view.html",
276
+ * {
277
+ * description: "Internal dashboard with company data",
278
+ * },
279
+ * async () => ({
280
+ * contents: [
281
+ * {
282
+ * uri: "ui://dashboard/view.html",
283
+ * mimeType: RESOURCE_MIME_TYPE,
284
+ * text: dashboardHtml,
285
+ * _meta: {
286
+ * ui: {
287
+ * // CSP: tell browser the app is allowed to make requests
288
+ * csp: {
289
+ * connectDomains: ["https://api.example.com"],
290
+ * },
291
+ * // CORS: give app a stable origin for the API server to allowlist
292
+ * //
293
+ * // (Public APIs that use `Access-Control-Allow-Origin: *` or API
294
+ * // key auth don't need this.)
295
+ * domain: APP_DOMAIN,
296
+ * },
297
+ * },
298
+ * },
299
+ * ],
300
+ * }),
301
+ * );
302
+ * ```
303
+ *
304
+ * @see {@link McpUiResourceMeta `McpUiResourceMeta`} for `_meta.ui` configuration options
305
+ * @see {@link McpUiResourceCsp `McpUiResourceCsp`} for CSP domain allowlist configuration
243
306
  * @see {@link registerAppTool `registerAppTool`} to register tools that reference this resource
244
307
  */
245
- export declare function registerAppResource(server: Pick<McpServer, "registerResource">, name: string, uri: string, config: McpUiAppResourceConfig, readCallback: ReadResourceCallback): void;
308
+ export declare function registerAppResource(server: Pick<McpServer, "registerResource">, name: string, uri: string, config: McpUiAppResourceConfig, readCallback: McpUiReadResourceCallback): RegisteredResource;
246
309
  /**
247
310
  * Extension identifier for MCP Apps capability negotiation.
248
311
  *
@@ -263,21 +326,31 @@ export declare const EXTENSION_ID = "io.modelcontextprotocol/ui";
263
326
  * @returns The MCP Apps capability settings, or `undefined` if not supported
264
327
  *
265
328
  * @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 }) => {
329
+ * ```ts source="./index.examples.ts#getUiCapability_checkSupport"
330
+ * server.server.oninitialized = () => {
331
+ * const clientCapabilities = server.server.getClientCapabilities();
270
332
  * const uiCap = getUiCapability(clientCapabilities);
333
+ *
271
334
  * 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);
335
+ * // App-enhanced tool
336
+ * registerAppTool(
337
+ * server,
338
+ * "weather",
339
+ * {
340
+ * description: "Get weather information with interactive dashboard",
341
+ * _meta: { ui: { resourceUri: "ui://weather/dashboard" } },
342
+ * },
343
+ * weatherHandler,
344
+ * );
276
345
  * } else {
277
- * // Register text-only fallback
278
- * server.registerTool("weather", {
279
- * description: "Get weather as text",
280
- * }, textWeatherHandler);
346
+ * // Text-only fallback
347
+ * server.registerTool(
348
+ * "weather",
349
+ * {
350
+ * description: "Get weather information",
351
+ * },
352
+ * textWeatherHandler,
353
+ * );
281
354
  * }
282
355
  * };
283
356
  * ```