|
| 1 | +import { readFile, writeFile } from "node:fs/promises"; |
| 2 | +import { resolve } from "node:path"; |
| 3 | +import { pathToFileURL } from "node:url"; |
| 4 | + |
| 5 | +const manifestSchema = |
| 6 | + "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json"; |
| 7 | +const repositoryUrl = "https://github.com/chrisdoc/hevy-mcp"; |
| 8 | + |
| 9 | +function assert(condition, message) { |
| 10 | + if (!condition) { |
| 11 | + throw new Error(message); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +async function readJson(path, label) { |
| 16 | + let contents; |
| 17 | + try { |
| 18 | + contents = await readFile(path, "utf8"); |
| 19 | + } catch (error) { |
| 20 | + throw new Error(`Unable to read ${label}: ${error.message}`); |
| 21 | + } |
| 22 | + |
| 23 | + try { |
| 24 | + return { contents, value: JSON.parse(contents) }; |
| 25 | + } catch (error) { |
| 26 | + throw new Error(`${label} is not valid JSON: ${error.message}`); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function validatePackageJson(packageJson) { |
| 31 | + assert( |
| 32 | + packageJson?.name === "hevy-mcp", |
| 33 | + "package.json name must be hevy-mcp", |
| 34 | + ); |
| 35 | + assert( |
| 36 | + packageJson.mcpName === "io.github.chrisdoc/hevy-mcp", |
| 37 | + "package.json mcpName must be io.github.chrisdoc/hevy-mcp", |
| 38 | + ); |
| 39 | + assert( |
| 40 | + typeof packageJson.version === "string" && packageJson.version.length > 0, |
| 41 | + "package.json version must be a non-empty string", |
| 42 | + ); |
| 43 | + assert( |
| 44 | + Array.isArray(packageJson.files) && |
| 45 | + packageJson.files.includes("server.json"), |
| 46 | + "package.json files must include server.json", |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +function validateManifestShape(manifest) { |
| 51 | + assert( |
| 52 | + manifest && typeof manifest === "object" && !Array.isArray(manifest), |
| 53 | + "server.json must contain an object", |
| 54 | + ); |
| 55 | + assert( |
| 56 | + manifest.$schema === manifestSchema, |
| 57 | + "server.json has an unexpected $schema", |
| 58 | + ); |
| 59 | + assert( |
| 60 | + manifest.title === "Hevy MCP Server", |
| 61 | + "server.json has an unexpected title", |
| 62 | + ); |
| 63 | + assert( |
| 64 | + manifest.description === |
| 65 | + "MCP server for managing workouts, routines, and exercise data through the Hevy API", |
| 66 | + "server.json has an unexpected description", |
| 67 | + ); |
| 68 | + assert( |
| 69 | + manifest.repository?.url === repositoryUrl && |
| 70 | + manifest.repository?.source === "github", |
| 71 | + "server.json has unexpected repository metadata", |
| 72 | + ); |
| 73 | + assert( |
| 74 | + Array.isArray(manifest.packages) && manifest.packages.length === 1, |
| 75 | + "server.json must contain exactly one package", |
| 76 | + ); |
| 77 | + |
| 78 | + const [packageEntry] = manifest.packages; |
| 79 | + assert( |
| 80 | + packageEntry.registryType === "npm", |
| 81 | + "server.json package registryType must be npm", |
| 82 | + ); |
| 83 | + assert( |
| 84 | + packageEntry.transport?.type === "stdio", |
| 85 | + "server.json package transport must be stdio", |
| 86 | + ); |
| 87 | + assert( |
| 88 | + Array.isArray(packageEntry.environmentVariables) && |
| 89 | + packageEntry.environmentVariables.length === 1, |
| 90 | + "server.json package must declare exactly one environment variable", |
| 91 | + ); |
| 92 | + |
| 93 | + const [apiKey] = packageEntry.environmentVariables; |
| 94 | + assert( |
| 95 | + apiKey.name === "HEVY_API_KEY" && |
| 96 | + apiKey.description === "API key for authenticating with the Hevy API" && |
| 97 | + apiKey.isRequired === true && |
| 98 | + apiKey.isSecret === true, |
| 99 | + "server.json has unexpected HEVY_API_KEY metadata", |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +function findDrift(packageJson, manifest) { |
| 104 | + const packageEntry = manifest.packages[0]; |
| 105 | + const drift = []; |
| 106 | + |
| 107 | + if (manifest.name !== packageJson.mcpName) { |
| 108 | + drift.push("name"); |
| 109 | + } |
| 110 | + if (manifest.version !== packageJson.version) { |
| 111 | + drift.push("version"); |
| 112 | + } |
| 113 | + if (packageEntry.identifier !== packageJson.name) { |
| 114 | + drift.push("packages[0].identifier"); |
| 115 | + } |
| 116 | + if (packageEntry.version !== packageJson.version) { |
| 117 | + drift.push("packages[0].version"); |
| 118 | + } |
| 119 | + |
| 120 | + return drift; |
| 121 | +} |
| 122 | + |
| 123 | +export async function runServerManifest({ mode, rootDir = process.cwd() }) { |
| 124 | + assert( |
| 125 | + mode === "check" || mode === "sync", |
| 126 | + `Invalid mode ${JSON.stringify(mode)}; expected "check" or "sync"`, |
| 127 | + ); |
| 128 | + |
| 129 | + const packagePath = resolve(rootDir, "package.json"); |
| 130 | + const manifestPath = resolve(rootDir, "server.json"); |
| 131 | + const [{ value: packageJson }, { value: manifest }] = await Promise.all([ |
| 132 | + readJson(packagePath, "package.json"), |
| 133 | + readJson(manifestPath, "server.json"), |
| 134 | + ]); |
| 135 | + |
| 136 | + validatePackageJson(packageJson); |
| 137 | + validateManifestShape(manifest); |
| 138 | + |
| 139 | + const drift = findDrift(packageJson, manifest); |
| 140 | + if (mode === "check") { |
| 141 | + assert( |
| 142 | + drift.length === 0, |
| 143 | + `server.json is out of sync with package.json: ${drift.join(", ")}. Run npm run sync:server-manifest.`, |
| 144 | + ); |
| 145 | + return { changed: false, drift }; |
| 146 | + } |
| 147 | + |
| 148 | + if (drift.length === 0) { |
| 149 | + return { changed: false, drift }; |
| 150 | + } |
| 151 | + |
| 152 | + manifest.name = packageJson.mcpName; |
| 153 | + manifest.version = packageJson.version; |
| 154 | + manifest.packages[0].identifier = packageJson.name; |
| 155 | + manifest.packages[0].version = packageJson.version; |
| 156 | + |
| 157 | + const updatedContents = `${JSON.stringify(manifest, null, "\t")}\n`; |
| 158 | + await writeFile(manifestPath, updatedContents, "utf8"); |
| 159 | + |
| 160 | + return { changed: true, drift }; |
| 161 | +} |
| 162 | + |
| 163 | +const isCli = |
| 164 | + process.argv[1] !== undefined && |
| 165 | + import.meta.url === pathToFileURL(resolve(process.argv[1])).href; |
| 166 | + |
| 167 | +if (isCli) { |
| 168 | + try { |
| 169 | + const result = await runServerManifest({ mode: process.argv[2] }); |
| 170 | + console.log( |
| 171 | + result.changed |
| 172 | + ? "Synchronized server.json with package.json." |
| 173 | + : "server.json is synchronized with package.json.", |
| 174 | + ); |
| 175 | + } catch (error) { |
| 176 | + console.error(`server-manifest: ${error.message}`); |
| 177 | + process.exitCode = 1; |
| 178 | + } |
| 179 | +} |
0 commit comments