@tacitai/mcp-server 0.6.48 → 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/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);
package/dist/setup.js CHANGED
@@ -627,16 +627,41 @@ export async function setup(argv) {
627
627
  fs.writeFileSync(dest, output, { encoding: 'utf-8', mode: 0o600 });
628
628
  fs.chmodSync(dest, 0o600);
629
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.
630
632
  const writtenProfiles = [];
631
633
  if (resolvedToken && allWorkspaces.length > 0) {
632
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
+ }
633
658
  writeV2Profile(ws.profileName, {
634
659
  version: 2,
635
660
  workspace_id: ws.id,
636
661
  workspace_name: ws.name,
637
662
  api_url: apiUrl,
638
663
  app_url: deriveAppUrl(apiUrl),
639
- token: resolvedToken,
664
+ token: wsToken,
640
665
  created_at: new Date().toISOString(),
641
666
  expires_at: expiresAt || undefined,
642
667
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tacitai/mcp-server",
3
- "version": "0.6.48",
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": {