Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/server/index.examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { z } from "zod";
import {
registerAppTool,
registerAppResource,
getUiCapability,
RESOURCE_MIME_TYPE,
} from "./index.js";

Expand Down Expand Up @@ -196,3 +197,41 @@ function registerAppResource_withCsp(
);
//#endregion registerAppResource_withCsp
}

/**
* Example: Check for MCP Apps support in server initialization.
*/
function getUiCapability_checkSupport(
server: McpServer,
weatherHandler: ToolCallback,
textWeatherHandler: ToolCallback,
) {
//#region getUiCapability_checkSupport
server.server.oninitialized = () => {
const clientCapabilities = server.server.getClientCapabilities();
const uiCap = getUiCapability(clientCapabilities);

if (uiCap?.mimeTypes?.includes(RESOURCE_MIME_TYPE)) {
// App-enhanced tool
registerAppTool(
server,
"weather",
{
description: "Get weather information with interactive dashboard",
_meta: { ui: { resourceUri: "ui://weather/dashboard" } },
},
weatherHandler,
);
} else {
// Text-only fallback
server.registerTool(
"weather",
{
description: "Get weather information",
},
textWeatherHandler,
);
}
};
//#endregion getUiCapability_checkSupport
}
34 changes: 22 additions & 12 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,21 +340,31 @@ export const EXTENSION_ID = "io.modelcontextprotocol/ui";
* @returns The MCP Apps capability settings, or `undefined` if not supported
*
* @example Check for MCP Apps support in server initialization
* ```typescript
* import { getUiCapability, RESOURCE_MIME_TYPE, registerAppTool } from "@modelcontextprotocol/ext-apps/server";
*
* server.oninitialized = ({ clientCapabilities }) => {
* ```ts source="./index.examples.ts#getUiCapability_checkSupport"
* server.server.oninitialized = () => {
* const clientCapabilities = server.server.getClientCapabilities();
* const uiCap = getUiCapability(clientCapabilities);
*
* if (uiCap?.mimeTypes?.includes(RESOURCE_MIME_TYPE)) {
* registerAppTool(server, "weather", {
* description: "Get weather with interactive dashboard",
* _meta: { ui: { resourceUri: "ui://weather/dashboard" } },
* }, weatherHandler);
* // App-enhanced tool
* registerAppTool(
* server,
* "weather",
* {
* description: "Get weather information with interactive dashboard",
* _meta: { ui: { resourceUri: "ui://weather/dashboard" } },
* },
* weatherHandler,
* );
* } else {
* // Register text-only fallback
* server.registerTool("weather", {
* description: "Get weather as text",
* }, textWeatherHandler);
* // Text-only fallback
* server.registerTool(
* "weather",
* {
* description: "Get weather information",
* },
* textWeatherHandler,
* );
* }
* };
* ```
Expand Down
Loading