@mcp-use/cli 2.8.4 → 2.9.0-canary.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -4794,11 +4794,12 @@ program.command("build").description("Build TypeScript and MCP UI widgets").opti
4794
4794
  process.exit(1);
4795
4795
  }
4796
4796
  });
4797
- program.command("dev").description("Run development server with auto-reload and inspector").option("-p, --path <path>", "Path to project directory", process.cwd()).option("--port <port>", "Server port", "3000").option("--host <host>", "Server host", "localhost").option("--no-open", "Do not auto-open inspector").action(async (options) => {
4797
+ program.command("dev").description("Run development server with auto-reload and inspector").option("-p, --path <path>", "Path to project directory", process.cwd()).option("--port <port>", "Server port", "3000").option("--host <host>", "Server host", "localhost").option("--no-open", "Do not auto-open inspector").option("--no-hmr", "Disable hot module reloading (use tsx watch instead)").action(async (options) => {
4798
4798
  try {
4799
4799
  const projectPath = import_node_path6.default.resolve(options.path);
4800
4800
  let port = parseInt(options.port, 10);
4801
4801
  const host = options.host;
4802
+ const useHmr = options.hmr !== false;
4802
4803
  console.log(source_default.cyan.bold(`mcp-use v${packageJson.version}`));
4803
4804
  if (!await isPortAvailable(port, host)) {
4804
4805
  console.log(source_default.yellow.bold(`\u26A0\uFE0F Port ${port} is already in use`));
@@ -4807,62 +4808,251 @@ program.command("dev").description("Run development server with auto-reload and
4807
4808
  port = availablePort;
4808
4809
  }
4809
4810
  const serverFile = await findServerFile(projectPath);
4810
- const processes = [];
4811
- const env2 = {
4812
- PORT: String(port),
4813
- HOST: host,
4814
- NODE_ENV: "development"
4815
- };
4816
- const serverCommand = runCommand(
4817
- "npx",
4818
- ["tsx", "watch", serverFile],
4819
- projectPath,
4820
- env2,
4821
- true
4811
+ process.env.PORT = String(port);
4812
+ process.env.HOST = host;
4813
+ process.env.NODE_ENV = "development";
4814
+ if (!useHmr) {
4815
+ console.log(source_default.gray("HMR disabled, using tsx watch (full restart)"));
4816
+ const processes = [];
4817
+ const env2 = {
4818
+ PORT: String(port),
4819
+ HOST: host,
4820
+ NODE_ENV: "development"
4821
+ };
4822
+ const { createRequire: createRequire2 } = await import("module");
4823
+ let cmd;
4824
+ let args;
4825
+ try {
4826
+ const projectRequire = createRequire2(
4827
+ import_node_path6.default.join(projectPath, "package.json")
4828
+ );
4829
+ const tsxPkgPath = projectRequire.resolve("tsx/package.json");
4830
+ const tsxPkg = JSON.parse(await (0, import_promises5.readFile)(tsxPkgPath, "utf-8"));
4831
+ let binPath;
4832
+ if (typeof tsxPkg.bin === "string") {
4833
+ binPath = tsxPkg.bin;
4834
+ } else if (tsxPkg.bin && typeof tsxPkg.bin === "object") {
4835
+ binPath = tsxPkg.bin.tsx || Object.values(tsxPkg.bin)[0];
4836
+ } else {
4837
+ throw new Error("No bin field found in tsx package.json");
4838
+ }
4839
+ const tsxBin = import_node_path6.default.resolve(import_node_path6.default.dirname(tsxPkgPath), binPath);
4840
+ cmd = "node";
4841
+ args = [tsxBin, "watch", serverFile];
4842
+ } catch (error) {
4843
+ console.log(
4844
+ source_default.yellow(
4845
+ `Could not resolve local tsx: ${error instanceof Error ? error.message : "unknown error"}`
4846
+ )
4847
+ );
4848
+ cmd = "npx";
4849
+ args = ["tsx", "watch", serverFile];
4850
+ }
4851
+ const serverCommand = runCommand(cmd, args, projectPath, env2, true);
4852
+ processes.push(serverCommand.process);
4853
+ if (options.open !== false) {
4854
+ const startTime = Date.now();
4855
+ const ready = await waitForServer(port, host);
4856
+ if (ready) {
4857
+ const mcpEndpoint = `http://${host}:${port}/mcp`;
4858
+ const inspectorUrl = `http://${host}:${port}/inspector?autoConnect=${encodeURIComponent(mcpEndpoint)}`;
4859
+ const readyTime = Date.now() - startTime;
4860
+ console.log(source_default.green.bold(`\u2713 Ready in ${readyTime}ms`));
4861
+ console.log(source_default.whiteBright(`Local: http://${host}:${port}`));
4862
+ console.log(source_default.whiteBright(`Network: http://${host}:${port}`));
4863
+ console.log(source_default.whiteBright(`MCP: ${mcpEndpoint}`));
4864
+ console.log(source_default.whiteBright(`Inspector: ${inspectorUrl}
4865
+ `));
4866
+ await open_default(inspectorUrl);
4867
+ }
4868
+ }
4869
+ const cleanup2 = () => {
4870
+ console.log(source_default.gray("\n\nShutting down..."));
4871
+ processes.forEach((proc) => {
4872
+ if (proc && typeof proc.kill === "function") {
4873
+ proc.kill("SIGINT");
4874
+ }
4875
+ });
4876
+ setTimeout(() => process.exit(0), 1e3);
4877
+ };
4878
+ process.on("SIGINT", cleanup2);
4879
+ process.on("SIGTERM", cleanup2);
4880
+ await new Promise(() => {
4881
+ });
4882
+ return;
4883
+ }
4884
+ console.log(
4885
+ source_default.gray(
4886
+ "HMR enabled - changes will hot reload without dropping connections"
4887
+ )
4822
4888
  );
4823
- processes.push(serverCommand.process);
4824
- if (options.open !== false) {
4889
+ const chokidarModule = await import("chokidar");
4890
+ const chokidar = chokidarModule.default || chokidarModule;
4891
+ const { pathToFileURL } = await import("url");
4892
+ const { createRequire } = await import("module");
4893
+ let tsImport = null;
4894
+ try {
4895
+ const projectRequire = createRequire(
4896
+ import_node_path6.default.join(projectPath, "package.json")
4897
+ );
4898
+ const tsxApiPath = projectRequire.resolve("tsx/esm/api");
4899
+ const tsxApi = await import(pathToFileURL(tsxApiPath).href);
4900
+ tsImport = tsxApi.tsImport;
4901
+ } catch {
4902
+ console.log(
4903
+ source_default.yellow(
4904
+ "Warning: tsx not found in project dependencies. TypeScript HMR may not work.\nAdd tsx to your devDependencies: npm install -D tsx"
4905
+ )
4906
+ );
4907
+ }
4908
+ const serverFilePath = import_node_path6.default.join(projectPath, serverFile);
4909
+ const serverFileUrl = pathToFileURL(serverFilePath).href;
4910
+ globalThis.__mcpUseHmrMode = true;
4911
+ const importServerModule = async () => {
4912
+ if (tsImport) {
4913
+ await tsImport(`${serverFilePath}?t=${Date.now()}`, importMetaUrl);
4914
+ } else {
4915
+ await import(`${serverFileUrl}?t=${Date.now()}`);
4916
+ }
4917
+ const instance = globalThis.__mcpUseLastServer;
4918
+ return instance || null;
4919
+ };
4920
+ console.log(source_default.gray(`Loading server from ${serverFile}...`));
4921
+ let runningServer;
4922
+ try {
4923
+ runningServer = await importServerModule();
4924
+ if (!runningServer) {
4925
+ console.error(
4926
+ source_default.red(
4927
+ "Error: Could not find MCPServer instance.\nMake sure your server file creates an MCPServer:\n const server = new MCPServer({ name: 'my-server', version: '1.0.0' });"
4928
+ )
4929
+ );
4930
+ process.exit(1);
4931
+ }
4932
+ if (typeof runningServer.listen !== "function") {
4933
+ console.error(
4934
+ source_default.red("Error: MCPServer instance must have a listen() method")
4935
+ );
4936
+ process.exit(1);
4937
+ }
4825
4938
  const startTime = Date.now();
4826
- const ready = await waitForServer(port, host);
4827
- if (ready) {
4828
- const mcpEndpoint = `http://${host}:${port}/mcp`;
4829
- const inspectorUrl = `http://${host}:${port}/inspector?autoConnect=${encodeURIComponent(mcpEndpoint)}`;
4830
- const readyTime = Date.now() - startTime;
4831
- console.log(source_default.green.bold(`\u2713 Ready in ${readyTime}ms`));
4832
- console.log(source_default.whiteBright(`Local: http://${host}:${port}`));
4833
- console.log(source_default.whiteBright(`Network: http://${host}:${port}`));
4834
- console.log(source_default.whiteBright(`MCP: ${mcpEndpoint}`));
4835
- console.log(source_default.whiteBright(`Inspector: ${inspectorUrl}
4939
+ globalThis.__mcpUseHmrMode = false;
4940
+ await runningServer.listen(port);
4941
+ globalThis.__mcpUseHmrMode = true;
4942
+ if (options.open !== false) {
4943
+ const ready = await waitForServer(port, host);
4944
+ if (ready) {
4945
+ const mcpEndpoint = `http://${host}:${port}/mcp`;
4946
+ const inspectorUrl = `http://${host}:${port}/inspector?autoConnect=${encodeURIComponent(mcpEndpoint)}`;
4947
+ const readyTime = Date.now() - startTime;
4948
+ console.log(source_default.green.bold(`\u2713 Ready in ${readyTime}ms`));
4949
+ console.log(source_default.whiteBright(`Local: http://${host}:${port}`));
4950
+ console.log(source_default.whiteBright(`Network: http://${host}:${port}`));
4951
+ console.log(source_default.whiteBright(`MCP: ${mcpEndpoint}`));
4952
+ console.log(source_default.whiteBright(`Inspector: ${inspectorUrl}`));
4953
+ console.log(source_default.gray(`Watching for changes...
4836
4954
  `));
4837
- await open_default(inspectorUrl);
4955
+ await open_default(inspectorUrl);
4956
+ }
4838
4957
  }
4958
+ } catch (error) {
4959
+ console.error(
4960
+ source_default.red("Failed to start server:"),
4961
+ error?.message || error
4962
+ );
4963
+ if (error?.stack) {
4964
+ console.error(source_default.gray(error.stack));
4965
+ }
4966
+ process.exit(1);
4839
4967
  }
4968
+ if (options.open === false) {
4969
+ const mcpEndpoint = `http://${host}:${port}/mcp`;
4970
+ console.log(source_default.green.bold(`\u2713 Server ready`));
4971
+ console.log(source_default.whiteBright(`Local: http://${host}:${port}`));
4972
+ console.log(source_default.whiteBright(`MCP: ${mcpEndpoint}`));
4973
+ console.log(source_default.gray(`Watching for changes...
4974
+ `));
4975
+ }
4976
+ const watcher = chokidar.watch(".", {
4977
+ cwd: projectPath,
4978
+ ignored: [
4979
+ /(^|[/\\])\../,
4980
+ // dotfiles
4981
+ "**/node_modules/**",
4982
+ "**/dist/**",
4983
+ "**/resources/**",
4984
+ // widgets are watched separately by vite
4985
+ "**/*.d.ts"
4986
+ // type definitions
4987
+ ],
4988
+ persistent: true,
4989
+ ignoreInitial: true,
4990
+ depth: 3
4991
+ // Limit depth to avoid watching too many files
4992
+ });
4993
+ watcher.on("ready", () => {
4994
+ const watched = watcher.getWatched();
4995
+ const dirs = Object.keys(watched);
4996
+ console.log(
4997
+ source_default.gray(`[HMR] Watcher ready, watching ${dirs.length} paths`)
4998
+ );
4999
+ }).on("error", (error) => {
5000
+ console.error(
5001
+ source_default.red(
5002
+ `[HMR] Watcher error: ${error instanceof Error ? error.message : String(error)}`
5003
+ )
5004
+ );
5005
+ });
5006
+ let reloadTimeout = null;
5007
+ let isReloading = false;
5008
+ watcher.on("change", async (filePath) => {
5009
+ if (!filePath.endsWith(".ts") && !filePath.endsWith(".tsx") || filePath.endsWith(".d.ts")) {
5010
+ return;
5011
+ }
5012
+ if (isReloading) return;
5013
+ if (reloadTimeout) {
5014
+ clearTimeout(reloadTimeout);
5015
+ }
5016
+ reloadTimeout = setTimeout(async () => {
5017
+ isReloading = true;
5018
+ console.log(source_default.yellow(`
5019
+ [HMR] File changed: ${filePath}`));
5020
+ try {
5021
+ const newServer = await importServerModule();
5022
+ if (!newServer) {
5023
+ console.warn(
5024
+ source_default.yellow(
5025
+ "[HMR] Warning: No MCPServer instance found after reload, skipping"
5026
+ )
5027
+ );
5028
+ isReloading = false;
5029
+ return;
5030
+ }
5031
+ if (typeof runningServer.syncRegistrationsFrom !== "function") {
5032
+ console.warn(
5033
+ source_default.yellow(
5034
+ "[HMR] Warning: Server does not support hot reload (missing syncRegistrationsFrom)"
5035
+ )
5036
+ );
5037
+ isReloading = false;
5038
+ return;
5039
+ }
5040
+ runningServer.syncRegistrationsFrom(newServer);
5041
+ console.log(
5042
+ source_default.green(
5043
+ `[HMR] \u2713 Reloaded: ${runningServer.registeredTools?.length || 0} tools, ${runningServer.registeredPrompts?.length || 0} prompts, ${runningServer.registeredResources?.length || 0} resources`
5044
+ )
5045
+ );
5046
+ } catch (error) {
5047
+ console.error(source_default.red(`[HMR] Reload failed: ${error.message}`));
5048
+ }
5049
+ isReloading = false;
5050
+ }, 100);
5051
+ });
4840
5052
  const cleanup = () => {
4841
5053
  console.log(source_default.gray("\n\nShutting down..."));
4842
- const processesToKill = processes.length;
4843
- let killedCount = 0;
4844
- const checkAndExit = () => {
4845
- killedCount++;
4846
- if (killedCount >= processesToKill) {
4847
- process.exit(0);
4848
- }
4849
- };
4850
- processes.forEach((proc) => {
4851
- if (proc && typeof proc.kill === "function") {
4852
- proc.on("exit", checkAndExit);
4853
- proc.kill("SIGINT");
4854
- } else {
4855
- checkAndExit();
4856
- }
4857
- });
4858
- setTimeout(() => {
4859
- processes.forEach((proc) => {
4860
- if (proc && typeof proc.kill === "function" && proc.exitCode === null) {
4861
- proc.kill("SIGKILL");
4862
- }
4863
- });
4864
- process.exit(0);
4865
- }, 1e3);
5054
+ watcher.close();
5055
+ process.exit(0);
4866
5056
  };
4867
5057
  process.on("SIGINT", cleanup);
4868
5058
  process.on("SIGTERM", cleanup);