|
| 1 | +import type { Registry, RegistryItem } from 'shadcn-vue/schema' |
| 2 | +import type { ZodRawShape } from 'zod' |
| 3 | +import { fromWebHandler } from 'h3' |
| 4 | +import { createMcpHandler } from 'mcp-handler' |
| 5 | +import { useStorage } from 'nitropack/runtime' |
| 6 | +import { z } from 'zod' |
| 7 | + |
| 8 | +const REGISTRY_STORAGE_BASE = 'assets:registry' |
| 9 | +const REGISTRY_INDEX_FILE = 'index.json' |
| 10 | + |
| 11 | +// Parameter Schemas |
| 12 | +const componentParamsShape = { |
| 13 | + component: z.string().min(1, 'component is required'), |
| 14 | +} satisfies ZodRawShape |
| 15 | + |
| 16 | +const componentParamsSchema = z.object(componentParamsShape) |
| 17 | + |
| 18 | +// Data Access Layer |
| 19 | +function getRegistryStorage() { |
| 20 | + return useStorage(REGISTRY_STORAGE_BASE) |
| 21 | +} |
| 22 | + |
| 23 | +async function loadRegistryIndex(): Promise<Registry | null> { |
| 24 | + try { |
| 25 | + const storage = getRegistryStorage() |
| 26 | + return await storage.getItem(REGISTRY_INDEX_FILE) as Registry |
| 27 | + } |
| 28 | + catch (error) { |
| 29 | + console.error('Failed to read registry index', error) |
| 30 | + return null |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function listComponentNames(): Promise<string[]> { |
| 35 | + const index = await loadRegistryIndex() |
| 36 | + if (!index?.items) { |
| 37 | + return [] |
| 38 | + } |
| 39 | + |
| 40 | + return index.items |
| 41 | + .filter(item => item.type === 'registry:component') |
| 42 | + .map(item => item.name) |
| 43 | + .sort((a, b) => a.localeCompare(b)) |
| 44 | +} |
| 45 | + |
| 46 | +async function loadRegistryItem(name: string): Promise<RegistryItem | null> { |
| 47 | + const storage = getRegistryStorage() |
| 48 | + // Normalize and sanitize input |
| 49 | + const normalized = name.replace(/\.json$/i, '') |
| 50 | + |
| 51 | + try { |
| 52 | + const componentJson = await storage.getItem(`components/${normalized}.json`) as RegistryItem |
| 53 | + if (componentJson) { |
| 54 | + return componentJson |
| 55 | + } |
| 56 | + } |
| 57 | + catch (error) { |
| 58 | + console.error(`Failed to read component ${normalized}.json`, error) |
| 59 | + } |
| 60 | + |
| 61 | + return null |
| 62 | +} |
| 63 | + |
| 64 | +// Tool Handlers |
| 65 | +async function handleListComponents() { |
| 66 | + const componentNames = await listComponentNames() |
| 67 | + const body = componentNames.length |
| 68 | + ? JSON.stringify(componentNames, null, 2) |
| 69 | + : '[]' |
| 70 | + |
| 71 | + return { |
| 72 | + content: [{ type: 'text' as const, text: body }], |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +async function handleGetComponent(args: Record<string, unknown>) { |
| 77 | + const parsedArgs = componentParamsSchema.safeParse(args) |
| 78 | + if (!parsedArgs.success) { |
| 79 | + return { |
| 80 | + content: [{ type: 'text' as const, text: `Invalid input: ${parsedArgs.error.message}` }], |
| 81 | + isError: true, |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const { component } = parsedArgs.data |
| 86 | + const registryItem = await loadRegistryItem(component) |
| 87 | + |
| 88 | + if (!registryItem) { |
| 89 | + return { |
| 90 | + content: [{ type: 'text' as const, text: `Component "${component}" not found.` }], |
| 91 | + isError: true, |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return { |
| 96 | + content: [{ type: 'text' as const, text: JSON.stringify(registryItem, null, 2) }], |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// Main Handler |
| 101 | +const handler = createMcpHandler( |
| 102 | + (server) => { |
| 103 | + server.registerTool( |
| 104 | + 'get_ai_elements_components', |
| 105 | + { |
| 106 | + title: 'List AI Elements components', |
| 107 | + description: 'Provides a list of all AI Elements components.', |
| 108 | + }, |
| 109 | + handleListComponents, |
| 110 | + ) |
| 111 | + |
| 112 | + server.registerTool( |
| 113 | + 'get_ai_elements_component', |
| 114 | + { |
| 115 | + title: 'Get AI Elements component', |
| 116 | + description: 'Provides information about an AI Elements component.', |
| 117 | + inputSchema: componentParamsShape, |
| 118 | + }, |
| 119 | + handleGetComponent, |
| 120 | + ) |
| 121 | + }, |
| 122 | +) |
| 123 | + |
| 124 | +export default fromWebHandler(handler) |
0 commit comments