@icoretech/warden-mcp 0.2.11 → 0.2.12
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 +152 -119
- 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, rm } 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,12 @@ export class BwSessionManager {
|
|
|
51
53
|
configuredHost = null;
|
|
52
54
|
homeDir;
|
|
53
55
|
appDataDir;
|
|
56
|
+
processLockDir;
|
|
54
57
|
constructor(env) {
|
|
55
58
|
this.env = env;
|
|
56
59
|
this.homeDir = env.homeDir ?? process.env.HOME ?? '/data';
|
|
57
60
|
this.appDataDir = join(this.homeDir, '.bitwarden-cli');
|
|
61
|
+
this.processLockDir = join(this.appDataDir, '.warden-mcp-auth-lock');
|
|
58
62
|
}
|
|
59
63
|
baseEnv(extra) {
|
|
60
64
|
return {
|
|
@@ -86,6 +90,8 @@ export class BwSessionManager {
|
|
|
86
90
|
return;
|
|
87
91
|
const intervalMs = Math.max(10, this.env.unlockIntervalSeconds) * 1000;
|
|
88
92
|
this.keepaliveTimer = setInterval(() => {
|
|
93
|
+
if (!this.session)
|
|
94
|
+
return;
|
|
89
95
|
void this.lock.runExclusive(async () => {
|
|
90
96
|
try {
|
|
91
97
|
await this.ensureUnlockedInternal();
|
|
@@ -114,39 +120,43 @@ export class BwSessionManager {
|
|
|
114
120
|
return this.getTemplateItemLocked(session);
|
|
115
121
|
}
|
|
116
122
|
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
|
-
};
|
|
123
|
+
const { stdout } = await runBw(['status'], {
|
|
124
|
+
env: this.baseEnv(),
|
|
125
|
+
timeoutMs: 60_000,
|
|
149
126
|
});
|
|
127
|
+
let parsed;
|
|
128
|
+
try {
|
|
129
|
+
parsed = JSON.parse(stdout);
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
throw new Error(`Failed to parse bw status output (${stdout.length} bytes)`, {
|
|
133
|
+
cause: err,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
const rawStatus = typeof parsed.status === 'string' ? parsed.status : null;
|
|
137
|
+
const serverUrl = typeof parsed.serverUrl === 'string' ? parsed.serverUrl : this.env.host;
|
|
138
|
+
const userEmail = typeof parsed.userEmail === 'string'
|
|
139
|
+
? parsed.userEmail
|
|
140
|
+
: this.env.login.method === 'userpass'
|
|
141
|
+
? this.env.login.user
|
|
142
|
+
: null;
|
|
143
|
+
const isUnlocked = rawStatus === 'unlocked';
|
|
144
|
+
const summaryParts = isUnlocked
|
|
145
|
+
? ['Vault access ready']
|
|
146
|
+
: ['Vault access not ready'];
|
|
147
|
+
if (userEmail)
|
|
148
|
+
summaryParts.push(`for ${userEmail}`);
|
|
149
|
+
if (serverUrl)
|
|
150
|
+
summaryParts.push(`on ${serverUrl}`);
|
|
151
|
+
return {
|
|
152
|
+
...parsed,
|
|
153
|
+
summary: `${summaryParts.join(' ')}.`,
|
|
154
|
+
operational: {
|
|
155
|
+
ready: isUnlocked,
|
|
156
|
+
sessionValid: isUnlocked,
|
|
157
|
+
source: 'session_manager',
|
|
158
|
+
},
|
|
159
|
+
};
|
|
150
160
|
}
|
|
151
161
|
// Run a bw command within an existing session, using this manager's HOME/profile.
|
|
152
162
|
// Intended to be used from inside `withSession` to avoid relocking.
|
|
@@ -191,102 +201,125 @@ export class BwSessionManager {
|
|
|
191
201
|
}
|
|
192
202
|
}
|
|
193
203
|
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 {
|
|
208
|
-
this.session = null;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
const unlockEnv = this.baseEnv({
|
|
212
|
-
BW_PASSWORD: this.env.password,
|
|
213
|
-
BW_HOST: this.env.host,
|
|
214
|
-
});
|
|
215
|
-
const tryUnlock = async () => {
|
|
216
|
-
try {
|
|
217
|
-
const { stdout } = await runBw(['unlock', '--passwordenv', 'BW_PASSWORD', '--raw'], { env: unlockEnv, timeoutMs: 60_000, noInteraction: false });
|
|
218
|
-
return stdout.trim();
|
|
219
|
-
}
|
|
220
|
-
catch {
|
|
221
|
-
return '';
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
const tryLoginRaw = async () => {
|
|
225
|
-
try {
|
|
226
|
-
if (this.env.login.method === 'apikey') {
|
|
227
|
-
const { stdout } = await runBw(['login', '--apikey', '--raw'], {
|
|
228
|
-
env: this.baseEnv({
|
|
229
|
-
BW_CLIENTID: this.env.login.clientId,
|
|
230
|
-
BW_CLIENTSECRET: this.env.login.clientSecret,
|
|
231
|
-
BW_HOST: this.env.host,
|
|
232
|
-
}),
|
|
233
|
-
timeoutMs: 60_000,
|
|
234
|
-
noInteraction: false,
|
|
204
|
+
return this.withProcessAuthLock(async () => {
|
|
205
|
+
// Ensure server config points to BW_HOST.
|
|
206
|
+
await this.ensureServerConfigured();
|
|
207
|
+
// If we already have a session, check if it still works.
|
|
208
|
+
if (this.session) {
|
|
209
|
+
try {
|
|
210
|
+
const { stdout } = await runBw(['--session', this.session, 'unlock', '--check'], {
|
|
211
|
+
env: this.baseEnv(),
|
|
212
|
+
timeoutMs: 30_000,
|
|
235
213
|
});
|
|
236
|
-
|
|
214
|
+
void stdout;
|
|
215
|
+
return this.session;
|
|
237
216
|
}
|
|
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);
|
|
217
|
+
catch {
|
|
218
|
+
this.session = null;
|
|
258
219
|
}
|
|
259
220
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
221
|
+
const unlockEnv = this.baseEnv({
|
|
222
|
+
BW_PASSWORD: this.env.password,
|
|
223
|
+
BW_HOST: this.env.host,
|
|
224
|
+
});
|
|
225
|
+
const tryUnlock = async () => {
|
|
226
|
+
try {
|
|
227
|
+
const { stdout } = await runBw(['unlock', '--passwordenv', 'BW_PASSWORD', '--raw'], { env: unlockEnv, timeoutMs: 60_000, noInteraction: false });
|
|
228
|
+
return stdout.trim();
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
return '';
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
const tryLoginRaw = async () => {
|
|
235
|
+
try {
|
|
236
|
+
if (this.env.login.method === 'apikey') {
|
|
237
|
+
const { stdout } = await runBw(['login', '--apikey', '--raw'], {
|
|
238
|
+
env: this.baseEnv({
|
|
239
|
+
BW_CLIENTID: this.env.login.clientId,
|
|
240
|
+
BW_CLIENTSECRET: this.env.login.clientSecret,
|
|
241
|
+
BW_HOST: this.env.host,
|
|
242
|
+
}),
|
|
243
|
+
timeoutMs: 60_000,
|
|
244
|
+
noInteraction: false,
|
|
245
|
+
});
|
|
246
|
+
return { completed: true, session: stdout.trim() };
|
|
247
|
+
}
|
|
248
|
+
const { stdout } = await runBw([
|
|
249
|
+
'login',
|
|
250
|
+
this.env.login.user,
|
|
251
|
+
'--passwordenv',
|
|
252
|
+
'BW_PASSWORD',
|
|
253
|
+
'--raw',
|
|
254
|
+
], { env: unlockEnv, timeoutMs: 60_000, noInteraction: false });
|
|
255
|
+
return { completed: true, session: stdout.trim() };
|
|
256
|
+
}
|
|
257
|
+
catch {
|
|
258
|
+
return { completed: false, session: '' };
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
const retryUnlockAfterLogin = async () => {
|
|
262
|
+
for (let attempt = 0; attempt < POST_LOGIN_UNLOCK_RETRY_ATTEMPTS; attempt += 1) {
|
|
263
|
+
const session = await tryUnlock();
|
|
264
|
+
if (session)
|
|
265
|
+
return session;
|
|
266
|
+
if (attempt < POST_LOGIN_UNLOCK_RETRY_ATTEMPTS - 1) {
|
|
267
|
+
await sleep(POST_LOGIN_UNLOCK_RETRY_DELAY_MS);
|
|
268
|
+
}
|
|
271
269
|
}
|
|
272
|
-
|
|
273
|
-
|
|
270
|
+
return '';
|
|
271
|
+
};
|
|
272
|
+
const obtainSession = async () => {
|
|
273
|
+
let session = await tryUnlock();
|
|
274
|
+
if (!session) {
|
|
275
|
+
const login = await tryLoginRaw();
|
|
276
|
+
if (login.session) {
|
|
277
|
+
session = login.session;
|
|
278
|
+
}
|
|
279
|
+
else if (login.completed) {
|
|
280
|
+
session = await retryUnlockAfterLogin();
|
|
281
|
+
}
|
|
274
282
|
}
|
|
283
|
+
if (!session)
|
|
284
|
+
session = await tryUnlock();
|
|
285
|
+
return session;
|
|
286
|
+
};
|
|
287
|
+
let session = await obtainSession();
|
|
288
|
+
if (!session) {
|
|
289
|
+
await this.resetCliProfile();
|
|
290
|
+
await this.ensureServerConfigured();
|
|
291
|
+
session = await obtainSession();
|
|
275
292
|
}
|
|
276
293
|
if (!session)
|
|
277
|
-
|
|
294
|
+
throw new Error('bw login/unlock returned an empty session');
|
|
295
|
+
this.session = session;
|
|
278
296
|
return session;
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
async withProcessAuthLock(fn) {
|
|
300
|
+
const startedAt = Date.now();
|
|
301
|
+
await mkdir(this.appDataDir, { recursive: true });
|
|
302
|
+
while (true) {
|
|
303
|
+
try {
|
|
304
|
+
await mkdir(this.processLockDir);
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
const err = error;
|
|
309
|
+
if (err?.code !== 'EEXIST')
|
|
310
|
+
throw error;
|
|
311
|
+
if (Date.now() - startedAt >= PROCESS_LOCK_TIMEOUT_MS) {
|
|
312
|
+
throw new Error(`Timed out waiting for process auth lock after ${PROCESS_LOCK_TIMEOUT_MS}ms`);
|
|
313
|
+
}
|
|
314
|
+
await sleep(PROCESS_LOCK_WAIT_MS);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
try {
|
|
318
|
+
return await fn();
|
|
319
|
+
}
|
|
320
|
+
finally {
|
|
321
|
+
await rm(this.processLockDir, { recursive: true, force: true }).catch(() => { });
|
|
285
322
|
}
|
|
286
|
-
if (!session)
|
|
287
|
-
throw new Error('bw login/unlock returned an empty session');
|
|
288
|
-
this.session = session;
|
|
289
|
-
return session;
|
|
290
323
|
}
|
|
291
324
|
async ensureServerConfigured() {
|
|
292
325
|
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 () => {
|