Skip to content

Commit 5c4f496

Browse files
Merge pull request modelcontextprotocol#406 from jonathanhefner/extract-getUiCapability-example
Extract `getUiCapability` example to type-checked examples file
2 parents d7ff999 + 52cc193 commit 5c4f496

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

src/server/index.examples.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { z } from "zod";
1717
import {
1818
registerAppTool,
1919
registerAppResource,
20+
getUiCapability,
2021
RESOURCE_MIME_TYPE,
2122
} from "./index.js";
2223

@@ -196,3 +197,41 @@ function registerAppResource_withCsp(
196197
);
197198
//#endregion registerAppResource_withCsp
198199
}
200+
201+
/**
202+
* Example: Check for MCP Apps support in server initialization.
203+
*/
204+
function getUiCapability_checkSupport(
205+
server: McpServer,
206+
weatherHandler: ToolCallback,
207+
textWeatherHandler: ToolCallback,
208+
) {
209+
//#region getUiCapability_checkSupport
210+
server.server.oninitialized = () => {
211+
const clientCapabilities = server.server.getClientCapabilities();
212+
const uiCap = getUiCapability(clientCapabilities);
213+
214+
if (uiCap?.mimeTypes?.includes(RESOURCE_MIME_TYPE)) {
215+
// App-enhanced tool
216+
registerAppTool(
217+
server,
218+
"weather",
219+
{
220+
description: "Get weather information with interactive dashboard",
221+
_meta: { ui: { resourceUri: "ui://weather/dashboard" } },
222+
},
223+
weatherHandler,
224+
);
225+
} else {
226+
// Text-only fallback
227+
server.registerTool(
228+
"weather",
229+
{
230+
description: "Get weather information",
231+
},
232+
textWeatherHandler,
233+
);
234+
}
235+
};
236+
//#endregion getUiCapability_checkSupport
237+
}

src/server/index.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -340,21 +340,31 @@ export const EXTENSION_ID = "io.modelcontextprotocol/ui";
340340
* @returns The MCP Apps capability settings, or `undefined` if not supported
341341
*
342342
* @example Check for MCP Apps support in server initialization
343-
* ```typescript
344-
* import { getUiCapability, RESOURCE_MIME_TYPE, registerAppTool } from "@modelcontextprotocol/ext-apps/server";
345-
*
346-
* server.oninitialized = ({ clientCapabilities }) => {
343+
* ```ts source="./index.examples.ts#getUiCapability_checkSupport"
344+
* server.server.oninitialized = () => {
345+
* const clientCapabilities = server.server.getClientCapabilities();
347346
* const uiCap = getUiCapability(clientCapabilities);
347+
*
348348
* if (uiCap?.mimeTypes?.includes(RESOURCE_MIME_TYPE)) {
349-
* registerAppTool(server, "weather", {
350-
* description: "Get weather with interactive dashboard",
351-
* _meta: { ui: { resourceUri: "ui://weather/dashboard" } },
352-
* }, weatherHandler);
349+
* // App-enhanced tool
350+
* registerAppTool(
351+
* server,
352+
* "weather",
353+
* {
354+
* description: "Get weather information with interactive dashboard",
355+
* _meta: { ui: { resourceUri: "ui://weather/dashboard" } },
356+
* },
357+
* weatherHandler,
358+
* );
353359
* } else {
354-
* // Register text-only fallback
355-
* server.registerTool("weather", {
356-
* description: "Get weather as text",
357-
* }, textWeatherHandler);
360+
* // Text-only fallback
361+
* server.registerTool(
362+
* "weather",
363+
* {
364+
* description: "Get weather information",
365+
* },
366+
* textWeatherHandler,
367+
* );
358368
* }
359369
* };
360370
* ```

0 commit comments

Comments
 (0)