@icoretech/warden-mcp 0.2.11 → 0.2.13
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/bw/bwSession.js +218 -116
- package/dist/transports/stdio.js +0 -3
- package/package.json +1 -1
package/dist/bw/bwSession.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// src/bw/bwSession.ts
|
|
2
|
-
import { rm } from 'node:fs/promises';
|
|
2
|
+
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { runBw } from './bwCli.js';
|
|
5
5
|
import { Mutex } from './mutex.js';
|
|
6
6
|
const POST_LOGIN_UNLOCK_RETRY_ATTEMPTS = 20;
|
|
7
7
|
const POST_LOGIN_UNLOCK_RETRY_DELAY_MS = 2_000;
|
|
8
|
+
const PROCESS_LOCK_WAIT_MS = 100;
|
|
9
|
+
const PROCESS_LOCK_TIMEOUT_MS = 90_000;
|
|
8
10
|
function sleep(ms) {
|
|
9
11
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
10
12
|
}
|
|
@@ -51,10 +53,14 @@ export class BwSessionManager {
|
|
|
51
53
|
configuredHost = null;
|
|
52
54
|
homeDir;
|
|
53
55
|
appDataDir;
|
|
56
|
+
processLockDir;
|
|
57
|
+
sessionStatePath;
|
|
54
58
|
constructor(env) {
|
|
55
59
|
this.env = env;
|
|
56
60
|
this.homeDir = env.homeDir ?? process.env.HOME ?? '/data';
|
|
57
61
|
this.appDataDir = join(this.homeDir, '.bitwarden-cli');
|
|
62
|
+
this.processLockDir = join(this.appDataDir, '.warden-mcp-auth-lock');
|
|
63
|
+
this.sessionStatePath = join(this.appDataDir, '.warden-mcp-session.json');
|
|
58
64
|
}
|
|
59
65
|
baseEnv(extra) {
|
|
60
66
|
return {
|
|
@@ -86,6 +92,8 @@ export class BwSessionManager {
|
|
|
86
92
|
return;
|
|
87
93
|
const intervalMs = Math.max(10, this.env.unlockIntervalSeconds) * 1000;
|
|
88
94
|
this.keepaliveTimer = setInterval(() => {
|
|
95
|
+
if (!this.session)
|
|
96
|
+
return;
|
|
89
97
|
void this.lock.runExclusive(async () => {
|
|
90
98
|
try {
|
|
91
99
|
await this.ensureUnlockedInternal();
|
|
@@ -114,39 +122,43 @@ export class BwSessionManager {
|
|
|
114
122
|
return this.getTemplateItemLocked(session);
|
|
115
123
|
}
|
|
116
124
|
async status() {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
timeoutMs: 60_000,
|
|
121
|
-
});
|
|
122
|
-
let parsed;
|
|
123
|
-
try {
|
|
124
|
-
parsed = JSON.parse(stdout);
|
|
125
|
-
}
|
|
126
|
-
catch (err) {
|
|
127
|
-
throw new Error(`Failed to parse bw status output (${stdout.length} bytes)`, { cause: err });
|
|
128
|
-
}
|
|
129
|
-
const serverUrl = typeof parsed.serverUrl === 'string' ? parsed.serverUrl : this.env.host;
|
|
130
|
-
const userEmail = typeof parsed.userEmail === 'string'
|
|
131
|
-
? parsed.userEmail
|
|
132
|
-
: this.env.login.method === 'userpass'
|
|
133
|
-
? this.env.login.user
|
|
134
|
-
: null;
|
|
135
|
-
const summaryParts = ['Vault access ready'];
|
|
136
|
-
if (userEmail)
|
|
137
|
-
summaryParts.push(`for ${userEmail}`);
|
|
138
|
-
if (serverUrl)
|
|
139
|
-
summaryParts.push(`on ${serverUrl}`);
|
|
140
|
-
return {
|
|
141
|
-
...parsed,
|
|
142
|
-
summary: `${summaryParts.join(' ')}.`,
|
|
143
|
-
operational: {
|
|
144
|
-
ready: true,
|
|
145
|
-
sessionValid: true,
|
|
146
|
-
source: 'session_manager',
|
|
147
|
-
},
|
|
148
|
-
};
|
|
125
|
+
const { stdout } = await runBw(['status'], {
|
|
126
|
+
env: this.baseEnv(),
|
|
127
|
+
timeoutMs: 60_000,
|
|
149
128
|
});
|
|
129
|
+
let parsed;
|
|
130
|
+
try {
|
|
131
|
+
parsed = JSON.parse(stdout);
|
|
132
|
+
}
|
|
133
|
+
catch (err) {
|
|
134
|
+
throw new Error(`Failed to parse bw status output (${stdout.length} bytes)`, {
|
|
135
|
+
cause: err,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
const rawStatus = typeof parsed.status === 'string' ? parsed.status : null;
|
|
139
|
+
const serverUrl = typeof parsed.serverUrl === 'string' ? parsed.serverUrl : this.env.host;
|
|
140
|
+
const userEmail = typeof parsed.userEmail === 'string'
|
|
141
|
+
? parsed.userEmail
|
|
142
|
+
: this.env.login.method === 'userpass'
|
|
143
|
+
? this.env.login.user
|
|
144
|
+
: null;
|
|
145
|
+
const isUnlocked = rawStatus === 'unlocked';
|
|
146
|
+
const summaryParts = isUnlocked
|
|
147
|
+
? ['Vault access ready']
|
|
148
|
+
: ['Vault access not ready'];
|
|
149
|
+
if (userEmail)
|
|
150
|
+
summaryParts.push(`for ${userEmail}`);
|
|
151
|
+
if (serverUrl)
|
|
152
|
+
summaryParts.push(`on ${serverUrl}`);
|
|
153
|
+
return {
|
|
154
|
+
...parsed,
|
|
155
|
+
summary: `${summaryParts.join(' ')}.`,
|
|
156
|
+
operational: {
|
|
157
|
+
ready: isUnlocked,
|
|
158
|
+
sessionValid: isUnlocked,
|
|
159
|
+
source: 'session_manager',
|
|
160
|
+
},
|
|
161
|
+
};
|
|
150
162
|
}
|
|
151
163
|
// Run a bw command within an existing session, using this manager's HOME/profile.
|
|
152
164
|
// Intended to be used from inside `withSession` to avoid relocking.
|
|
@@ -160,6 +172,7 @@ export class BwSessionManager {
|
|
|
160
172
|
this.session = null;
|
|
161
173
|
this.templateItem = null;
|
|
162
174
|
this.configuredHost = null;
|
|
175
|
+
await this.clearStoredSessionState();
|
|
163
176
|
await runBw(['logout'], { env: this.baseEnv(), timeoutMs: 30_000 }).catch(() => { });
|
|
164
177
|
const home = this.homeDir;
|
|
165
178
|
const cliStateDirs = [
|
|
@@ -178,6 +191,18 @@ export class BwSessionManager {
|
|
|
178
191
|
}
|
|
179
192
|
}
|
|
180
193
|
async currentServerUrl() {
|
|
194
|
+
try {
|
|
195
|
+
const dataPath = join(this.appDataDir, 'data.json');
|
|
196
|
+
const raw = await readFile(dataPath, 'utf8');
|
|
197
|
+
const parsed = JSON.parse(raw);
|
|
198
|
+
const base = parsed.global_environment_environment?.urls?.base;
|
|
199
|
+
if (typeof base === 'string' && base.length > 0) {
|
|
200
|
+
return base;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
// Fall back to the CLI if local state is absent or unreadable.
|
|
205
|
+
}
|
|
181
206
|
try {
|
|
182
207
|
const { stdout } = await runBw(['status'], {
|
|
183
208
|
env: this.baseEnv(),
|
|
@@ -191,102 +216,179 @@ export class BwSessionManager {
|
|
|
191
216
|
}
|
|
192
217
|
}
|
|
193
218
|
async ensureUnlockedInternal() {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
});
|
|
203
|
-
// unlock --check prints "Vault is unlocked!" or similar; exit code 0 means ok.
|
|
204
|
-
void stdout;
|
|
205
|
-
return this.session;
|
|
206
|
-
}
|
|
207
|
-
catch {
|
|
219
|
+
return this.withProcessAuthLock(async () => {
|
|
220
|
+
// Ensure server config points to BW_HOST.
|
|
221
|
+
await this.ensureServerConfigured();
|
|
222
|
+
// If we already have a session, check if it still works.
|
|
223
|
+
if (this.session) {
|
|
224
|
+
if (await this.isSessionValid(this.session)) {
|
|
225
|
+
return this.session;
|
|
226
|
+
}
|
|
208
227
|
this.session = null;
|
|
209
228
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
return stdout.trim();
|
|
219
|
-
}
|
|
220
|
-
catch {
|
|
221
|
-
return '';
|
|
229
|
+
const storedSession = await this.readStoredSessionState();
|
|
230
|
+
if (storedSession?.session) {
|
|
231
|
+
if (await this.isSessionValid(storedSession.session)) {
|
|
232
|
+
this.session = storedSession.session;
|
|
233
|
+
await this.writeStoredSessionState(storedSession.session);
|
|
234
|
+
return storedSession.session;
|
|
235
|
+
}
|
|
236
|
+
await this.clearStoredSessionState();
|
|
222
237
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
238
|
+
const unlockEnv = this.baseEnv({
|
|
239
|
+
BW_PASSWORD: this.env.password,
|
|
240
|
+
BW_HOST: this.env.host,
|
|
241
|
+
});
|
|
242
|
+
const tryUnlock = async () => {
|
|
243
|
+
try {
|
|
244
|
+
const { stdout } = await runBw(['unlock', '--passwordenv', 'BW_PASSWORD', '--raw'], { env: unlockEnv, timeoutMs: 60_000, noInteraction: false });
|
|
245
|
+
return stdout.trim();
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
return '';
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
const tryLoginRaw = async () => {
|
|
252
|
+
try {
|
|
253
|
+
if (this.env.login.method === 'apikey') {
|
|
254
|
+
const { stdout } = await runBw(['login', '--apikey', '--raw'], {
|
|
255
|
+
env: this.baseEnv({
|
|
256
|
+
BW_CLIENTID: this.env.login.clientId,
|
|
257
|
+
BW_CLIENTSECRET: this.env.login.clientSecret,
|
|
258
|
+
BW_HOST: this.env.host,
|
|
259
|
+
}),
|
|
260
|
+
timeoutMs: 60_000,
|
|
261
|
+
noInteraction: false,
|
|
262
|
+
});
|
|
263
|
+
return { completed: true, session: stdout.trim() };
|
|
264
|
+
}
|
|
265
|
+
const { stdout } = await runBw([
|
|
266
|
+
'login',
|
|
267
|
+
this.env.login.user,
|
|
268
|
+
'--passwordenv',
|
|
269
|
+
'BW_PASSWORD',
|
|
270
|
+
'--raw',
|
|
271
|
+
], { env: unlockEnv, timeoutMs: 60_000, noInteraction: false });
|
|
236
272
|
return { completed: true, session: stdout.trim() };
|
|
237
273
|
}
|
|
238
|
-
|
|
239
|
-
'
|
|
240
|
-
this.env.login.user,
|
|
241
|
-
'--passwordenv',
|
|
242
|
-
'BW_PASSWORD',
|
|
243
|
-
'--raw',
|
|
244
|
-
], { env: unlockEnv, timeoutMs: 60_000, noInteraction: false });
|
|
245
|
-
return { completed: true, session: stdout.trim() };
|
|
246
|
-
}
|
|
247
|
-
catch {
|
|
248
|
-
return { completed: false, session: '' };
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
const retryUnlockAfterLogin = async () => {
|
|
252
|
-
for (let attempt = 0; attempt < POST_LOGIN_UNLOCK_RETRY_ATTEMPTS; attempt += 1) {
|
|
253
|
-
const session = await tryUnlock();
|
|
254
|
-
if (session)
|
|
255
|
-
return session;
|
|
256
|
-
if (attempt < POST_LOGIN_UNLOCK_RETRY_ATTEMPTS - 1) {
|
|
257
|
-
await sleep(POST_LOGIN_UNLOCK_RETRY_DELAY_MS);
|
|
274
|
+
catch {
|
|
275
|
+
return { completed: false, session: '' };
|
|
258
276
|
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
const login = await tryLoginRaw();
|
|
269
|
-
if (login.session) {
|
|
270
|
-
session = login.session;
|
|
277
|
+
};
|
|
278
|
+
const retryUnlockAfterLogin = async () => {
|
|
279
|
+
for (let attempt = 0; attempt < POST_LOGIN_UNLOCK_RETRY_ATTEMPTS; attempt += 1) {
|
|
280
|
+
const session = await tryUnlock();
|
|
281
|
+
if (session)
|
|
282
|
+
return session;
|
|
283
|
+
if (attempt < POST_LOGIN_UNLOCK_RETRY_ATTEMPTS - 1) {
|
|
284
|
+
await sleep(POST_LOGIN_UNLOCK_RETRY_DELAY_MS);
|
|
285
|
+
}
|
|
271
286
|
}
|
|
272
|
-
|
|
273
|
-
|
|
287
|
+
return '';
|
|
288
|
+
};
|
|
289
|
+
const obtainSession = async () => {
|
|
290
|
+
let session = await tryUnlock();
|
|
291
|
+
if (!session) {
|
|
292
|
+
const login = await tryLoginRaw();
|
|
293
|
+
if (login.session) {
|
|
294
|
+
session = login.session;
|
|
295
|
+
}
|
|
296
|
+
else if (login.completed) {
|
|
297
|
+
session = await retryUnlockAfterLogin();
|
|
298
|
+
}
|
|
274
299
|
}
|
|
300
|
+
if (!session)
|
|
301
|
+
session = await tryUnlock();
|
|
302
|
+
return session;
|
|
303
|
+
};
|
|
304
|
+
let session = await obtainSession();
|
|
305
|
+
if (!session) {
|
|
306
|
+
await this.resetCliProfile();
|
|
307
|
+
await this.ensureServerConfigured();
|
|
308
|
+
session = await obtainSession();
|
|
275
309
|
}
|
|
276
310
|
if (!session)
|
|
277
|
-
|
|
311
|
+
throw new Error('bw login/unlock returned an empty session');
|
|
312
|
+
this.session = session;
|
|
313
|
+
await this.writeStoredSessionState(session);
|
|
278
314
|
return session;
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
sessionIdentity() {
|
|
318
|
+
return this.env.login.method === 'apikey'
|
|
319
|
+
? `apikey:${this.env.login.clientId}`
|
|
320
|
+
: `userpass:${this.env.login.user}`;
|
|
321
|
+
}
|
|
322
|
+
async isSessionValid(session) {
|
|
323
|
+
try {
|
|
324
|
+
const { stdout } = await runBw(['--session', session, 'unlock', '--check'], {
|
|
325
|
+
env: this.baseEnv(),
|
|
326
|
+
timeoutMs: 30_000,
|
|
327
|
+
});
|
|
328
|
+
void stdout;
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
catch {
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
async readStoredSessionState() {
|
|
336
|
+
try {
|
|
337
|
+
const raw = await readFile(this.sessionStatePath, 'utf8');
|
|
338
|
+
const parsed = JSON.parse(raw);
|
|
339
|
+
if (parsed.version !== 1 ||
|
|
340
|
+
parsed.host !== this.env.host ||
|
|
341
|
+
parsed.identity !== this.sessionIdentity() ||
|
|
342
|
+
typeof parsed.session !== 'string' ||
|
|
343
|
+
parsed.session.length === 0) {
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
return parsed;
|
|
347
|
+
}
|
|
348
|
+
catch {
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
async writeStoredSessionState(session) {
|
|
353
|
+
const now = new Date().toISOString();
|
|
354
|
+
const state = {
|
|
355
|
+
version: 1,
|
|
356
|
+
host: this.env.host,
|
|
357
|
+
identity: this.sessionIdentity(),
|
|
358
|
+
session,
|
|
359
|
+
createdAt: now,
|
|
360
|
+
validatedAt: now,
|
|
279
361
|
};
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
362
|
+
await mkdir(this.appDataDir, { recursive: true });
|
|
363
|
+
await writeFile(this.sessionStatePath, JSON.stringify(state), 'utf8');
|
|
364
|
+
}
|
|
365
|
+
async clearStoredSessionState() {
|
|
366
|
+
await rm(this.sessionStatePath, { force: true }).catch(() => { });
|
|
367
|
+
}
|
|
368
|
+
async withProcessAuthLock(fn) {
|
|
369
|
+
const startedAt = Date.now();
|
|
370
|
+
await mkdir(this.appDataDir, { recursive: true });
|
|
371
|
+
while (true) {
|
|
372
|
+
try {
|
|
373
|
+
await mkdir(this.processLockDir);
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
catch (error) {
|
|
377
|
+
const err = error;
|
|
378
|
+
if (err?.code !== 'EEXIST')
|
|
379
|
+
throw error;
|
|
380
|
+
if (Date.now() - startedAt >= PROCESS_LOCK_TIMEOUT_MS) {
|
|
381
|
+
throw new Error(`Timed out waiting for process auth lock after ${PROCESS_LOCK_TIMEOUT_MS}ms`);
|
|
382
|
+
}
|
|
383
|
+
await sleep(PROCESS_LOCK_WAIT_MS);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
try {
|
|
387
|
+
return await fn();
|
|
388
|
+
}
|
|
389
|
+
finally {
|
|
390
|
+
await rm(this.processLockDir, { recursive: true, force: true }).catch(() => { });
|
|
285
391
|
}
|
|
286
|
-
if (!session)
|
|
287
|
-
throw new Error('bw login/unlock returned an empty session');
|
|
288
|
-
this.session = session;
|
|
289
|
-
return session;
|
|
290
392
|
}
|
|
291
393
|
async ensureServerConfigured() {
|
|
292
394
|
if (this.configuredHost === this.env.host)
|
package/dist/transports/stdio.js
CHANGED
|
@@ -17,9 +17,6 @@ export async function runStdioTransport() {
|
|
|
17
17
|
`${process.env.HOME ?? '/data'}/bw-profiles`,
|
|
18
18
|
});
|
|
19
19
|
const bw = await pool.getOrCreate(bwEnv);
|
|
20
|
-
await bw.status().catch(() => {
|
|
21
|
-
// Best-effort stdio prewarm only. Real tool calls still surface failures.
|
|
22
|
-
});
|
|
23
20
|
const server = new McpServer({ name: APP_NAME, version: SERVER_VERSION });
|
|
24
21
|
registerTools(server, {
|
|
25
22
|
getSdk: async () => {
|