|
| 1 | +/** |
| 2 | + * Validates Cursor/Claude plugin manifests and MCP config files. |
| 3 | + * |
| 4 | + * Checks: |
| 5 | + * - `.cursor-plugin/plugin.json` and `.cursor-plugin/marketplace.json` parse |
| 6 | + * and have required fields. |
| 7 | + * - `.claude-plugin/plugin.json` and `.claude-plugin/marketplace.json` parse. |
| 8 | + * - `mcp.json` and `.mcp.json` exist and have identical `mcpServers` content. |
| 9 | + * - Declared skill paths exist on disk. |
| 10 | + * |
| 11 | + * Exit code is non-zero on any failure. |
| 12 | + */ |
| 13 | + |
| 14 | +import { existsSync, readFileSync } from "node:fs"; |
| 15 | +import { dirname, resolve } from "node:path"; |
| 16 | +import { fileURLToPath } from "node:url"; |
| 17 | + |
| 18 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 19 | +const REPO_ROOT = resolve(HERE, "../.."); |
| 20 | + |
| 21 | +const PLUGIN_NAME_RE = /^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$/; |
| 22 | + |
| 23 | +interface Issue { |
| 24 | + file: string; |
| 25 | + message: string; |
| 26 | +} |
| 27 | + |
| 28 | +const issues: Issue[] = []; |
| 29 | + |
| 30 | +function fail(file: string, message: string): void { |
| 31 | + issues.push({ file, message }); |
| 32 | +} |
| 33 | + |
| 34 | +function readJson(file: string): unknown { |
| 35 | + const path = resolve(REPO_ROOT, file); |
| 36 | + if (!existsSync(path)) { |
| 37 | + fail(file, "file is missing"); |
| 38 | + return undefined; |
| 39 | + } |
| 40 | + try { |
| 41 | + return JSON.parse(readFileSync(path, "utf8")); |
| 42 | + } catch (error) { |
| 43 | + fail(file, `invalid JSON: ${error instanceof Error ? error.message : String(error)}`); |
| 44 | + return undefined; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 49 | + return typeof value === "object" && value !== null && !Array.isArray(value); |
| 50 | +} |
| 51 | + |
| 52 | +function assertPluginName(file: string, name: unknown): void { |
| 53 | + if (typeof name !== "string" || name.length === 0) { |
| 54 | + fail(file, "`name` must be a non-empty string"); |
| 55 | + return; |
| 56 | + } |
| 57 | + if (!PLUGIN_NAME_RE.test(name)) { |
| 58 | + fail(file, `\`name\` "${name}" must be lowercase kebab-case`); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function assertSkillPaths(file: string, skills: unknown): void { |
| 63 | + if (skills === undefined) return; |
| 64 | + const paths = Array.isArray(skills) ? skills : [skills]; |
| 65 | + for (const entry of paths) { |
| 66 | + if (typeof entry !== "string") { |
| 67 | + fail(file, "`skills` entries must be strings"); |
| 68 | + continue; |
| 69 | + } |
| 70 | + const skillDir = resolve(REPO_ROOT, entry); |
| 71 | + const skillMd = resolve(skillDir, "SKILL.md"); |
| 72 | + if (!existsSync(skillMd)) { |
| 73 | + fail(file, `skill path "${entry}" is missing SKILL.md`); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function validateCursorPlugin(): void { |
| 79 | + const file = ".cursor-plugin/plugin.json"; |
| 80 | + const manifest = readJson(file); |
| 81 | + if (!isRecord(manifest)) return; |
| 82 | + |
| 83 | + assertPluginName(file, manifest.name); |
| 84 | + assertSkillPaths(file, manifest.skills); |
| 85 | + |
| 86 | + if ("mcpServers" in manifest) { |
| 87 | + fail( |
| 88 | + file, |
| 89 | + "omit `mcpServers` — Cursor auto-discovers `mcp.json` at the plugin root", |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +function validateCursorMarketplace(): void { |
| 95 | + const file = ".cursor-plugin/marketplace.json"; |
| 96 | + const manifest = readJson(file); |
| 97 | + if (!isRecord(manifest)) return; |
| 98 | + |
| 99 | + assertPluginName(file, manifest.name); |
| 100 | + |
| 101 | + if (!isRecord(manifest.owner) || typeof manifest.owner.name !== "string") { |
| 102 | + fail(file, "`owner.name` is required"); |
| 103 | + } |
| 104 | + |
| 105 | + if (!Array.isArray(manifest.plugins) || manifest.plugins.length === 0) { |
| 106 | + fail(file, "`plugins` must be a non-empty array"); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + for (const entry of manifest.plugins) { |
| 111 | + if (!isRecord(entry)) { |
| 112 | + fail(file, "each `plugins` entry must be an object"); |
| 113 | + continue; |
| 114 | + } |
| 115 | + assertPluginName(file, entry.name); |
| 116 | + if (typeof entry.source !== "string" || entry.source.length === 0) { |
| 117 | + fail(file, "each `plugins` entry needs a `source` path"); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +function validateClaudePlugin(): void { |
| 123 | + const file = ".claude-plugin/plugin.json"; |
| 124 | + const manifest = readJson(file); |
| 125 | + if (!isRecord(manifest)) return; |
| 126 | + |
| 127 | + assertPluginName(file, manifest.name); |
| 128 | + assertSkillPaths(file, manifest.skills); |
| 129 | +} |
| 130 | + |
| 131 | +function validateClaudeMarketplace(): void { |
| 132 | + const file = ".claude-plugin/marketplace.json"; |
| 133 | + const manifest = readJson(file); |
| 134 | + if (!isRecord(manifest)) return; |
| 135 | + |
| 136 | + assertPluginName(file, manifest.name); |
| 137 | + |
| 138 | + if (!isRecord(manifest.owner) || typeof manifest.owner.name !== "string") { |
| 139 | + fail(file, "`owner.name` is required"); |
| 140 | + } |
| 141 | + |
| 142 | + if (!Array.isArray(manifest.plugins) || manifest.plugins.length === 0) { |
| 143 | + fail(file, "`plugins` must be a non-empty array"); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +function validateMcpConfigs(): void { |
| 148 | + const cursorMcp = readJson("mcp.json"); |
| 149 | + const claudeMcp = readJson(".mcp.json"); |
| 150 | + if (!isRecord(cursorMcp) || !isRecord(claudeMcp)) return; |
| 151 | + |
| 152 | + const cursorServers = cursorMcp.mcpServers; |
| 153 | + const claudeServers = claudeMcp.mcpServers; |
| 154 | + |
| 155 | + if (!isRecord(cursorServers)) { |
| 156 | + fail("mcp.json", "`mcpServers` must be an object"); |
| 157 | + return; |
| 158 | + } |
| 159 | + if (!isRecord(claudeServers)) { |
| 160 | + fail(".mcp.json", "`mcpServers` must be an object"); |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + const cursorJson = JSON.stringify(cursorServers); |
| 165 | + const claudeJson = JSON.stringify(claudeServers); |
| 166 | + if (cursorJson !== claudeJson) { |
| 167 | + fail("mcp.json", "`mcpServers` must match `.mcp.json` exactly"); |
| 168 | + } |
| 169 | + |
| 170 | + const context7 = cursorServers.context7; |
| 171 | + if (!isRecord(context7)) { |
| 172 | + fail("mcp.json", "`mcpServers.context7` is required"); |
| 173 | + return; |
| 174 | + } |
| 175 | + if (context7.url !== "https://mcp.context7.com/mcp") { |
| 176 | + fail("mcp.json", "`mcpServers.context7.url` must point at Context7"); |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +function main(): void { |
| 181 | + validateCursorPlugin(); |
| 182 | + validateCursorMarketplace(); |
| 183 | + validateClaudePlugin(); |
| 184 | + validateClaudeMarketplace(); |
| 185 | + validateMcpConfigs(); |
| 186 | + |
| 187 | + if (issues.length === 0) { |
| 188 | + console.log("validate-plugins: OK"); |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + console.error(`validate-plugins: ${issues.length} issue(s)\n`); |
| 193 | + for (const issue of issues) { |
| 194 | + console.error(` ${issue.file}: ${issue.message}`); |
| 195 | + } |
| 196 | + process.exit(1); |
| 197 | +} |
| 198 | + |
| 199 | +main(); |
0 commit comments