@costlens/mcp-server 0.4.2 → 0.4.3

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.
Files changed (2) hide show
  1. package/dist/cli.js +50 -41
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -1,8 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var __create = Object.create;
3
4
  var __defProp = Object.defineProperty;
4
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
6
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
9
  var __esm = (fn, res) => function __init() {
8
10
  return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
@@ -378,11 +388,11 @@ var init_index = __esm({
378
388
  });
379
389
 
380
390
  // src/cli.ts
381
- var import_http = require("http");
382
391
  var import_child_process = require("child_process");
383
392
  var import_fs2 = require("fs");
384
393
  var import_os2 = require("os");
385
394
  var import_path2 = require("path");
395
+ var import_crypto = __toESM(require("crypto"));
386
396
  var CONFIG_DIR = (0, import_path2.join)((0, import_os2.homedir)(), ".costlens");
387
397
  var CONFIG_FILE = (0, import_path2.join)(CONFIG_DIR, "config.json");
388
398
  var API_BASE2 = process.env.COSTLENS_API_URL || "https://api.costlens.dev";
@@ -400,50 +410,49 @@ function writeConfig(config) {
400
410
  }
401
411
  async function login() {
402
412
  console.log("\u{1F511} CostLens \u2014 Authenticating...\n");
403
- const port = 9876 + Math.floor(Math.random() * 100);
404
- const server2 = (0, import_http.createServer)((req, res) => {
405
- const url = new URL(req.url, `http://localhost:${port}`);
406
- const key = url.searchParams.get("key");
407
- if (key) {
408
- writeConfig({ apiKey: key });
409
- res.writeHead(200, { "Content-Type": "text/html" });
410
- res.end('<html><body style="font-family:sans-serif;text-align:center;padding:60px"><h2>Authenticated</h2><p>You can close this tab and return to your terminal.</p></body></html>');
411
- console.log("\u2713 Authenticated successfully");
412
- console.log(` Key saved to ${CONFIG_FILE}
413
+ const sessionId = import_crypto.default.randomBytes(16).toString("hex");
414
+ const authUrl = `${APP_URL}/cli-auth?session=${sessionId}`;
415
+ console.log(` Opening browser: ${authUrl}
413
416
  `);
414
- setTimeout(() => {
415
- server2.close();
416
- if (process.argv[2] === "setup") {
417
- init();
418
- } else {
419
- process.exit(0);
420
- }
421
- }, 500);
422
- } else {
423
- res.writeHead(400);
424
- res.end("Missing key");
425
- }
426
- });
427
- server2.listen(port, () => {
428
- const authUrl = `${APP_URL}/cli-auth?port=${port}`;
429
- console.log(` Opening browser: ${authUrl}
430
- `);
431
- const platform = process.platform;
417
+ const platform = process.platform;
418
+ try {
419
+ if (platform === "darwin") (0, import_child_process.execSync)(`open "${authUrl}"`);
420
+ else if (platform === "linux") (0, import_child_process.execSync)(`xdg-open "${authUrl}"`);
421
+ else if (platform === "win32") (0, import_child_process.execSync)(`start "${authUrl}"`);
422
+ else console.log(` Open this URL manually: ${authUrl}`);
423
+ } catch {
424
+ console.log(` Open this URL manually: ${authUrl}`);
425
+ }
426
+ console.log(" Waiting for authentication...");
427
+ const startTime = Date.now();
428
+ const TIMEOUT = 12e4;
429
+ const INTERVAL = 2e3;
430
+ while (Date.now() - startTime < TIMEOUT) {
431
+ await new Promise((r) => setTimeout(r, INTERVAL));
432
432
  try {
433
- if (platform === "darwin") (0, import_child_process.execSync)(`open "${authUrl}"`);
434
- else if (platform === "linux") (0, import_child_process.execSync)(`xdg-open "${authUrl}"`);
435
- else if (platform === "win32") (0, import_child_process.execSync)(`start "${authUrl}"`);
436
- else console.log(` Open this URL manually: ${authUrl}`);
433
+ const res = await fetch(`${APP_URL}/api/cli-auth/poll?session=${sessionId}`, {
434
+ signal: AbortSignal.timeout(5e3)
435
+ });
436
+ if (res.ok) {
437
+ const data = await res.json();
438
+ if (data.key) {
439
+ writeConfig({ apiKey: data.key });
440
+ console.log("\n\u2713 Authenticated successfully");
441
+ console.log(` Key saved to ${CONFIG_FILE}
442
+ `);
443
+ if (process.argv[2] === "setup") {
444
+ init();
445
+ } else {
446
+ process.exit(0);
447
+ }
448
+ return;
449
+ }
450
+ }
437
451
  } catch {
438
- console.log(` Open this URL manually: ${authUrl}`);
439
452
  }
440
- console.log(" Waiting for authentication...");
441
- });
442
- setTimeout(() => {
443
- console.log("\n\u2717 Timed out. Try again.");
444
- server2.close();
445
- process.exit(1);
446
- }, 12e4);
453
+ }
454
+ console.log("\n\u2717 Timed out. Try again.");
455
+ process.exit(1);
447
456
  }
448
457
  function init() {
449
458
  const config = readConfig();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@costlens/mcp-server",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "MCP server for AI cost optimization with prompt complexity classification",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",