|
| 1 | +import { |
| 2 | + AppProtocol, |
| 3 | + AppToolDefinition, |
| 4 | + AppCSPConfig, |
| 5 | + AppPermissionsConfig, |
| 6 | + AppUIResourceMeta, |
| 7 | + MCP_APP_MIME_TYPE, |
| 8 | +} from './types.js'; |
| 9 | +import { validateAppUri, validateAppToolVisibility, warnContentSize } from './validation.js'; |
| 10 | +import { ResourceContent, ResourceDefinition } from '../resources/BaseResource.js'; |
| 11 | + |
| 12 | +/** UI configuration for an MCPApp. */ |
| 13 | +export interface AppUIConfig { |
| 14 | + /** URI for this app's UI resource. Must start with "ui://". */ |
| 15 | + resourceUri: string; |
| 16 | + /** Human-readable name for the UI resource. */ |
| 17 | + resourceName: string; |
| 18 | + /** Optional description of the UI. */ |
| 19 | + resourceDescription?: string; |
| 20 | + /** CSP configuration for the iframe sandbox. */ |
| 21 | + csp?: AppCSPConfig; |
| 22 | + /** Browser permissions needed by the app. */ |
| 23 | + permissions?: AppPermissionsConfig; |
| 24 | + /** Whether the host should render a visible border. */ |
| 25 | + prefersBorder?: boolean; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Base class for standalone MCP Apps (Mode A). |
| 30 | + * |
| 31 | + * Bundles UI configuration, HTML content, and associated tool definitions |
| 32 | + * into a single auto-discoverable class. Place subclasses in `src/apps/`. |
| 33 | + * |
| 34 | + * @example |
| 35 | + * ```typescript |
| 36 | + * class DashboardApp extends MCPApp { |
| 37 | + * name = "dashboard"; |
| 38 | + * ui = { |
| 39 | + * resourceUri: "ui://dashboard/view", |
| 40 | + * resourceName: "Dashboard", |
| 41 | + * }; |
| 42 | + * getContent() { return readFileSync("./dashboard.html", "utf-8"); } |
| 43 | + * tools = [{ |
| 44 | + * name: "show_dashboard", |
| 45 | + * description: "Show the dashboard", |
| 46 | + * schema: z.object({ range: z.string().describe("Time range") }), |
| 47 | + * execute: async (input) => fetchData(input.range), |
| 48 | + * }]; |
| 49 | + * } |
| 50 | + * ``` |
| 51 | + */ |
| 52 | +export abstract class MCPApp implements AppProtocol { |
| 53 | + /** Unique identifier for this app. */ |
| 54 | + abstract name: string; |
| 55 | + /** UI configuration (resource URI, name, CSP, permissions). */ |
| 56 | + abstract ui: AppUIConfig; |
| 57 | + /** Tools associated with this app. At least one is required. */ |
| 58 | + abstract tools: AppToolDefinition[]; |
| 59 | + /** Return the HTML content for the UI resource. */ |
| 60 | + abstract getContent(): Promise<string> | string; |
| 61 | + |
| 62 | + /** Validates app configuration. Called by MCPServer at startup. */ |
| 63 | + validate(): void { |
| 64 | + validateAppUri(this.ui.resourceUri, `app "${this.name}"`); |
| 65 | + |
| 66 | + if (!this.ui.resourceName) { |
| 67 | + throw new Error(`App "${this.name}" must have a ui.resourceName.`); |
| 68 | + } |
| 69 | + |
| 70 | + if (!this.tools || this.tools.length === 0) { |
| 71 | + throw new Error(`App "${this.name}" must define at least one tool.`); |
| 72 | + } |
| 73 | + |
| 74 | + const toolNames = new Set<string>(); |
| 75 | + for (const tool of this.tools) { |
| 76 | + if (toolNames.has(tool.name)) { |
| 77 | + throw new Error(`App "${this.name}" has duplicate tool name: "${tool.name}".`); |
| 78 | + } |
| 79 | + toolNames.add(tool.name); |
| 80 | + |
| 81 | + if (!tool.name || !tool.description || !tool.schema || !tool.execute) { |
| 82 | + throw new Error( |
| 83 | + `App "${this.name}" tool "${tool.name || '(unnamed)'}" must have name, description, schema, and execute.`, |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + validateAppToolVisibility(tool.visibility, tool.name); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** Returns the MCP resource definition for this app's UI. */ |
| 92 | + get resourceDefinition(): ResourceDefinition { |
| 93 | + return { |
| 94 | + uri: this.ui.resourceUri, |
| 95 | + name: this.ui.resourceName, |
| 96 | + description: this.ui.resourceDescription, |
| 97 | + mimeType: MCP_APP_MIME_TYPE, |
| 98 | + }; |
| 99 | + } |
| 100 | + |
| 101 | + /** Returns the _meta.ui metadata for resource content, or undefined if none. */ |
| 102 | + get resourceMeta(): AppUIResourceMeta | undefined { |
| 103 | + const { csp, permissions, prefersBorder } = this.ui; |
| 104 | + if (!csp && !permissions && prefersBorder === undefined) return undefined; |
| 105 | + return { |
| 106 | + ...(csp && { csp }), |
| 107 | + ...(permissions && { permissions }), |
| 108 | + ...(prefersBorder !== undefined && { prefersBorder }), |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + /** Reads HTML content and returns it as MCP ResourceContent. */ |
| 113 | + async readResource(): Promise<ResourceContent[]> { |
| 114 | + const html = await this.getContent(); |
| 115 | + warnContentSize(html, this.name); |
| 116 | + |
| 117 | + const content: ResourceContent & { _meta?: Record<string, unknown> } = { |
| 118 | + uri: this.ui.resourceUri, |
| 119 | + mimeType: MCP_APP_MIME_TYPE, |
| 120 | + text: html, |
| 121 | + }; |
| 122 | + |
| 123 | + const meta = this.resourceMeta; |
| 124 | + if (meta) { |
| 125 | + content._meta = { ui: meta }; |
| 126 | + } |
| 127 | + |
| 128 | + return [content]; |
| 129 | + } |
| 130 | + |
| 131 | + /** Returns the _meta.ui object for a tool definition. */ |
| 132 | + getToolMeta(toolName: string): { ui: { resourceUri: string; visibility?: Array<string> } } { |
| 133 | + const tool = this.tools.find((t) => t.name === toolName); |
| 134 | + const visibility = tool?.visibility ?? ['model', 'app']; |
| 135 | + return { |
| 136 | + ui: { |
| 137 | + resourceUri: this.ui.resourceUri, |
| 138 | + ...(visibility && { visibility }), |
| 139 | + }, |
| 140 | + }; |
| 141 | + } |
| 142 | +} |
0 commit comments