Files
webstudio/packages/cli/src/cli-update.ts
T
Oleg Isonen 11c31bdc25 feat: Warn MCP agents when a newer CLI is available (#6462)
## What changes

When MCP starts, the CLI checks npm's `latest` release while the project
session and API contract are loading. If a newer CLI is available, the
MCP initialization instructions tell the connected agent which versions
are running and recommend restarting with the latest CLI. The CLI never
updates or restarts itself.

The registry request has a 1.5-second timeout and fails open on network
or response errors. It may add up to 1.5 seconds to startup if the
project session finishes before the check; the existing server
compatibility check remains the authority for whether writes are
supported.

## Verification

- [x] Added tests for newer, same/older, invalid, and unavailable
registry responses; the newer-version test failed when the check was
temporarily disabled.
- [x] Verified the update notice reaches the connected MCP client; the
handshake test failed when the notice was temporarily omitted.
- [x] Full CLI suite: 1,159 tests passed.
- [x] CLI and project-build typechecks passed; focused MCP
initialization integration test passed.
- [x] Lint, formatting, and diff checks passed.
- [x] Documentation impact reviewed: no docs change is needed because
the notice is sent dynamically at MCP startup.
- [ ] Full Luna/low agent evaluation suite: approval requested, not
received; not run.

Ref #6461
2026-09-28 16:40:01 +02:00

54 lines
1.2 KiB
TypeScript

import semver from "semver";
import { isPlainRecord } from "./type-utils";
const latestPackageUrl = "https://registry.npmjs.org/webstudio/latest";
const requestTimeoutMs = 1_500;
export type CliUpdate = {
currentVersion: string;
latestVersion: string;
};
export const checkForCliUpdate = async ({
currentVersion,
request = fetch,
}: {
currentVersion: string;
request?: typeof fetch;
}): Promise<CliUpdate | undefined> => {
if (
currentVersion === "0.0.0-webstudio-version" ||
semver.valid(currentVersion) === null
) {
return;
}
try {
const response = await request(latestPackageUrl, {
signal: AbortSignal.timeout(requestTimeoutMs),
});
if (response.ok === false) {
return;
}
const latestPackage: unknown = await response.json();
if (isPlainRecord(latestPackage) === false) {
return;
}
const latestVersion =
typeof latestPackage.version === "string"
? semver.valid(latestPackage.version)
: null;
if (
latestVersion === null ||
semver.gt(latestVersion, currentVersion) === false
) {
return;
}
return { currentVersion, latestVersion };
} catch {
return;
}
};