@mcp-use/cli 3.1.0-canary.3 → 3.1.0-canary.5

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.js CHANGED
@@ -1508,6 +1508,28 @@ var McpUseAPI = class _McpUseAPI {
1508
1508
  }
1509
1509
  };
1510
1510
 
1511
+ // src/utils/errors.ts
1512
+ function handleCommandError(error, context) {
1513
+ if (error instanceof ApiUnauthorizedError) {
1514
+ console.error(
1515
+ source_default.red("\n\u2717 Your session has expired or your API key is invalid.")
1516
+ );
1517
+ console.error(
1518
+ source_default.gray(
1519
+ `Run ${source_default.white("npx mcp-use login")} to re-authenticate.
1520
+ `
1521
+ )
1522
+ );
1523
+ process.exit(1);
1524
+ }
1525
+ console.error(
1526
+ source_default.red.bold(`
1527
+ \u2717 ${context}:`),
1528
+ source_default.red(error instanceof Error ? error.message : "Unknown error")
1529
+ );
1530
+ process.exit(1);
1531
+ }
1532
+
1511
1533
  // src/commands/auth.ts
1512
1534
  var DEVICE_CLIENT_ID = "mcp-use-cli";
1513
1535
  var DEVICE_POLL_TIMEOUT = 18e5;
@@ -1636,14 +1658,32 @@ async function loginCommand(options) {
1636
1658
  return;
1637
1659
  }
1638
1660
  if (await isLoggedIn()) {
1661
+ let needsReauth = false;
1662
+ try {
1663
+ await (await McpUseAPI.create()).testAuth();
1664
+ } catch (e) {
1665
+ if (e instanceof ApiUnauthorizedError) {
1666
+ needsReauth = true;
1667
+ }
1668
+ }
1669
+ if (!needsReauth) {
1670
+ if (!options?.silent) {
1671
+ console.log(
1672
+ source_default.yellow(
1673
+ "You are already logged in. Run 'npx mcp-use logout' first if you want to login with a different account."
1674
+ )
1675
+ );
1676
+ }
1677
+ return;
1678
+ }
1639
1679
  if (!options?.silent) {
1640
1680
  console.log(
1641
1681
  source_default.yellow(
1642
- "You are already logged in. Run 'npx mcp-use logout' first if you want to login with a different account."
1682
+ "\u26A0\uFE0F Stored credentials are invalid or expired. Re-authenticating..."
1643
1683
  )
1644
1684
  );
1645
1685
  }
1646
- return;
1686
+ await deleteConfig();
1647
1687
  }
1648
1688
  console.log(source_default.cyan.bold("Logging in to mcp-use cloud...\n"));
1649
1689
  const authBaseUrl = await getAuthBaseUrl();
@@ -1810,21 +1850,7 @@ async function whoamiCommand() {
1810
1850
  }
1811
1851
  }
1812
1852
  } catch (error) {
1813
- if (error?.status === 401) {
1814
- console.error(
1815
- source_default.red("\nYour session has expired or your API key is invalid.")
1816
- );
1817
- console.log(
1818
- source_default.gray(`Run ${source_default.white("mcp-use login")} to re-authenticate.
1819
- `)
1820
- );
1821
- } else {
1822
- console.error(
1823
- source_default.red.bold("\n\u2717 Failed to get user info:"),
1824
- source_default.red(error instanceof Error ? error.message : "Unknown error")
1825
- );
1826
- }
1827
- process.exit(1);
1853
+ handleCommandError(error, "Failed to get user info");
1828
1854
  }
1829
1855
  }
1830
1856
 
@@ -3575,12 +3601,16 @@ async function deployCommand(options) {
3575
3601
  }
3576
3602
  let api = await McpUseAPI.create();
3577
3603
  let resolvedOrgId;
3604
+ let resolvedOrgName;
3605
+ let resolvedOrgSlug;
3578
3606
  if (options.org) {
3579
3607
  const authInfo = await api.testAuth();
3580
3608
  const match = resolveOrgFromOption(authInfo.orgs ?? [], options.org);
3581
3609
  if (match) {
3582
3610
  api.setOrgId(match.id);
3583
3611
  resolvedOrgId = match.id;
3612
+ resolvedOrgName = match.name;
3613
+ resolvedOrgSlug = match.slug ?? void 0;
3584
3614
  const slug = match.slug ? source_default.gray(` (${match.slug})`) : "";
3585
3615
  console.log(
3586
3616
  source_default.gray("Organization: ") + source_default.cyan(match.name) + slug
@@ -3620,6 +3650,8 @@ async function deployCommand(options) {
3620
3650
  }
3621
3651
  api.setOrgId(selectedOrg.id);
3622
3652
  resolvedOrgId = selectedOrg.id;
3653
+ resolvedOrgName = selectedOrg.name;
3654
+ resolvedOrgSlug = selectedOrg.slug ?? void 0;
3623
3655
  await writeConfig({
3624
3656
  ...config,
3625
3657
  orgId: selectedOrg.id,
@@ -3631,6 +3663,8 @@ async function deployCommand(options) {
3631
3663
  );
3632
3664
  } else {
3633
3665
  resolvedOrgId = config.orgId;
3666
+ resolvedOrgName = config.orgName;
3667
+ resolvedOrgSlug = config.orgSlug;
3634
3668
  api.setOrgId(config.orgId);
3635
3669
  if (config.orgName) {
3636
3670
  const slug = config.orgSlug ? source_default.gray(` (${config.orgSlug})`) : "";
@@ -4027,6 +4061,22 @@ async function deployCommand(options) {
4027
4061
  }
4028
4062
  const existingLink = !options.new ? await getProjectLink(cwd) : null;
4029
4063
  let serverId = existingLink?.serverId;
4064
+ if (serverId && resolvedOrgId) {
4065
+ try {
4066
+ const linkedServer = await api.getServer(serverId);
4067
+ if (linkedServer.organizationId !== resolvedOrgId) {
4068
+ const target = resolvedOrgName ? `${resolvedOrgName}${resolvedOrgSlug ? ` (${resolvedOrgSlug})` : ""}` : resolvedOrgId;
4069
+ console.log(
4070
+ source_default.yellow(
4071
+ `\u26A0\uFE0F Linked server belongs to a different organization. Creating a new server in ${target}...
4072
+ `
4073
+ )
4074
+ );
4075
+ serverId = void 0;
4076
+ }
4077
+ } catch {
4078
+ }
4079
+ }
4030
4080
  if (existingLink && serverId) {
4031
4081
  try {
4032
4082
  const existingDep = await api.getDeployment(existingLink.deploymentId);
@@ -4269,11 +4319,7 @@ async function listDeploymentsCommand() {
4269
4319
  }
4270
4320
  console.log();
4271
4321
  } catch (error) {
4272
- console.error(
4273
- source_default.red.bold("\n\u2717 Failed to list deployments:"),
4274
- source_default.red(error instanceof Error ? error.message : "Unknown error")
4275
- );
4276
- process.exit(1);
4322
+ handleCommandError(error, "Failed to list deployments");
4277
4323
  }
4278
4324
  }
4279
4325
  async function getDeploymentCommand(deploymentId) {
@@ -4335,11 +4381,7 @@ async function getDeploymentCommand(deploymentId) {
4335
4381
  }
4336
4382
  console.log();
4337
4383
  } catch (error) {
4338
- console.error(
4339
- source_default.red.bold("\n\u2717 Failed to get deployment:"),
4340
- source_default.red(error instanceof Error ? error.message : "Unknown error")
4341
- );
4342
- process.exit(1);
4384
+ handleCommandError(error, "Failed to get deployment");
4343
4385
  }
4344
4386
  }
4345
4387
  async function restartDeploymentCommand(deploymentId, options) {
@@ -4410,11 +4452,7 @@ async function restartDeploymentCommand(deploymentId, options) {
4410
4452
  }
4411
4453
  console.log();
4412
4454
  } catch (error) {
4413
- console.error(
4414
- source_default.red.bold("\n\u2717 Failed to restart deployment:"),
4415
- source_default.red(error instanceof Error ? error.message : "Unknown error")
4416
- );
4417
- process.exit(1);
4455
+ handleCommandError(error, "Failed to restart deployment");
4418
4456
  }
4419
4457
  }
4420
4458
  async function deleteDeploymentCommand(deploymentId, options) {
@@ -4457,11 +4495,7 @@ async function deleteDeploymentCommand(deploymentId, options) {
4457
4495
  `)
4458
4496
  );
4459
4497
  } catch (error) {
4460
- console.error(
4461
- source_default.red.bold("\n\u2717 Failed to delete deployment:"),
4462
- source_default.red(error instanceof Error ? error.message : "Unknown error")
4463
- );
4464
- process.exit(1);
4498
+ handleCommandError(error, "Failed to delete deployment");
4465
4499
  }
4466
4500
  }
4467
4501
  async function logsCommand(deploymentId, options) {
@@ -4550,11 +4584,7 @@ async function logsCommand(deploymentId, options) {
4550
4584
  }
4551
4585
  console.log();
4552
4586
  } catch (error) {
4553
- console.error(
4554
- source_default.red.bold("\n\u2717 Failed to get logs:"),
4555
- source_default.red(error instanceof Error ? error.message : "Unknown error")
4556
- );
4557
- process.exit(1);
4587
+ handleCommandError(error, "Failed to get logs");
4558
4588
  }
4559
4589
  }
4560
4590
  async function stopDeploymentCommand(deploymentId) {
@@ -4574,11 +4604,7 @@ async function stopDeploymentCommand(deploymentId) {
4574
4604
  \u2713 Deployment stopped
4575
4605
  `));
4576
4606
  } catch (error) {
4577
- console.error(
4578
- source_default.red.bold("\n\u2717 Failed to stop deployment:"),
4579
- source_default.red(error instanceof Error ? error.message : "Unknown error")
4580
- );
4581
- process.exit(1);
4607
+ handleCommandError(error, "Failed to stop deployment");
4582
4608
  }
4583
4609
  }
4584
4610
  async function startDeploymentCommand(deploymentId) {
@@ -4598,11 +4624,7 @@ async function startDeploymentCommand(deploymentId) {
4598
4624
  )
4599
4625
  );
4600
4626
  } catch (error) {
4601
- console.error(
4602
- source_default.red.bold("\n\u2717 Failed to start deployment:"),
4603
- source_default.red(error instanceof Error ? error.message : "Unknown error")
4604
- );
4605
- process.exit(1);
4627
+ handleCommandError(error, "Failed to start deployment");
4606
4628
  }
4607
4629
  }
4608
4630
  function createDeploymentsCommand() {
@@ -4671,87 +4693,103 @@ async function requireLogin() {
4671
4693
  }
4672
4694
  }
4673
4695
  async function listEnvCommand(options) {
4674
- await requireLogin();
4675
- const api = await McpUseAPI.create();
4676
- const vars = await api.listEnvVariables(options.server);
4677
- if (vars.length === 0) {
4678
- console.log(
4679
- source_default.yellow("\nNo environment variables set for this server.\n")
4680
- );
4681
- return;
4682
- }
4683
- console.log(source_default.cyan.bold(`
4696
+ try {
4697
+ await requireLogin();
4698
+ const api = await McpUseAPI.create();
4699
+ const vars = await api.listEnvVariables(options.server);
4700
+ if (vars.length === 0) {
4701
+ console.log(
4702
+ source_default.yellow("\nNo environment variables set for this server.\n")
4703
+ );
4704
+ return;
4705
+ }
4706
+ console.log(source_default.cyan.bold(`
4684
4707
  Environment Variables (${vars.length})
4685
4708
  `));
4686
- for (const v of vars) {
4687
- printEnvVar(v, options.showValues);
4688
- console.log();
4709
+ for (const v of vars) {
4710
+ printEnvVar(v, options.showValues);
4711
+ console.log();
4712
+ }
4713
+ } catch (error) {
4714
+ handleCommandError(error, "Failed to list environment variables");
4689
4715
  }
4690
4716
  }
4691
4717
  async function addEnvCommand(assignment, options) {
4692
- await requireLogin();
4693
- const eqIdx = assignment.indexOf("=");
4694
- if (eqIdx === -1) {
4695
- console.error(
4696
- source_default.red(
4697
- "\u2717 Expected KEY=VALUE format, e.g. mcp-use servers env add API_KEY=abc123"
4698
- )
4699
- );
4700
- process.exit(1);
4701
- }
4702
- const key = assignment.substring(0, eqIdx).trim();
4703
- const value = assignment.substring(eqIdx + 1);
4704
- if (!key) {
4705
- console.error(source_default.red("\u2717 Key must not be empty."));
4706
- process.exit(1);
4707
- }
4708
- const environments = options.env ? parseEnvironments(options.env) : ALL_ENVS;
4709
- const api = await McpUseAPI.create();
4710
- const created = await api.createEnvVariable(options.server, {
4711
- key,
4712
- value,
4713
- environments,
4714
- sensitive: options.sensitive ?? false
4715
- });
4716
- console.log(
4717
- source_default.green(`
4718
+ try {
4719
+ await requireLogin();
4720
+ const eqIdx = assignment.indexOf("=");
4721
+ if (eqIdx === -1) {
4722
+ console.error(
4723
+ source_default.red(
4724
+ "\u2717 Expected KEY=VALUE format, e.g. mcp-use servers env add API_KEY=abc123"
4725
+ )
4726
+ );
4727
+ process.exit(1);
4728
+ }
4729
+ const key = assignment.substring(0, eqIdx).trim();
4730
+ const value = assignment.substring(eqIdx + 1);
4731
+ if (!key) {
4732
+ console.error(source_default.red("\u2717 Key must not be empty."));
4733
+ process.exit(1);
4734
+ }
4735
+ const environments = options.env ? parseEnvironments(options.env) : ALL_ENVS;
4736
+ const api = await McpUseAPI.create();
4737
+ const created = await api.createEnvVariable(options.server, {
4738
+ key,
4739
+ value,
4740
+ environments,
4741
+ sensitive: options.sensitive ?? false
4742
+ });
4743
+ console.log(
4744
+ source_default.green(`
4718
4745
  \u2713 Environment variable "${created.key}" added.
4719
4746
  `)
4720
- );
4721
- printEnvVar(created, true);
4722
- console.log();
4723
- }
4724
- async function updateEnvCommand(varId, options) {
4725
- await requireLogin();
4726
- if (!options.value && !options.env && options.sensitive === void 0) {
4727
- console.error(
4728
- source_default.red(
4729
- "\u2717 Nothing to update. Provide at least one of --value, --env, --sensitive."
4730
- )
4731
4747
  );
4732
- process.exit(1);
4748
+ printEnvVar(created, true);
4749
+ console.log();
4750
+ } catch (error) {
4751
+ handleCommandError(error, "Failed to add environment variable");
4733
4752
  }
4734
- const body = {};
4735
- if (options.value !== void 0) body.value = options.value;
4736
- if (options.env) body.environments = parseEnvironments(options.env);
4737
- if (options.sensitive !== void 0) body.sensitive = options.sensitive;
4738
- const api = await McpUseAPI.create();
4739
- const updated = await api.updateEnvVariable(options.server, varId, body);
4740
- console.log(
4741
- source_default.green(`
4753
+ }
4754
+ async function updateEnvCommand(varId, options) {
4755
+ try {
4756
+ await requireLogin();
4757
+ if (!options.value && !options.env && options.sensitive === void 0) {
4758
+ console.error(
4759
+ source_default.red(
4760
+ "\u2717 Nothing to update. Provide at least one of --value, --env, --sensitive."
4761
+ )
4762
+ );
4763
+ process.exit(1);
4764
+ }
4765
+ const body = {};
4766
+ if (options.value !== void 0) body.value = options.value;
4767
+ if (options.env) body.environments = parseEnvironments(options.env);
4768
+ if (options.sensitive !== void 0) body.sensitive = options.sensitive;
4769
+ const api = await McpUseAPI.create();
4770
+ const updated = await api.updateEnvVariable(options.server, varId, body);
4771
+ console.log(
4772
+ source_default.green(`
4742
4773
  \u2713 Environment variable "${updated.key}" updated.
4743
4774
  `)
4744
- );
4745
- printEnvVar(updated, !!options.value);
4746
- console.log();
4775
+ );
4776
+ printEnvVar(updated, !!options.value);
4777
+ console.log();
4778
+ } catch (error) {
4779
+ handleCommandError(error, "Failed to update environment variable");
4780
+ }
4747
4781
  }
4748
4782
  async function removeEnvCommand(varId, options) {
4749
- await requireLogin();
4750
- const api = await McpUseAPI.create();
4751
- await api.deleteEnvVariable(options.server, varId);
4752
- console.log(source_default.green(`
4783
+ try {
4784
+ await requireLogin();
4785
+ const api = await McpUseAPI.create();
4786
+ await api.deleteEnvVariable(options.server, varId);
4787
+ console.log(source_default.green(`
4753
4788
  \u2713 Environment variable ${varId} removed.
4754
4789
  `));
4790
+ } catch (error) {
4791
+ handleCommandError(error, "Failed to remove environment variable");
4792
+ }
4755
4793
  }
4756
4794
  function createEnvCommand() {
4757
4795
  const envCommand = new Command3("env").description(
@@ -4881,11 +4919,7 @@ async function listServersCommand(options) {
4881
4919
  }
4882
4920
  console.log();
4883
4921
  } catch (error) {
4884
- console.error(
4885
- source_default.red.bold("\n\u2717 Failed to list servers:"),
4886
- source_default.red(error instanceof Error ? error.message : "Unknown error")
4887
- );
4888
- process.exit(1);
4922
+ handleCommandError(error, "Failed to list servers");
4889
4923
  }
4890
4924
  }
4891
4925
  async function getServerCommand(idOrSlug, options) {
@@ -4985,11 +5019,7 @@ async function getServerCommand(idOrSlug, options) {
4985
5019
  }
4986
5020
  console.log();
4987
5021
  } catch (error) {
4988
- console.error(
4989
- source_default.red.bold("\n\u2717 Failed to get server:"),
4990
- source_default.red(error instanceof Error ? error.message : "Unknown error")
4991
- );
4992
- process.exit(1);
5022
+ handleCommandError(error, "Failed to get server");
4993
5023
  }
4994
5024
  }
4995
5025
  async function deleteServerCommand(serverId, options) {
@@ -5042,11 +5072,7 @@ async function deleteServerCommand(serverId, options) {
5042
5072
  )
5043
5073
  );
5044
5074
  } catch (error) {
5045
- console.error(
5046
- source_default.red.bold("\n\u2717 Failed to delete server:"),
5047
- source_default.red(error instanceof Error ? error.message : "Unknown error")
5048
- );
5049
- process.exit(1);
5075
+ handleCommandError(error, "Failed to delete server");
5050
5076
  }
5051
5077
  }
5052
5078
  function createServersCommand() {
@@ -5098,11 +5124,7 @@ async function orgListCommand() {
5098
5124
  );
5099
5125
  }
5100
5126
  } catch (error) {
5101
- console.error(
5102
- source_default.red.bold("\n\u2717 Failed to list organizations:"),
5103
- source_default.red(error instanceof Error ? error.message : "Unknown error")
5104
- );
5105
- process.exit(1);
5127
+ handleCommandError(error, "Failed to list organizations");
5106
5128
  }
5107
5129
  }
5108
5130
  async function orgSwitchCommand() {
@@ -5147,11 +5169,7 @@ async function orgSwitchCommand() {
5147
5169
  source_default.green.bold("\n\u2713 Switched to ") + source_default.cyan.bold(selected.name) + slug
5148
5170
  );
5149
5171
  } catch (error) {
5150
- console.error(
5151
- source_default.red.bold("\n\u2717 Failed to switch organization:"),
5152
- source_default.red(error instanceof Error ? error.message : "Unknown error")
5153
- );
5154
- process.exit(1);
5172
+ handleCommandError(error, "Failed to switch organization");
5155
5173
  }
5156
5174
  }
5157
5175
  async function orgCurrentCommand() {
@@ -5171,11 +5189,7 @@ async function orgCurrentCommand() {
5171
5189
  source_default.cyan.bold("\u{1F3E2} Active organization: ") + source_default.white(config.orgName || config.orgId) + slug
5172
5190
  );
5173
5191
  } catch (error) {
5174
- console.error(
5175
- source_default.red.bold("\n\u2717 Failed to get organization:"),
5176
- source_default.red(error instanceof Error ? error.message : "Unknown error")
5177
- );
5178
- process.exit(1);
5192
+ handleCommandError(error, "Failed to get organization");
5179
5193
  }
5180
5194
  }
5181
5195