Merge pull request #406 from jonathanhefner/extract-getUiCapability-example
Extract `getUiCapability` example to type-checked examples file
This commit is contained in:
commit
5c4f4969b1
2 changed files with 61 additions and 12 deletions
|
|
@ -17,6 +17,7 @@ import { z } from "zod";
|
||||||
import {
|
import {
|
||||||
registerAppTool,
|
registerAppTool,
|
||||||
registerAppResource,
|
registerAppResource,
|
||||||
|
getUiCapability,
|
||||||
RESOURCE_MIME_TYPE,
|
RESOURCE_MIME_TYPE,
|
||||||
} from "./index.js";
|
} from "./index.js";
|
||||||
|
|
||||||
|
|
@ -196,3 +197,41 @@ function registerAppResource_withCsp(
|
||||||
);
|
);
|
||||||
//#endregion 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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -340,21 +340,31 @@ export const EXTENSION_ID = "io.modelcontextprotocol/ui";
|
||||||
* @returns The MCP Apps capability settings, or `undefined` if not supported
|
* @returns The MCP Apps capability settings, or `undefined` if not supported
|
||||||
*
|
*
|
||||||
* @example Check for MCP Apps support in server initialization
|
* @example Check for MCP Apps support in server initialization
|
||||||
* ```typescript
|
* ```ts source="./index.examples.ts#getUiCapability_checkSupport"
|
||||||
* import { getUiCapability, RESOURCE_MIME_TYPE, registerAppTool } from "@modelcontextprotocol/ext-apps/server";
|
* server.server.oninitialized = () => {
|
||||||
*
|
* const clientCapabilities = server.server.getClientCapabilities();
|
||||||
* server.oninitialized = ({ clientCapabilities }) => {
|
|
||||||
* const uiCap = getUiCapability(clientCapabilities);
|
* const uiCap = getUiCapability(clientCapabilities);
|
||||||
|
*
|
||||||
* if (uiCap?.mimeTypes?.includes(RESOURCE_MIME_TYPE)) {
|
* if (uiCap?.mimeTypes?.includes(RESOURCE_MIME_TYPE)) {
|
||||||
* registerAppTool(server, "weather", {
|
* // App-enhanced tool
|
||||||
* description: "Get weather with interactive dashboard",
|
* registerAppTool(
|
||||||
* _meta: { ui: { resourceUri: "ui://weather/dashboard" } },
|
* server,
|
||||||
* }, weatherHandler);
|
* "weather",
|
||||||
|
* {
|
||||||
|
* description: "Get weather information with interactive dashboard",
|
||||||
|
* _meta: { ui: { resourceUri: "ui://weather/dashboard" } },
|
||||||
|
* },
|
||||||
|
* weatherHandler,
|
||||||
|
* );
|
||||||
* } else {
|
* } else {
|
||||||
* // Register text-only fallback
|
* // Text-only fallback
|
||||||
* server.registerTool("weather", {
|
* server.registerTool(
|
||||||
* description: "Get weather as text",
|
* "weather",
|
||||||
* }, textWeatherHandler);
|
* {
|
||||||
|
* description: "Get weather information",
|
||||||
|
* },
|
||||||
|
* textWeatherHandler,
|
||||||
|
* );
|
||||||
* }
|
* }
|
||||||
* };
|
* };
|
||||||
* ```
|
* ```
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue