@tacitai/mcp-server 0.6.47 → 0.6.49

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
@@ -3,7 +3,7 @@ import * as fs from 'fs';
3
3
  import * as path from 'path';
4
4
  import * as os from 'os';
5
5
  import * as readline from 'readline';
6
- import { spawn } from 'child_process';
6
+ import { spawn, execFile } from 'child_process';
7
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');
@@ -115,6 +115,31 @@ function tryLoadProfile(name) {
115
115
  }
116
116
  }
117
117
  // ─── Subcommands ─────────────────────────────────────────────────────────────
118
+ const PROFILE_PASSTHROUGH = '${TACIT_PROFILE}';
119
+ // Idempotent: older setups lack TACIT_PROFILE in the MCP env, so the spawned MCP server never sees the profile.
120
+ function migrateMcpProfilePassthrough() {
121
+ try {
122
+ const mcpPath = path.join(os.homedir(), '.claude', 'mcp.json');
123
+ if (!fs.existsSync(mcpPath))
124
+ return;
125
+ const config = JSON.parse(fs.readFileSync(mcpPath, 'utf-8'));
126
+ const entry = config.mcpServers?.['tacit'];
127
+ if (!entry || typeof entry !== 'object')
128
+ return;
129
+ const env = entry.env ?? {};
130
+ if ('TACIT_PROFILE' in env)
131
+ return;
132
+ entry.env = { ...env, TACIT_PROFILE: PROFILE_PASSTHROUGH };
133
+ const tmp = mcpPath + '.tmp';
134
+ fs.writeFileSync(tmp, JSON.stringify(config, null, 2) + '\n', { mode: 0o600 });
135
+ fs.renameSync(tmp, mcpPath);
136
+ // Skip add-json when a literal token is embedded: argv is visible to other local users via ps.
137
+ if (typeof env['TACIT_TOKEN'] === 'string' && env['TACIT_TOKEN'].startsWith('${')) {
138
+ execFile('claude', ['mcp', 'add-json', '--scope', 'user', 'tacit', JSON.stringify(entry)], () => { });
139
+ }
140
+ }
141
+ catch { /* best-effort migration */ }
142
+ }
118
143
  async function cmdLaunch(name, extraArgs) {
119
144
  const profile = loadProfile(name);
120
145
  if (profile.expires_at) {
@@ -142,6 +167,7 @@ async function cmdLaunch(name, extraArgs) {
142
167
  if (model) {
143
168
  claudeArgs.push('--model', model);
144
169
  }
170
+ migrateMcpProfilePassthrough();
145
171
  const child = spawn('claude', claudeArgs, {
146
172
  stdio: 'inherit',
147
173
  env: { ...process.env, TACIT_PROFILE: name },
package/dist/client.d.ts CHANGED
@@ -7,6 +7,8 @@
7
7
  * - No anthropic/openai SDK import.
8
8
  * - Token sourced from process.env.TACIT_TOKEN or v2 profile files (~/.tacit/profiles/).
9
9
  */
10
+ /** Extract workspace_id from a JWT's payload without verifying the signature. */
11
+ export declare function decodeJwtWorkspaceId(token: string): string | null;
10
12
  export interface TacitClientOptions {
11
13
  apiUrl?: string;
12
14
  appUrl?: string;
@@ -24,6 +26,7 @@ export declare class TacitClient {
24
26
  private _workspaceId;
25
27
  private _workspaceName;
26
28
  private _initialized;
29
+ private readonly _profileWorkspaceId;
27
30
  /**
28
31
  * @param fetchOrOpts - Optional: a custom fetch implementation (for testing),
29
32
  * or a TacitClientOptions object. If a function is provided it overrides
package/dist/client.js CHANGED
@@ -15,6 +15,23 @@ function isHmacSessionToken(token) {
15
15
  const parts = token.split('.');
16
16
  return parts.length === 2 && /^[0-9a-f]{64}$/.test(parts[1]);
17
17
  }
18
+ /** Extract workspace_id from a JWT's payload without verifying the signature. */
19
+ export function decodeJwtWorkspaceId(token) {
20
+ if (!token || isHmacSessionToken(token))
21
+ return null;
22
+ const parts = token.split('.');
23
+ if (parts.length !== 3)
24
+ return null;
25
+ try {
26
+ let payload = parts[1];
27
+ payload += '='.repeat((4 - (payload.length % 4)) % 4);
28
+ const decoded = JSON.parse(Buffer.from(payload, 'base64url').toString('utf-8'));
29
+ return typeof decoded['workspace_id'] === 'string' ? decoded['workspace_id'] : null;
30
+ }
31
+ catch {
32
+ return null;
33
+ }
34
+ }
18
35
  export class TacitClient {
19
36
  apiUrl;
20
37
  _appUrl;
@@ -24,6 +41,7 @@ export class TacitClient {
24
41
  _workspaceId;
25
42
  _workspaceName = null;
26
43
  _initialized = false;
44
+ _profileWorkspaceId;
27
45
  /**
28
46
  * @param fetchOrOpts - Optional: a custom fetch implementation (for testing),
29
47
  * or a TacitClientOptions object. If a function is provided it overrides
@@ -64,6 +82,7 @@ export class TacitClient {
64
82
  }
65
83
  }
66
84
  this._workspaceId = resolvedWorkspaceId;
85
+ this._profileWorkspaceId = resolvedOpts.workspaceId ?? '';
67
86
  }
68
87
  get workspaceId() {
69
88
  return this._workspaceId;
@@ -75,10 +94,27 @@ export class TacitClient {
75
94
  if (this._initialized)
76
95
  return;
77
96
  try {
97
+ // BUG-375: when profile specified a workspace_id, check if the token targets
98
+ // a different workspace (setup wrote the same JWT to all profiles). If so,
99
+ // call switchWorkspace to get a correctly-scoped token before anything else.
100
+ if (this._profileWorkspaceId) {
101
+ const jwtWs = decodeJwtWorkspaceId(this.token);
102
+ if (jwtWs && jwtWs !== this._profileWorkspaceId) {
103
+ try {
104
+ await this.switchWorkspace(this._profileWorkspaceId);
105
+ }
106
+ catch {
107
+ process.stderr.write(`[TACIT] switchWorkspace failed for profile workspace — using token as-is\n`);
108
+ }
109
+ }
110
+ }
78
111
  const data = await this.get('/api/v1/workspaces/mine');
79
112
  if (data && typeof data === 'object') {
80
- if ('active_workspace_id' in data && typeof data.active_workspace_id === 'string') {
81
- this._workspaceId = data.active_workspace_id;
113
+ // BUG-375: don't clobber workspace_id when the profile explicitly provided one.
114
+ if (!this._profileWorkspaceId) {
115
+ if ('active_workspace_id' in data && typeof data.active_workspace_id === 'string') {
116
+ this._workspaceId = data.active_workspace_id;
117
+ }
82
118
  }
83
119
  if (Array.isArray(data.workspaces) && data.workspaces.length > 0) {
84
120
  const active = data.workspaces.find((w) => w.workspace_id === this._workspaceId || w.id === this._workspaceId);
@@ -477,7 +513,7 @@ export function loadProfile(name) {
477
513
  export function resolveProfileOptions() {
478
514
  // Level 1: TACIT_PROFILE env → load profile (profile values take precedence over stray env vars)
479
515
  const profileName = process.env['TACIT_PROFILE'];
480
- if (isNonEmpty(profileName)) {
516
+ if (isNonEmpty(profileName) && !profileName.includes('${')) {
481
517
  const profile = loadProfile(profileName);
482
518
  return {
483
519
  apiUrl: profile.api_url,
package/dist/setup.js CHANGED
@@ -169,6 +169,7 @@ function buildProfileEntry(apiUrl, envVarName, token) {
169
169
  TACIT_TOKEN: token ?? `\${${envVarName}}`,
170
170
  TACIT_API_URL: apiUrl,
171
171
  TACIT_APP_URL: deriveAppUrl(apiUrl),
172
+ TACIT_PROFILE: '${TACIT_PROFILE}',
172
173
  },
173
174
  };
174
175
  }
@@ -626,16 +627,41 @@ export async function setup(argv) {
626
627
  fs.writeFileSync(dest, output, { encoding: 'utf-8', mode: 0o600 });
627
628
  fs.chmodSync(dest, 0o600);
628
629
  // Write v2 profiles for ALL workspaces the user has access to.
630
+ // Each workspace gets its OWN token via switch-workspace so the JWT's
631
+ // workspace_id claim matches the profile — the backend trusts the JWT, not metadata.
629
632
  const writtenProfiles = [];
630
633
  if (resolvedToken && allWorkspaces.length > 0) {
631
634
  for (const ws of allWorkspaces) {
635
+ let wsToken = resolvedToken;
636
+ if (ws.id !== activeWorkspaceId) {
637
+ try {
638
+ const switchResp = await fetch(apiUrl.endsWith('/api/v1')
639
+ ? `${apiUrl}/auth/switch-workspace`
640
+ : `${apiUrl}/api/v1/auth/switch-workspace`, {
641
+ method: 'POST',
642
+ headers: {
643
+ 'Authorization': `Bearer ${resolvedToken}`,
644
+ 'Content-Type': 'application/json',
645
+ 'Accept': 'application/json',
646
+ },
647
+ body: JSON.stringify({ workspace_id: ws.id }),
648
+ });
649
+ if (switchResp.ok) {
650
+ const switchData = await switchResp.json();
651
+ if (typeof switchData['token'] === 'string' && switchData['token']) {
652
+ wsToken = switchData['token'];
653
+ }
654
+ }
655
+ }
656
+ catch { /* switch failed — fall back to original token; runtime Fix A will handle it */ }
657
+ }
632
658
  writeV2Profile(ws.profileName, {
633
659
  version: 2,
634
660
  workspace_id: ws.id,
635
661
  workspace_name: ws.name,
636
662
  api_url: apiUrl,
637
663
  app_url: deriveAppUrl(apiUrl),
638
- token: resolvedToken,
664
+ token: wsToken,
639
665
  created_at: new Date().toISOString(),
640
666
  expires_at: expiresAt || undefined,
641
667
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tacitai/mcp-server",
3
- "version": "0.6.47",
3
+ "version": "0.6.49",
4
4
  "description": "stdio MCP server wrapping Tacit /api/v1 REST/SSE API",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {