@icoretech/warden-mcp 0.2.12 → 0.2.14
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/bwCli.js +31 -0
- package/dist/bw/bwSession.js +125 -14
- package/dist/sdk/keychainSdk.js +64 -3
- package/package.json +3 -3
package/dist/bw/bwCli.js
CHANGED
|
@@ -13,6 +13,37 @@ export class BwCliError extends Error {
|
|
|
13
13
|
this.stderr = opts.stderr;
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
+
export function isBwAuthSessionInvalidError(error) {
|
|
17
|
+
if (!(error instanceof BwCliError))
|
|
18
|
+
return false;
|
|
19
|
+
const combined = [error.stderr, error.stdout, error.message]
|
|
20
|
+
.join('\n')
|
|
21
|
+
.toLowerCase();
|
|
22
|
+
if (/not found/.test(combined) ||
|
|
23
|
+
/more than one result/.test(combined) ||
|
|
24
|
+
/multiple results/.test(combined) ||
|
|
25
|
+
/ambiguous/.test(combined) ||
|
|
26
|
+
/invalid search/.test(combined) ||
|
|
27
|
+
/could not connect/.test(combined) ||
|
|
28
|
+
/connection/.test(combined) ||
|
|
29
|
+
/network/.test(combined) ||
|
|
30
|
+
/timeout/.test(combined) ||
|
|
31
|
+
/timed out/.test(combined) ||
|
|
32
|
+
/server error/.test(combined)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
return (/invalid\s+(bw\s+)?session/.test(combined) ||
|
|
36
|
+
/(bw\s+)?session\s+(is\s+)?invalid/.test(combined) ||
|
|
37
|
+
/expired\s+(bw\s+)?session/.test(combined) ||
|
|
38
|
+
/(bw\s+)?session\s+(has\s+)?expired/.test(combined) ||
|
|
39
|
+
/not logged in/.test(combined) ||
|
|
40
|
+
/not authenticated/.test(combined) ||
|
|
41
|
+
/you are not authenticated/.test(combined) ||
|
|
42
|
+
/vault is locked/.test(combined) ||
|
|
43
|
+
/please unlock/.test(combined) ||
|
|
44
|
+
/unlock your vault/.test(combined) ||
|
|
45
|
+
/requires an unlocked vault/.test(combined));
|
|
46
|
+
}
|
|
16
47
|
export async function runBw(args, opts = {}) {
|
|
17
48
|
const bwBin = process.env.BW_BIN ?? resolveBundledBwBin() ?? 'bw';
|
|
18
49
|
// Ensure the CLI never blocks waiting for a prompt (e.g. master password).
|
package/dist/bw/bwSession.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// src/bw/bwSession.ts
|
|
2
|
-
import { mkdir, rm } from 'node:fs/promises';
|
|
2
|
+
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
|
-
import { runBw } from './bwCli.js';
|
|
4
|
+
import { isBwAuthSessionInvalidError, 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;
|
|
@@ -50,15 +50,18 @@ export class BwSessionManager {
|
|
|
50
50
|
session = null;
|
|
51
51
|
templateItem = null;
|
|
52
52
|
keepaliveTimer = null;
|
|
53
|
+
warmupPromise = null;
|
|
53
54
|
configuredHost = null;
|
|
54
55
|
homeDir;
|
|
55
56
|
appDataDir;
|
|
56
57
|
processLockDir;
|
|
58
|
+
sessionStatePath;
|
|
57
59
|
constructor(env) {
|
|
58
60
|
this.env = env;
|
|
59
61
|
this.homeDir = env.homeDir ?? process.env.HOME ?? '/data';
|
|
60
62
|
this.appDataDir = join(this.homeDir, '.bitwarden-cli');
|
|
61
63
|
this.processLockDir = join(this.appDataDir, '.warden-mcp-auth-lock');
|
|
64
|
+
this.sessionStatePath = join(this.appDataDir, '.warden-mcp-session.json');
|
|
62
65
|
}
|
|
63
66
|
baseEnv(extra) {
|
|
64
67
|
return {
|
|
@@ -106,7 +109,16 @@ export class BwSessionManager {
|
|
|
106
109
|
async withSession(fn) {
|
|
107
110
|
return this.lock.runExclusive(async () => {
|
|
108
111
|
const session = await this.ensureUnlockedInternal();
|
|
109
|
-
|
|
112
|
+
try {
|
|
113
|
+
return await fn(session);
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
if (!isBwAuthSessionInvalidError(error))
|
|
117
|
+
throw error;
|
|
118
|
+
await this.clearSessionStateLocked();
|
|
119
|
+
const refreshedSession = await this.ensureUnlockedInternal();
|
|
120
|
+
return fn(refreshedSession);
|
|
121
|
+
}
|
|
110
122
|
});
|
|
111
123
|
}
|
|
112
124
|
async getTemplateItem() {
|
|
@@ -140,7 +152,24 @@ export class BwSessionManager {
|
|
|
140
152
|
: this.env.login.method === 'userpass'
|
|
141
153
|
? this.env.login.user
|
|
142
154
|
: null;
|
|
143
|
-
|
|
155
|
+
let isUnlocked = rawStatus === 'unlocked';
|
|
156
|
+
if (!isUnlocked) {
|
|
157
|
+
if (this.session && (await this.isSessionValid(this.session))) {
|
|
158
|
+
isUnlocked = true;
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
const storedSession = await this.readStoredSessionState();
|
|
162
|
+
if (storedSession?.session &&
|
|
163
|
+
(await this.isSessionValid(storedSession.session))) {
|
|
164
|
+
this.session = storedSession.session;
|
|
165
|
+
await this.writeStoredSessionState(storedSession.session);
|
|
166
|
+
isUnlocked = true;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (rawStatus === 'locked') {
|
|
171
|
+
this.startBackgroundUnlock();
|
|
172
|
+
}
|
|
144
173
|
const summaryParts = isUnlocked
|
|
145
174
|
? ['Vault access ready']
|
|
146
175
|
: ['Vault access not ready'];
|
|
@@ -166,9 +195,13 @@ export class BwSessionManager {
|
|
|
166
195
|
env: this.baseEnv(opts.env),
|
|
167
196
|
});
|
|
168
197
|
}
|
|
169
|
-
async
|
|
198
|
+
async clearSessionStateLocked() {
|
|
170
199
|
this.session = null;
|
|
171
200
|
this.templateItem = null;
|
|
201
|
+
await this.clearStoredSessionState();
|
|
202
|
+
}
|
|
203
|
+
async resetCliProfile() {
|
|
204
|
+
await this.clearSessionStateLocked();
|
|
172
205
|
this.configuredHost = null;
|
|
173
206
|
await runBw(['logout'], { env: this.baseEnv(), timeoutMs: 30_000 }).catch(() => { });
|
|
174
207
|
const home = this.homeDir;
|
|
@@ -188,6 +221,18 @@ export class BwSessionManager {
|
|
|
188
221
|
}
|
|
189
222
|
}
|
|
190
223
|
async currentServerUrl() {
|
|
224
|
+
try {
|
|
225
|
+
const dataPath = join(this.appDataDir, 'data.json');
|
|
226
|
+
const raw = await readFile(dataPath, 'utf8');
|
|
227
|
+
const parsed = JSON.parse(raw);
|
|
228
|
+
const base = parsed.global_environment_environment?.urls?.base;
|
|
229
|
+
if (typeof base === 'string' && base.length > 0) {
|
|
230
|
+
return base;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
// Fall back to the CLI if local state is absent or unreadable.
|
|
235
|
+
}
|
|
191
236
|
try {
|
|
192
237
|
const { stdout } = await runBw(['status'], {
|
|
193
238
|
env: this.baseEnv(),
|
|
@@ -206,17 +251,19 @@ export class BwSessionManager {
|
|
|
206
251
|
await this.ensureServerConfigured();
|
|
207
252
|
// If we already have a session, check if it still works.
|
|
208
253
|
if (this.session) {
|
|
209
|
-
|
|
210
|
-
const { stdout } = await runBw(['--session', this.session, 'unlock', '--check'], {
|
|
211
|
-
env: this.baseEnv(),
|
|
212
|
-
timeoutMs: 30_000,
|
|
213
|
-
});
|
|
214
|
-
void stdout;
|
|
254
|
+
if (await this.isSessionValid(this.session)) {
|
|
215
255
|
return this.session;
|
|
216
256
|
}
|
|
217
|
-
|
|
218
|
-
|
|
257
|
+
this.session = null;
|
|
258
|
+
}
|
|
259
|
+
const storedSession = await this.readStoredSessionState();
|
|
260
|
+
if (storedSession?.session) {
|
|
261
|
+
if (await this.isSessionValid(storedSession.session)) {
|
|
262
|
+
this.session = storedSession.session;
|
|
263
|
+
await this.writeStoredSessionState(storedSession.session);
|
|
264
|
+
return storedSession.session;
|
|
219
265
|
}
|
|
266
|
+
await this.clearStoredSessionState();
|
|
220
267
|
}
|
|
221
268
|
const unlockEnv = this.baseEnv({
|
|
222
269
|
BW_PASSWORD: this.env.password,
|
|
@@ -252,7 +299,10 @@ export class BwSessionManager {
|
|
|
252
299
|
'BW_PASSWORD',
|
|
253
300
|
'--raw',
|
|
254
301
|
], { env: unlockEnv, timeoutMs: 60_000, noInteraction: false });
|
|
255
|
-
|
|
302
|
+
const session = stdout.trim();
|
|
303
|
+
if (!session)
|
|
304
|
+
return { completed: false, session: '' };
|
|
305
|
+
return { completed: true, session };
|
|
256
306
|
}
|
|
257
307
|
catch {
|
|
258
308
|
return { completed: false, session: '' };
|
|
@@ -293,9 +343,70 @@ export class BwSessionManager {
|
|
|
293
343
|
if (!session)
|
|
294
344
|
throw new Error('bw login/unlock returned an empty session');
|
|
295
345
|
this.session = session;
|
|
346
|
+
await this.writeStoredSessionState(session);
|
|
296
347
|
return session;
|
|
297
348
|
});
|
|
298
349
|
}
|
|
350
|
+
startBackgroundUnlock() {
|
|
351
|
+
if (this.session || this.warmupPromise)
|
|
352
|
+
return;
|
|
353
|
+
this.warmupPromise = this.withSession(async () => undefined)
|
|
354
|
+
.catch(() => { })
|
|
355
|
+
.finally(() => {
|
|
356
|
+
this.warmupPromise = null;
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
sessionIdentity() {
|
|
360
|
+
return this.env.login.method === 'apikey'
|
|
361
|
+
? `apikey:${this.env.login.clientId}`
|
|
362
|
+
: `userpass:${this.env.login.user}`;
|
|
363
|
+
}
|
|
364
|
+
async isSessionValid(session) {
|
|
365
|
+
try {
|
|
366
|
+
const { stdout } = await runBw(['--session', session, 'unlock', '--check'], {
|
|
367
|
+
env: this.baseEnv(),
|
|
368
|
+
timeoutMs: 30_000,
|
|
369
|
+
});
|
|
370
|
+
void stdout;
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
catch {
|
|
374
|
+
return false;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
async readStoredSessionState() {
|
|
378
|
+
try {
|
|
379
|
+
const raw = await readFile(this.sessionStatePath, 'utf8');
|
|
380
|
+
const parsed = JSON.parse(raw);
|
|
381
|
+
if (parsed.version !== 1 ||
|
|
382
|
+
parsed.host !== this.env.host ||
|
|
383
|
+
parsed.identity !== this.sessionIdentity() ||
|
|
384
|
+
typeof parsed.session !== 'string' ||
|
|
385
|
+
parsed.session.length === 0) {
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
return parsed;
|
|
389
|
+
}
|
|
390
|
+
catch {
|
|
391
|
+
return null;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
async writeStoredSessionState(session) {
|
|
395
|
+
const now = new Date().toISOString();
|
|
396
|
+
const state = {
|
|
397
|
+
version: 1,
|
|
398
|
+
host: this.env.host,
|
|
399
|
+
identity: this.sessionIdentity(),
|
|
400
|
+
session,
|
|
401
|
+
createdAt: now,
|
|
402
|
+
validatedAt: now,
|
|
403
|
+
};
|
|
404
|
+
await mkdir(this.appDataDir, { recursive: true });
|
|
405
|
+
await writeFile(this.sessionStatePath, JSON.stringify(state), 'utf8');
|
|
406
|
+
}
|
|
407
|
+
async clearStoredSessionState() {
|
|
408
|
+
await rm(this.sessionStatePath, { force: true }).catch(() => { });
|
|
409
|
+
}
|
|
299
410
|
async withProcessAuthLock(fn) {
|
|
300
411
|
const startedAt = Date.now();
|
|
301
412
|
await mkdir(this.appDataDir, { recursive: true });
|
package/dist/sdk/keychainSdk.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { tmpdir } from 'node:os';
|
|
4
4
|
import { basename, join } from 'node:path';
|
|
5
|
-
import { BwCliError } from '../bw/bwCli.js';
|
|
5
|
+
import { BwCliError, isBwAuthSessionInvalidError } from '../bw/bwCli.js';
|
|
6
6
|
import { deepClone } from './clone.js';
|
|
7
7
|
import { buildBwGenerateArgs } from './generateArgs.js';
|
|
8
8
|
import { applyItemPatch } from './patch.js';
|
|
@@ -68,6 +68,30 @@ function denormalizeUris(raw) {
|
|
|
68
68
|
}
|
|
69
69
|
return out;
|
|
70
70
|
}
|
|
71
|
+
function isUsernameLookupFailure(error) {
|
|
72
|
+
const combined = [error.stderr, error.stdout, error.message]
|
|
73
|
+
.join('\n')
|
|
74
|
+
.toLowerCase();
|
|
75
|
+
if (/could not connect/.test(combined) ||
|
|
76
|
+
/connection/.test(combined) ||
|
|
77
|
+
/network/.test(combined) ||
|
|
78
|
+
/timeout/.test(combined) ||
|
|
79
|
+
/timed out/.test(combined) ||
|
|
80
|
+
/server error/.test(combined) ||
|
|
81
|
+
/internal server error/.test(combined) ||
|
|
82
|
+
/bad gateway/.test(combined) ||
|
|
83
|
+
/service unavailable/.test(combined)) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
return (/not found/.test(combined) ||
|
|
87
|
+
/no item/.test(combined) ||
|
|
88
|
+
/no matching/.test(combined) ||
|
|
89
|
+
/more than one result/.test(combined) ||
|
|
90
|
+
/multiple results/.test(combined) ||
|
|
91
|
+
/ambiguous/.test(combined) ||
|
|
92
|
+
/invalid search/.test(combined) ||
|
|
93
|
+
/invalid lookup/.test(combined));
|
|
94
|
+
}
|
|
71
95
|
function kindFromItem(item) {
|
|
72
96
|
const type = item.type;
|
|
73
97
|
if (type === ITEM_TYPE.login)
|
|
@@ -927,9 +951,46 @@ export class KeychainSdk {
|
|
|
927
951
|
}
|
|
928
952
|
/** Always reveals — username is not considered a secret by Bitwarden. */
|
|
929
953
|
async getUsername(input) {
|
|
954
|
+
const term = input.term.trim();
|
|
955
|
+
if (!term)
|
|
956
|
+
throw new Error('Username lookup term is empty');
|
|
930
957
|
return this.bw.withSession(async (session) => {
|
|
931
|
-
|
|
932
|
-
|
|
958
|
+
try {
|
|
959
|
+
const { stdout } = await this.bw.runForSession(session, ['--raw', 'get', 'username', term], { timeoutMs: 60_000 });
|
|
960
|
+
const username = stdout.trim();
|
|
961
|
+
if (!username)
|
|
962
|
+
throw new Error('Username lookup found an empty username');
|
|
963
|
+
return this.valueResult(username, true);
|
|
964
|
+
}
|
|
965
|
+
catch (error) {
|
|
966
|
+
if (isBwAuthSessionInvalidError(error))
|
|
967
|
+
throw error;
|
|
968
|
+
if (!(error instanceof BwCliError) || error.exitCode !== 1)
|
|
969
|
+
throw error;
|
|
970
|
+
if (!isUsernameLookupFailure(error))
|
|
971
|
+
throw error;
|
|
972
|
+
const { stdout } = await this.bw.runForSession(session, ['list', 'items', '--search', term], { timeoutMs: 120_000 });
|
|
973
|
+
const rawResults = this.parseBwJson(stdout);
|
|
974
|
+
const candidates = rawResults.filter((raw) => !!raw &&
|
|
975
|
+
typeof raw === 'object' &&
|
|
976
|
+
raw.type === ITEM_TYPE.login);
|
|
977
|
+
if (candidates.length === 0) {
|
|
978
|
+
throw new Error('Username lookup failed: no login item found');
|
|
979
|
+
}
|
|
980
|
+
const exactCandidates = candidates.filter((item) => this.candidateMatchesTerm(item, [term]));
|
|
981
|
+
const narrowed = exactCandidates.length > 0 ? exactCandidates : candidates;
|
|
982
|
+
if (narrowed.length !== 1) {
|
|
983
|
+
throw new Error('Username lookup failed: multiple matching login items found');
|
|
984
|
+
}
|
|
985
|
+
const login = narrowed[0]?.login && typeof narrowed[0].login === 'object'
|
|
986
|
+
? narrowed[0].login
|
|
987
|
+
: null;
|
|
988
|
+
const username = login?.username;
|
|
989
|
+
if (typeof username !== 'string' || username.trim().length === 0) {
|
|
990
|
+
throw new Error('Username lookup found an empty username');
|
|
991
|
+
}
|
|
992
|
+
return this.valueResult(username.trim(), true);
|
|
993
|
+
}
|
|
933
994
|
});
|
|
934
995
|
}
|
|
935
996
|
/** Requires opts.reveal=true; returns {value: null, revealed: false} when ungated. */
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"private": false,
|
|
3
3
|
"name": "@icoretech/warden-mcp",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.14",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "Vaultwarden/Bitwarden MCP server backed by Bitwarden CLI (bw).",
|
|
@@ -49,10 +49,10 @@
|
|
|
49
49
|
"zod": "^4.3.6"
|
|
50
50
|
},
|
|
51
51
|
"optionalDependencies": {
|
|
52
|
-
"@bitwarden/cli": "2026.
|
|
52
|
+
"@bitwarden/cli": "2026.4.1"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@biomejs/biome": "^2.4.
|
|
55
|
+
"@biomejs/biome": "^2.4.13",
|
|
56
56
|
"@types/express": "^5.0.6",
|
|
57
57
|
"@types/node": "^25.6.0",
|
|
58
58
|
"playwright": "1.59.1",
|