UNPKG

mcp-use

Version:

Opinionated MCP Framework for TypeScript (@modelcontextprotocol/sdk compatible) - Build MCP Agents, Clients and Servers with support for ChatGPT Apps, Code Mode, OAuth, Notifications, Sampling, Observability and more.

304 lines 10.1 kB
/** * Widget helper utilities * * This module provides utility functions for widget registration, URI generation, * and prop handling. */ import type { Hono as HonoType } from "hono"; import type { UIResourceContent, UIResourceDefinition, InputDefinition, WidgetProps } from "../types/index.js"; /** * Generate a widget URI with optional build ID for cache busting * * @param widgetName - Widget name/identifier * @param buildId - Optional build ID for cache busting * @param extension - Optional file extension (e.g., '.html') * @param suffix - Optional suffix (e.g., random ID for dynamic URIs) * @returns Widget URI with build ID if available * * @example * ```typescript * generateWidgetUri('kanban-board', 'abc123', '.html') * // Returns: 'ui://widget/kanban-board-abc123.html' * ``` */ export declare function generateWidgetUri(widgetName: string, buildId: string | undefined, extension?: string, suffix?: string): string; /** * Convert widget props definition to tool input schema * * Transforms the widget props configuration into the format expected by * the tool registration system, mapping types and handling defaults. * * @param props - Widget props configuration * @returns Array of InputDefinition objects for tool registration * * @example * ```typescript * const props = { * title: { type: 'string', required: true, description: 'Board title' }, * color: { type: 'string', default: 'blue' } * }; * const inputs = convertPropsToInputs(props); * // Returns: [ * // { name: 'title', type: 'string', required: true, description: 'Board title' }, * // { name: 'color', type: 'string', default: 'blue' } * // ] * ``` */ export declare function convertPropsToInputs(props?: WidgetProps): InputDefinition[]; /** * Apply default values to widget props * * Extracts default values from the props configuration to use when * the resource is accessed without parameters. * * @param props - Widget props configuration * @returns Object with default values for each prop * * @example * ```typescript * const props = { * title: { type: 'string', default: 'My Board' }, * color: { type: 'string', default: 'blue' }, * size: { type: 'number' } // no default * }; * const defaults = applyDefaultProps(props); * // Returns: { title: 'My Board', color: 'blue' } * ``` */ export declare function applyDefaultProps(props?: WidgetProps): Record<string, any>; /** * Read build manifest file * * @returns Build manifest or null if not found * * @example * ```typescript * const manifest = await readBuildManifest(); * if (manifest) { * console.log('Build ID:', manifest.buildId); * console.log('Widgets:', manifest.widgets); * } * ``` */ export declare function readBuildManifest(): Promise<{ includeInspector: boolean; widgets: string[] | Record<string, any>; buildTime?: string; buildId?: string; } | null>; /** * Server configuration for widget UI resource creation */ export interface WidgetServerConfig { /** Server host */ serverHost: string; /** Server port */ serverPort: number; /** Server base URL (if configured) */ serverBaseUrl?: string; /** Build ID for cache busting */ buildId?: string; } /** * Create a UIResource object for a widget with the given parameters * * This function creates a consistent UIResource structure that can be rendered * by MCP-UI compatible clients. It handles URL configuration, build IDs, and * metadata merging. * * @param definition - UIResource definition * @param params - Parameters to pass to the widget via URL * @param serverConfig - Server configuration (host, port, baseUrl, buildId) * @returns UIResource object compatible with MCP-UI * * @example * ```typescript * const serverConfig = { * serverHost: 'localhost', * serverPort: 3000, * serverBaseUrl: 'http://localhost:3000', * buildId: 'abc123' * }; * * const definition = { * type: 'appsSdk', * name: 'kanban-board', * title: 'Kanban Board', * htmlTemplate: '<div>...</div>', * appsSdkMetadata: { ... } * }; * * const uiResource = await createWidgetUIResource(definition, { title: 'My Board' }, serverConfig); * ``` */ /** * Get content type for a file based on its extension * * @param filename - The filename or path * @returns MIME type string * * @example * ```typescript * getContentType('script.js') // Returns: 'application/javascript' * getContentType('styles.css') // Returns: 'text/css' * ``` */ export declare function getContentType(filename: string): string; /** * Process widget HTML with base URL injection and path conversion * * @param html - Original HTML content * @param widgetName - Widget identifier * @param baseUrl - Server base URL * @returns Processed HTML with injected base tag and absolute URLs * * @example * ```typescript * const html = '<html><head></head><body>...</body></html>'; * const processed = processWidgetHtml(html, 'kanban-board', 'http://localhost:3000'); * ``` */ export declare function processWidgetHtml(html: string, widgetName: string, baseUrl: string): string; /** * Create a widget registration object with standard metadata * * @param widgetName - Widget identifier * @param metadata - Widget metadata from file or manifest * @param html - Processed HTML template * @param serverConfig - Server configuration for CSP and URLs * @param isDev - Whether this is development mode * @returns Widget registration object * * @example * ```typescript * const registration = createWidgetRegistration( * 'kanban-board', * { title: 'Kanban Board', description: 'Task board' }, * '<html>...</html>', * { serverBaseUrl: 'http://localhost:3000', cspUrls: [] }, * true * ); * ``` */ export declare function createWidgetRegistration(widgetName: string, metadata: Record<string, unknown> | { title?: string; description?: string; props?: unknown; inputs?: unknown; schema?: unknown; [key: string]: unknown; }, html: string, serverConfig: { serverBaseUrl: string; cspUrls: string[]; }, isDev?: boolean): { name: string; title: string; description: string; type: "appsSdk"; props: import("../types/resource.js").WidgetProps; _meta: Record<string, unknown>; htmlTemplate: string; appsSdkMetadata: Record<string, any>; }; export declare function createWidgetUIResource(definition: UIResourceDefinition, params: Record<string, any>, serverConfig: WidgetServerConfig): Promise<UIResourceContent>; /** * Ensure widget metadata has proper fallback values * * @param metadata - Widget metadata object * @param widgetName - Widget identifier for fallback description * @param widgetDescription - Optional custom description * @returns Metadata with ensured description * * @example * ```typescript * const metadata = ensureWidgetMetadata({}, 'kanban-board'); * // Returns: { description: 'Widget: kanban-board' } * ``` */ export declare function ensureWidgetMetadata(metadata: Record<string, unknown>, widgetName: string, widgetDescription?: string): Record<string, unknown>; /** * Read widget HTML file with consistent error handling * * @param filePath - Path to the HTML file * @param widgetName - Widget identifier for error messages * @returns HTML content or empty string on error * * @example * ```typescript * const html = await readWidgetHtml('/path/to/widget/index.html', 'kanban-board'); * ``` */ export declare function readWidgetHtml(filePath: string, widgetName: string): Promise<string>; /** * Register a widget from its HTML template and metadata * * This function encapsulates the common pattern of registering a widget: * - Read and process HTML template * - Ensure metadata has proper fallbacks * - Create widget registration object * - Call the registration callback * * @param widgetName - Widget identifier * @param htmlPath - Path to the HTML template file * @param metadata - Widget metadata * @param serverConfig - Server configuration for CSP and URLs * @param registerWidget - Callback to register the widget * @param isDev - Whether this is development mode * @returns Promise that resolves when widget is registered * * @example * ```typescript * await registerWidgetFromTemplate( * 'kanban-board', * './dist/resources/widgets/kanban-board/index.html', * { title: 'Kanban Board' }, * serverConfig, * registerWidget, * false * ); * ``` */ export declare function registerWidgetFromTemplate(widgetName: string, htmlPath: string, metadata: Record<string, unknown>, serverConfig: { serverBaseUrl: string; cspUrls: string[]; }, registerWidget: import("./widget-types.js").RegisterWidgetCallback, isDev?: boolean): Promise<void>; /** * Setup static file serving routes for public files * * Creates an HTTP route to serve files from the public/ or dist/public/ directory. * This function encapsulates the common pattern of serving static files. * * @param app - Hono app instance to mount routes on * @param useDistDirectory - Whether to serve from dist/public (production) or public (dev) * * @example * ```typescript * // For development mode * setupPublicRoutes(app, false); * * // For production mode * setupPublicRoutes(app, true); * ``` */ export declare function setupPublicRoutes(app: HonoType, useDistDirectory?: boolean): void; /** * Setup favicon route at server root * * Serves the configured favicon file at /favicon.ico so it appears * for the entire server domain (e.g., aaa.bbb.com/favicon.ico) * * @param app - Hono app instance to mount routes on * @param faviconPath - Path to favicon file relative to public directory * @param useDistDirectory - Whether to serve from dist/public (production) or public (dev) * * @example * ```typescript * // For development mode * setupFaviconRoute(app, 'favicon.ico', false); * * // For production mode * setupFaviconRoute(app, 'favicon.ico', true); * ``` */ export declare function setupFaviconRoute(app: HonoType, faviconPath: string | undefined, useDistDirectory?: boolean): void; //# sourceMappingURL=widget-helpers.d.ts.map