@tacitai/mcp-server 0.6.43 → 0.6.45

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/cli.js CHANGED
@@ -4,7 +4,7 @@ import * as path from 'path';
4
4
  import * as os from 'os';
5
5
  import * as readline from 'readline';
6
6
  import { spawn } from 'child_process';
7
- import { deviceAuthFlow, deriveAppUrl, workspaceNameToProfile } from './setup.js';
7
+ import { deviceAuthFlow, loginFlow, deriveAppUrl, workspaceNameToProfile } from './setup.js';
8
8
  const PROFILES_DIR = path.join(os.homedir(), '.tacit', 'profiles');
9
9
  const DEFAULT_FILE = path.join(os.homedir(), '.tacit', 'default');
10
10
  const PROFILE_REGEX = /^[a-z0-9][a-z0-9-]{0,63}$/;
@@ -163,22 +163,27 @@ async function cmdLogin(args) {
163
163
  try {
164
164
  result = await deviceAuthFlow(apiUrl);
165
165
  }
166
- catch (err) {
167
- const msg = err instanceof Error ? err.message : String(err);
166
+ catch (deviceErr) {
167
+ const msg = deviceErr instanceof Error ? deviceErr.message : String(deviceErr);
168
168
  if (msg.includes('timed out')) {
169
169
  process.stderr.write('Authentication timed out (120s). Try again or check browser.\n');
170
+ process.exit(1);
171
+ return;
170
172
  }
171
173
  else if (msg.includes('state mismatch')) {
172
174
  process.stderr.write('Authentication failed: state mismatch. Try again.\n');
175
+ process.exit(1);
176
+ return;
173
177
  }
174
178
  else if (msg.includes('EADDRINUSE')) {
175
179
  process.stderr.write('Port 9123 busy (another login running?). Try again.\n');
180
+ process.exit(1);
181
+ return;
176
182
  }
177
- else {
178
- process.stderr.write(`Authentication failed: ${msg}\n`);
179
- }
180
- process.exit(1);
181
- return;
183
+ // Device-auth not available (e.g. 404) — fall back to PIN login.
184
+ process.stderr.write(' Browser sign-in not available. Falling back to email + PIN.\n');
185
+ const loginResult = await loginFlow(apiUrl);
186
+ result = { token: loginResult.token, expiresAt: loginResult.expiresAt, workspaceName: loginResult.workspaceName };
182
187
  }
183
188
  let workspaceId;
184
189
  let displayName = result.workspaceName;
package/dist/setup.d.ts CHANGED
@@ -48,6 +48,7 @@ export declare function deriveAppUrl(apiUrl: string): string;
48
48
  /**
49
49
  * Write ~/.tacit/profiles/<profile>.json with { expires_at, api_url }.
50
50
  * Token plaintext is NEVER written here — metadata only.
51
+ * @deprecated Prefer writeV2Profile for full profile data.
51
52
  */
52
53
  export declare function writeProfileMetadata(profile: string, expiresAt: string, apiUrl: string): void;
53
54
  interface DeviceAuthResult {
@@ -71,5 +72,11 @@ interface DeviceAuthResult {
71
72
  * - Browser open fails → print URL to stdout for manual copy-paste (AC-16 fallback).
72
73
  */
73
74
  export declare function deviceAuthFlow(apiUrl: string): Promise<DeviceAuthResult>;
75
+ interface LoginResult {
76
+ token: string;
77
+ workspaceName: string;
78
+ expiresAt: string;
79
+ }
80
+ export declare function loginFlow(apiUrl: string): Promise<LoginResult>;
74
81
  export declare function setup(argv: string[]): Promise<void>;
75
82
  export {};
package/dist/setup.js CHANGED
@@ -189,10 +189,27 @@ function mergeConfig(existing, profileName, entry) {
189
189
  function configToBytes(cfg) {
190
190
  return JSON.stringify(cfg, null, 2) + '\n';
191
191
  }
192
- // ─── Metadata file ────────────────────────────────────────────────────────────
192
+ function writeV2Profile(name, data) {
193
+ if (!/^[a-z0-9][a-z0-9-]{0,63}$/.test(name)) {
194
+ process.stderr.write(`[TACIT] Invalid profile name '${name}' — skipping profile write.\n`);
195
+ return;
196
+ }
197
+ const metaDir = path.join(os.homedir(), '.tacit', 'profiles');
198
+ fs.mkdirSync(metaDir, { recursive: true, mode: 0o700 });
199
+ try {
200
+ fs.chmodSync(metaDir, 0o700);
201
+ }
202
+ catch { /* unsupported OS */ }
203
+ const filePath = path.join(metaDir, `${name}.json`);
204
+ const tmp = filePath + '.tmp';
205
+ fs.writeFileSync(tmp, JSON.stringify(data, null, 2) + '\n', { mode: 0o600 });
206
+ fs.renameSync(tmp, filePath);
207
+ }
208
+ // ─── Metadata file (legacy v1) ───────────────────────────────────────────────
193
209
  /**
194
210
  * Write ~/.tacit/profiles/<profile>.json with { expires_at, api_url }.
195
211
  * Token plaintext is NEVER written here — metadata only.
212
+ * @deprecated Prefer writeV2Profile for full profile data.
196
213
  */
197
214
  export function writeProfileMetadata(profile, expiresAt, apiUrl) {
198
215
  if (!/^[a-z0-9][a-z0-9-]{0,63}$/.test(profile)) {
@@ -401,7 +418,7 @@ function prompt(question, hidden = false) {
401
418
  }
402
419
  });
403
420
  }
404
- async function loginFlow(apiUrl) {
421
+ export async function loginFlow(apiUrl) {
405
422
  process.stderr.write('\n Log in with your TACIT credentials.\n\n');
406
423
  const email = await prompt(' Email: ');
407
424
  const pin = await prompt(' PIN: ', true);
@@ -423,18 +440,23 @@ async function loginFlow(apiUrl) {
423
440
  process.stderr.write('\n Login failed — check your email and PIN.\n');
424
441
  }
425
442
  else {
426
- process.stderr.write(`\n Login failed (${res.status}): ${body}\n`);
443
+ const isHtml = body.trimStart().startsWith('<!') || body.trimStart().startsWith('<html');
444
+ const detail = isHtml
445
+ ? `The server returned an HTML page instead of a JSON response.\n This usually means --api-url points to the web app, not the API.\n Try without --api-url (the default is correct for most users),\n or use the API URL (e.g. https://api-beta.tacitai.app).`
446
+ : body.slice(0, 200);
447
+ process.stderr.write(`\n Login failed (${res.status}): ${detail}\n`);
427
448
  }
428
449
  process.exit(1);
429
450
  }
430
451
  const data = await res.json();
431
452
  const token = typeof data['session_token'] === 'string' ? data['session_token'] : '';
432
453
  const workspaceName = typeof data['workspace_name'] === 'string' ? data['workspace_name'] : '';
454
+ const expiresAt = typeof data['expires_at'] === 'string' ? data['expires_at'] : '';
433
455
  if (!token) {
434
456
  process.stderr.write('Error: login returned no session token.\n');
435
457
  process.exit(1);
436
458
  }
437
- return { token, workspaceName };
459
+ return { token, workspaceName, expiresAt };
438
460
  }
439
461
  // ─── Smoke synthesis ──────────────────────────────────────────────────────────
440
462
  const SMOKE_QUERY = 'What is in this knowledge base?';
@@ -471,7 +493,17 @@ async function runSmoke(apiUrl, token) {
471
493
  export async function setup(argv) {
472
494
  const opts = parseArgs(argv);
473
495
  // Resolve api-url: flag > env > default.
474
- const apiUrl = opts.apiUrl ?? process.env['TACIT_API_URL'] ?? 'https://api-beta.tacitai.app';
496
+ let apiUrl = opts.apiUrl ?? process.env['TACIT_API_URL'] ?? 'https://api-beta.tacitai.app';
497
+ // Auto-correct common mistake: frontend URL (beta.tacitai.app) → API URL (api-beta.tacitai.app).
498
+ try {
499
+ const parsed = new URL(apiUrl);
500
+ if (/^(?!api[-.]).*\.tacitai\.app$/.test(parsed.hostname)) {
501
+ const corrected = `${parsed.protocol}//api-${parsed.hostname}`;
502
+ process.stderr.write(`\n Note: ${apiUrl} looks like the web app URL.\n Using API URL instead: ${corrected}\n\n`);
503
+ apiUrl = corrected;
504
+ }
505
+ }
506
+ catch { /* invalid URL — let it fail naturally downstream */ }
475
507
  // Determine profile and token.
476
508
  let profile = opts.profile;
477
509
  let token = opts.token;
@@ -498,11 +530,39 @@ export async function setup(argv) {
498
530
  if (!authenticated) {
499
531
  const loginResult = await loginFlow(apiUrl);
500
532
  token = loginResult.token;
533
+ if (loginResult.expiresAt)
534
+ expiresAt = loginResult.expiresAt;
501
535
  if (!opts.profileExplicit && loginResult.workspaceName) {
502
536
  profile = workspaceNameToProfile(loginResult.workspaceName);
503
537
  }
504
538
  }
505
539
  }
540
+ // Resolve workspace_id so we can write a full v2 profile.
541
+ let workspaceId = '';
542
+ let workspaceDisplayName = '';
543
+ if (token) {
544
+ try {
545
+ const wsResp = await fetch(apiUrl.endsWith('/api/v1')
546
+ ? `${apiUrl}/workspaces/mine`
547
+ : `${apiUrl}/api/v1/workspaces/mine`, { headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json' } });
548
+ if (wsResp.ok) {
549
+ const wsData = await wsResp.json();
550
+ workspaceId = typeof wsData['active_workspace_id'] === 'string' ? wsData['active_workspace_id'] : '';
551
+ if (Array.isArray(wsData['workspaces'])) {
552
+ const match = wsData['workspaces'].find(w => w.workspace_id === workspaceId || w.id === workspaceId);
553
+ if (match) {
554
+ const n = match.display_name ?? match.name;
555
+ if (typeof n === 'string')
556
+ workspaceDisplayName = n;
557
+ }
558
+ }
559
+ if (!opts.profileExplicit && workspaceDisplayName) {
560
+ profile = workspaceNameToProfile(workspaceDisplayName);
561
+ }
562
+ }
563
+ }
564
+ catch { /* workspace resolution failed — profile still works via .mcp.json fallback */ }
565
+ }
506
566
  // Final env-var name derived from (possibly updated) profile.
507
567
  const envVarName = profileToEnvVar(profile);
508
568
  // Pass the actual token when we have it (device-auth or --token);
@@ -546,8 +606,27 @@ export async function setup(argv) {
546
606
  const dest = mcpJsonPath();
547
607
  fs.writeFileSync(dest, output, { encoding: 'utf-8', mode: 0o600 });
548
608
  fs.chmodSync(dest, 0o600);
549
- // AC-12: write ~/.tacit/profiles/<profile>.json metadata (token NOT included).
550
- if (expiresAt) {
609
+ // Write a full v2 profile so `tacit launch <name>` works after setup.
610
+ if (resolvedToken && workspaceId) {
611
+ writeV2Profile(profile, {
612
+ version: 2,
613
+ workspace_id: workspaceId,
614
+ workspace_name: workspaceDisplayName || profile,
615
+ api_url: apiUrl,
616
+ app_url: deriveAppUrl(apiUrl),
617
+ token: resolvedToken,
618
+ created_at: new Date().toISOString(),
619
+ expires_at: expiresAt || undefined,
620
+ });
621
+ // Set as default profile if none exists.
622
+ const defaultFile = path.join(os.homedir(), '.tacit', 'default');
623
+ if (!fs.existsSync(defaultFile)) {
624
+ const dir = path.dirname(defaultFile);
625
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
626
+ fs.writeFileSync(defaultFile, profile + '\n', { mode: 0o600 });
627
+ }
628
+ }
629
+ else if (expiresAt) {
551
630
  writeProfileMetadata(profile, expiresAt, apiUrl);
552
631
  }
553
632
  if (registered) {
@@ -564,6 +643,12 @@ export async function setup(argv) {
564
643
  ` export ${envVarName}=<your-token>\n` +
565
644
  `\n Then start a new Claude Code session to connect.\n`);
566
645
  }
646
+ // Show profile launch tip when a v2 profile was written.
647
+ if (resolvedToken && workspaceId) {
648
+ process.stdout.write(`\n Profile saved: ${profile}\n` +
649
+ ` Launch Claude Code with this workspace:\n` +
650
+ ` npx -p @tacitai/mcp-server tacit launch ${profile}\n`);
651
+ }
567
652
  if (opts.smoke) {
568
653
  await runSmoke(apiUrl, token);
569
654
  // runSmoke calls process.exit internally; return here for type safety.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tacitai/mcp-server",
3
- "version": "0.6.43",
3
+ "version": "0.6.45",
4
4
  "description": "stdio MCP server wrapping Tacit /api/v1 REST/SSE API",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {