@sentientui/mcp 0.17.4 → 0.17.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
@@ -2,19 +2,21 @@
2
2
  import {
3
3
  ApiClient,
4
4
  createMcpServer
5
- } from "./chunk-CEZJQSKB.js";
5
+ } from "./chunk-H763K2O3.js";
6
6
 
7
7
  // src/index.ts
8
8
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
9
9
 
10
10
  // src/demo.ts
11
- import { readFileSync, writeFileSync, mkdirSync } from "fs";
11
+ import { readFileSync, writeFileSync, mkdirSync, rmSync } from "fs";
12
12
  import { join } from "path";
13
13
  import { homedir } from "os";
14
14
  var CONFIG_DIR = join(homedir(), ".config", "sentientui");
15
15
  var CONFIG_FILE = join(CONFIG_DIR, "mcp-anon.json");
16
16
  var _a;
17
17
  var API_BASE = (_a = process.env.SENTIENTUI_API_URL) != null ? _a : "https://api.sentient-ui.com";
18
+ var tokenCameFromCache = false;
19
+ var refreshAttempted = false;
18
20
  function readCachedToken() {
19
21
  try {
20
22
  return JSON.parse(readFileSync(CONFIG_FILE, "utf-8"));
@@ -26,17 +28,8 @@ function writeCachedToken(cfg) {
26
28
  mkdirSync(CONFIG_DIR, { recursive: true, mode: 448 });
27
29
  writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2), { encoding: "utf-8", mode: 384 });
28
30
  }
29
- async function resolveDemoToken() {
31
+ async function provisionToken() {
30
32
  var _a2;
31
- const cached = readCachedToken();
32
- if (cached == null ? void 0 : cached.token) {
33
- process.stderr.write(
34
- `[sentientui-mcp] Running in demo mode (${CONFIG_FILE}). Set SENTIENTUI_API_KEY for full access.
35
- `
36
- );
37
- return cached.token;
38
- }
39
- process.stderr.write("[sentientui-mcp] No API key found. Provisioning anonymous demo token...\n");
40
33
  const res = await fetch(`${API_BASE}/v1/mcp/demo`, { method: "POST" });
41
34
  if (!res.ok) {
42
35
  const body = await res.json().catch(() => ({}));
@@ -50,14 +43,51 @@ async function resolveDemoToken() {
50
43
  );
51
44
  return data.token;
52
45
  }
46
+ async function resolveDemoToken() {
47
+ const cached = readCachedToken();
48
+ if (cached == null ? void 0 : cached.token) {
49
+ tokenCameFromCache = true;
50
+ process.stderr.write(
51
+ `[sentientui-mcp] Running in demo mode (${CONFIG_FILE}). Set SENTIENTUI_API_KEY for full access.
52
+ `
53
+ );
54
+ return cached.token;
55
+ }
56
+ process.stderr.write("[sentientui-mcp] No API key found. Provisioning anonymous demo token...\n");
57
+ return provisionToken();
58
+ }
59
+ async function refreshDemoToken() {
60
+ if (!tokenCameFromCache || refreshAttempted) return null;
61
+ refreshAttempted = true;
62
+ try {
63
+ rmSync(CONFIG_FILE, { force: true });
64
+ } catch (e) {
65
+ }
66
+ process.stderr.write(
67
+ "[sentientui-mcp] Cached demo token was rejected by the server; provisioning a fresh one...\n"
68
+ );
69
+ try {
70
+ return await provisionToken();
71
+ } catch (e) {
72
+ return null;
73
+ }
74
+ }
53
75
 
54
76
  // src/index.ts
55
77
  async function main() {
56
- let apiKey = process.env.SENTIENTUI_API_KEY;
78
+ const rawKey = process.env.SENTIENTUI_API_KEY;
79
+ let apiKey = rawKey == null ? void 0 : rawKey.trim();
80
+ let refreshApiKey;
81
+ if (rawKey !== void 0 && !apiKey) {
82
+ throw new Error(
83
+ "SENTIENTUI_API_KEY is set but empty. Set it to a real server key (sk_\u2026), or unset it entirely to use anonymous demo mode."
84
+ );
85
+ }
57
86
  if (!apiKey) {
58
87
  apiKey = await resolveDemoToken();
88
+ refreshApiKey = refreshDemoToken;
59
89
  }
60
- const client = new ApiClient({ apiKey, baseUrl: process.env.SENTIENTUI_API_URL });
90
+ const client = new ApiClient({ apiKey, baseUrl: process.env.SENTIENTUI_API_URL, refreshApiKey });
61
91
  const server = createMcpServer(client);
62
92
  const transport = new StdioServerTransport();
63
93
  await server.connect(transport);