|
| 1 | +/** |
| 2 | + * Self-check for outdated installs of @aztec/mcp-server. |
| 3 | + * |
| 4 | + * Why: npx caches packages, and users frequently end up running an old |
| 5 | + * version while assuming `npx @aztec/mcp-server` always pulls the latest. |
| 6 | + * The result is silently-degraded behavior + bug reports against fixes |
| 7 | + * that have already shipped. This module fetches the current latest tag |
| 8 | + * from the npm registry at startup, compares against the running |
| 9 | + * version, and surfaces a warning into both the MCP `instructions` |
| 10 | + * banner (so the LLM tells the user) and `aztec_status` (so a curious |
| 11 | + * user running diagnostics also sees it). |
| 12 | + * |
| 13 | + * Failure modes are silent (registry down, no network, slow response) |
| 14 | + * — the check should never block startup or fail the server. Worst |
| 15 | + * case: no banner, business as usual. |
| 16 | + */ |
| 17 | + |
| 18 | +const NPM_REGISTRY_URL = "https://registry.npmjs.org/@aztec/mcp-server/latest"; |
| 19 | + |
| 20 | +export interface UpgradeInfo { |
| 21 | + current: string; |
| 22 | + latest: string; |
| 23 | + outdated: boolean; |
| 24 | +} |
| 25 | + |
| 26 | +let upgradeInfoCache: UpgradeInfo | null = null; |
| 27 | + |
| 28 | +/** |
| 29 | + * Test-only: reset the module-level upgrade cache between tests. |
| 30 | + */ |
| 31 | +export function _resetUpgradeCache(): void { |
| 32 | + upgradeInfoCache = null; |
| 33 | +} |
| 34 | + |
| 35 | +export function setUpgradeInfo(info: UpgradeInfo | null): void { |
| 36 | + upgradeInfoCache = info; |
| 37 | +} |
| 38 | + |
| 39 | +export function getUpgradeInfo(): UpgradeInfo | null { |
| 40 | + return upgradeInfoCache; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Fetch the latest published version of @aztec/mcp-server from npm. |
| 45 | + * Returns null on any failure (network, timeout, malformed body) — |
| 46 | + * never throws, so callers don't have to wrap in try/catch. |
| 47 | + */ |
| 48 | +export async function fetchLatestNpmVersion( |
| 49 | + timeoutMs: number = 2000, |
| 50 | + fetchImpl: typeof fetch = fetch |
| 51 | +): Promise<string | null> { |
| 52 | + const ctl = new AbortController(); |
| 53 | + const timer = setTimeout(() => ctl.abort(), timeoutMs); |
| 54 | + // `unref` (Node-only) prevents this timer from keeping the event |
| 55 | + // loop alive on its own. Critical for short-lived processes and |
| 56 | + // tests where a forgotten timer would block exit. Optional-chained |
| 57 | + // because `setTimeout` in browser-shaped environments returns a |
| 58 | + // primitive number with no `unref` — the optional call is safe. |
| 59 | + (timer as unknown as { unref?: () => void }).unref?.(); |
| 60 | + try { |
| 61 | + const resp = await fetchImpl(NPM_REGISTRY_URL, { signal: ctl.signal }); |
| 62 | + if (!resp.ok) return null; |
| 63 | + const data = await resp.json(); |
| 64 | + if (!data || typeof data !== "object") return null; |
| 65 | + const v = (data as Record<string, unknown>).version; |
| 66 | + return typeof v === "string" ? v : null; |
| 67 | + } catch { |
| 68 | + return null; |
| 69 | + } finally { |
| 70 | + // Always clear: the previous implementation only cleared on the |
| 71 | + // success path, leaking the timer when `fetchImpl` rejected |
| 72 | + // (network error, CORS, malformed body) before the timeout |
| 73 | + // fired. Combined with `unref` above this is belt-and-braces. |
| 74 | + clearTimeout(timer); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Numeric major.minor.patch comparison. Strips a leading ``v`` and |
| 80 | + * any pre-release / build suffix (so ``1.20.0-rc.1`` and ``1.20.0`` |
| 81 | + * compare equal — we don't want to flag a stable user as outdated |
| 82 | + * relative to a pre-release on npm). |
| 83 | + */ |
| 84 | +export function compareSemver(a: string, b: string): -1 | 0 | 1 { |
| 85 | + const parse = (v: string): number[] => { |
| 86 | + const core = v.replace(/^v/, "").split("-")[0].split("+")[0]; |
| 87 | + return core.split(".").map((p) => { |
| 88 | + const n = parseInt(p, 10); |
| 89 | + return Number.isFinite(n) ? n : 0; |
| 90 | + }); |
| 91 | + }; |
| 92 | + const ma = parse(a); |
| 93 | + const mb = parse(b); |
| 94 | + for (let i = 0; i < Math.max(ma.length, mb.length); i++) { |
| 95 | + const x = ma[i] ?? 0; |
| 96 | + const y = mb[i] ?? 0; |
| 97 | + if (x < y) return -1; |
| 98 | + if (x > y) return 1; |
| 99 | + } |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * High-level entry: fetch + compare + cache. Returns the populated |
| 105 | + * cache entry (also retrievable via ``getUpgradeInfo()``). |
| 106 | + */ |
| 107 | +export async function checkForUpgrade( |
| 108 | + currentVersion: string, |
| 109 | + options: { timeoutMs?: number; fetchImpl?: typeof fetch } = {} |
| 110 | +): Promise<UpgradeInfo | null> { |
| 111 | + const latest = await fetchLatestNpmVersion( |
| 112 | + options.timeoutMs, |
| 113 | + options.fetchImpl ?? fetch |
| 114 | + ); |
| 115 | + if (!latest) { |
| 116 | + setUpgradeInfo(null); |
| 117 | + return null; |
| 118 | + } |
| 119 | + const info: UpgradeInfo = { |
| 120 | + current: currentVersion, |
| 121 | + latest, |
| 122 | + outdated: compareSemver(currentVersion, latest) < 0, |
| 123 | + }; |
| 124 | + setUpgradeInfo(info); |
| 125 | + return info; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Format the upgrade warning that gets appended to the MCP server |
| 130 | + * instructions banner. The text is consumed by the LLM, not directly |
| 131 | + * by a human, so it explains what the LLM should *do*: tell the user |
| 132 | + * to update. Listed remediation commands match the install paths |
| 133 | + * documented in the README so the LLM can copy-paste them. |
| 134 | + */ |
| 135 | +export function formatUpgradeBanner(info: UpgradeInfo): string { |
| 136 | + return [ |
| 137 | + "", |
| 138 | + "", |
| 139 | + `⚠️ UPDATE AVAILABLE: this MCP server is running v${info.current}, but v${info.latest} is the current release on npm. ` + |
| 140 | + `Tell the user they're on an outdated version, and that bug reports about behavior may already be fixed in the latest release. ` + |
| 141 | + `To upgrade, ensure their MCP client config uses \`@aztec/mcp-server@latest\` so npx fetches the newest:`, |
| 142 | + ` • Claude Desktop / Cursor / Codex: change the args to ` + |
| 143 | + `["-y", "@aztec/mcp-server@latest"] in the MCP server config and restart the client.`, |
| 144 | + ` • Claude Code: \`claude mcp remove aztec-docs && claude mcp add aztec-docs ... -- npx -y @aztec/mcp-server@latest\``, |
| 145 | + ` • If installed globally: \`npm uninstall -g @aztec/mcp-server && npm install -g @aztec/mcp-server@latest\` (or just rely on npx).`, |
| 146 | + ].join("\n"); |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * Format a one-line upgrade summary suitable for inclusion in the |
| 151 | + * ``aztec_status`` output. Returns the empty string when the install |
| 152 | + * is current (so the formatter can unconditionally include it). |
| 153 | + */ |
| 154 | +export function formatUpgradeStatusLine(info: UpgradeInfo | null): string { |
| 155 | + if (!info) return ""; |
| 156 | + if (!info.outdated) { |
| 157 | + return `npm latest: v${info.latest} (you are up to date)`; |
| 158 | + } |
| 159 | + return ( |
| 160 | + `⚠️ UPDATE AVAILABLE: v${info.current} → v${info.latest} on npm. ` + |
| 161 | + `Switch your MCP config to \`@aztec/mcp-server@latest\` and restart the client.` |
| 162 | + ); |
| 163 | +} |
0 commit comments