@icoretech/warden-mcp 0.2.34 → 0.2.36
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/bin/patch-bitwarden-cli-lib.js +159 -33
- package/bin/patch-bitwarden-cli-policy.js +62 -0
- package/dist/bw/bwCli.js +29 -40
- package/dist/bw/bwCommandDisplay.js +133 -0
- package/dist/bw/bwEnv.js +34 -0
- package/dist/bw/bwSession.js +25 -434
- package/dist/bw/bwSessionAuth.js +201 -0
- package/dist/bw/bwSessionStorage.js +191 -0
- package/package.json +5 -5
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { runBw } from './bwCli.js';
|
|
2
|
+
import { BwSessionStorage } from './bwSessionStorage.js';
|
|
3
|
+
const POST_LOGIN_UNLOCK_RETRY_ATTEMPTS = 20;
|
|
4
|
+
const POST_LOGIN_UNLOCK_RETRY_DELAY_MS = 2_000;
|
|
5
|
+
function sleep(ms) {
|
|
6
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
7
|
+
}
|
|
8
|
+
export class BwSessionAuth {
|
|
9
|
+
env;
|
|
10
|
+
onSessionCleared;
|
|
11
|
+
storage;
|
|
12
|
+
session = null;
|
|
13
|
+
configuredHost = null;
|
|
14
|
+
constructor(env, onSessionCleared) {
|
|
15
|
+
this.env = env;
|
|
16
|
+
this.onSessionCleared = onSessionCleared;
|
|
17
|
+
const homeDir = env.homeDir ?? process.env.HOME ?? '/data';
|
|
18
|
+
const identity = env.login.method === 'apikey'
|
|
19
|
+
? `apikey:${env.login.clientId}`
|
|
20
|
+
: `userpass:${env.login.user}`;
|
|
21
|
+
this.storage = new BwSessionStorage(homeDir, env.host, identity);
|
|
22
|
+
}
|
|
23
|
+
hasSession() {
|
|
24
|
+
return this.session !== null;
|
|
25
|
+
}
|
|
26
|
+
async ensureUnlocked(invalidatedSession) {
|
|
27
|
+
return this.storage.withAuthLock(async () => {
|
|
28
|
+
if (invalidatedSession) {
|
|
29
|
+
this.clearInMemorySession();
|
|
30
|
+
if ((await this.storage.readSession()) === invalidatedSession) {
|
|
31
|
+
await this.storage.clearSession();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
await this.ensureServerConfigured();
|
|
35
|
+
if (this.session && (await this.isSessionValid(this.session))) {
|
|
36
|
+
return this.session;
|
|
37
|
+
}
|
|
38
|
+
this.session = null;
|
|
39
|
+
const storedSession = await this.storage.readSession();
|
|
40
|
+
if (storedSession) {
|
|
41
|
+
if (await this.isSessionValid(storedSession)) {
|
|
42
|
+
this.session = storedSession;
|
|
43
|
+
await this.storage.writeSession(storedSession);
|
|
44
|
+
return storedSession;
|
|
45
|
+
}
|
|
46
|
+
await this.storage.clearSession();
|
|
47
|
+
}
|
|
48
|
+
let session = await this.obtainSession();
|
|
49
|
+
if (!session) {
|
|
50
|
+
await this.resetCliProfile();
|
|
51
|
+
await this.ensureServerConfigured();
|
|
52
|
+
session = await this.obtainSession();
|
|
53
|
+
}
|
|
54
|
+
if (!session) {
|
|
55
|
+
throw new Error('bw login/unlock returned an empty session');
|
|
56
|
+
}
|
|
57
|
+
this.session = session;
|
|
58
|
+
await this.storage.writeSession(session);
|
|
59
|
+
return session;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
async hasValidSessionWithoutUnlock() {
|
|
63
|
+
if (this.session && (await this.isSessionValid(this.session)))
|
|
64
|
+
return true;
|
|
65
|
+
const storedSession = await this.storage.readSession();
|
|
66
|
+
if (!storedSession || !(await this.isSessionValid(storedSession))) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
this.session = storedSession;
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
currentSession() {
|
|
73
|
+
return this.session;
|
|
74
|
+
}
|
|
75
|
+
async isSessionValid(session) {
|
|
76
|
+
try {
|
|
77
|
+
const { stdout } = await runBw(['--session', session, 'unlock', '--check'], { env: this.storage.baseEnv(), timeoutMs: 30_000 });
|
|
78
|
+
void stdout;
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async tryUnlock() {
|
|
86
|
+
try {
|
|
87
|
+
const { stdout } = await runBw(['unlock', '--passwordenv', 'BW_PASSWORD', '--raw'], {
|
|
88
|
+
env: this.storage.baseEnv({
|
|
89
|
+
BW_PASSWORD: this.env.password,
|
|
90
|
+
BW_HOST: this.env.host,
|
|
91
|
+
}),
|
|
92
|
+
timeoutMs: 60_000,
|
|
93
|
+
noInteraction: false,
|
|
94
|
+
});
|
|
95
|
+
return stdout.trim();
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return '';
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async tryLogin() {
|
|
102
|
+
try {
|
|
103
|
+
if (this.env.login.method === 'apikey') {
|
|
104
|
+
const { stdout } = await runBw(['login', '--apikey', '--raw'], {
|
|
105
|
+
env: this.storage.baseEnv({
|
|
106
|
+
BW_CLIENTID: this.env.login.clientId,
|
|
107
|
+
BW_CLIENTSECRET: this.env.login.clientSecret,
|
|
108
|
+
BW_HOST: this.env.host,
|
|
109
|
+
}),
|
|
110
|
+
timeoutMs: 60_000,
|
|
111
|
+
noInteraction: false,
|
|
112
|
+
});
|
|
113
|
+
return { completed: true, session: stdout.trim() };
|
|
114
|
+
}
|
|
115
|
+
const { stdout } = await runBw(['login', this.env.login.user, '--passwordenv', 'BW_PASSWORD', '--raw'], {
|
|
116
|
+
env: this.storage.baseEnv({
|
|
117
|
+
BW_PASSWORD: this.env.password,
|
|
118
|
+
BW_HOST: this.env.host,
|
|
119
|
+
}),
|
|
120
|
+
timeoutMs: 60_000,
|
|
121
|
+
noInteraction: false,
|
|
122
|
+
});
|
|
123
|
+
const session = stdout.trim();
|
|
124
|
+
return { completed: session.length > 0, session };
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
return { completed: false, session: '' };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
async retryUnlockAfterLogin() {
|
|
131
|
+
for (let attempt = 0; attempt < POST_LOGIN_UNLOCK_RETRY_ATTEMPTS; attempt += 1) {
|
|
132
|
+
const session = await this.tryUnlock();
|
|
133
|
+
if (session)
|
|
134
|
+
return session;
|
|
135
|
+
if (attempt < POST_LOGIN_UNLOCK_RETRY_ATTEMPTS - 1) {
|
|
136
|
+
await sleep(POST_LOGIN_UNLOCK_RETRY_DELAY_MS);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return '';
|
|
140
|
+
}
|
|
141
|
+
async obtainSession() {
|
|
142
|
+
let session = await this.tryUnlock();
|
|
143
|
+
if (!session) {
|
|
144
|
+
const login = await this.tryLogin();
|
|
145
|
+
if (login.session) {
|
|
146
|
+
session = login.session;
|
|
147
|
+
}
|
|
148
|
+
else if (login.completed) {
|
|
149
|
+
session = await this.retryUnlockAfterLogin();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (!session)
|
|
153
|
+
session = await this.tryUnlock();
|
|
154
|
+
return session;
|
|
155
|
+
}
|
|
156
|
+
async resetCliProfile() {
|
|
157
|
+
this.clearInMemorySession();
|
|
158
|
+
await this.storage.clearSession();
|
|
159
|
+
this.configuredHost = null;
|
|
160
|
+
await runBw(['logout'], {
|
|
161
|
+
env: this.storage.baseEnv(),
|
|
162
|
+
timeoutMs: 30_000,
|
|
163
|
+
}).catch(() => { });
|
|
164
|
+
await this.storage.clearCliDataFiles();
|
|
165
|
+
}
|
|
166
|
+
async ensureServerConfigured() {
|
|
167
|
+
if (this.configuredHost === this.env.host)
|
|
168
|
+
return;
|
|
169
|
+
const currentHost = await this.storage.currentServerUrl();
|
|
170
|
+
if (currentHost === this.env.host) {
|
|
171
|
+
this.configuredHost = currentHost;
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
await runBw(['logout'], {
|
|
175
|
+
env: this.storage.baseEnv(),
|
|
176
|
+
timeoutMs: 30_000,
|
|
177
|
+
}).catch(() => { });
|
|
178
|
+
try {
|
|
179
|
+
await runBw(['config', 'server', this.env.host], {
|
|
180
|
+
env: this.storage.baseEnv(),
|
|
181
|
+
timeoutMs: 30_000,
|
|
182
|
+
});
|
|
183
|
+
this.configuredHost = this.env.host;
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
if (!(error instanceof Error))
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
await this.resetCliProfile();
|
|
191
|
+
await runBw(['config', 'server', this.env.host], {
|
|
192
|
+
env: this.storage.baseEnv(),
|
|
193
|
+
timeoutMs: 30_000,
|
|
194
|
+
});
|
|
195
|
+
this.configuredHost = this.env.host;
|
|
196
|
+
}
|
|
197
|
+
clearInMemorySession() {
|
|
198
|
+
this.session = null;
|
|
199
|
+
this.onSessionCleared();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { mkdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { runBw } from './bwCli.js';
|
|
4
|
+
const PROCESS_LOCK_WAIT_MS = 100;
|
|
5
|
+
const PROCESS_LOCK_TIMEOUT_MS = 90_000;
|
|
6
|
+
const PROCESS_LOCK_OWNER_FILE = 'owner.json';
|
|
7
|
+
function sleep(ms) {
|
|
8
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
9
|
+
}
|
|
10
|
+
function errorCode(error) {
|
|
11
|
+
if (typeof error !== 'object' || error === null || !('code' in error)) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
return error.code;
|
|
15
|
+
}
|
|
16
|
+
function isProcessLockOwner(value) {
|
|
17
|
+
return (typeof value === 'object' &&
|
|
18
|
+
value !== null &&
|
|
19
|
+
'pid' in value &&
|
|
20
|
+
'createdAt' in value &&
|
|
21
|
+
typeof value.pid === 'number' &&
|
|
22
|
+
Number.isInteger(value.pid) &&
|
|
23
|
+
typeof value.createdAt === 'string');
|
|
24
|
+
}
|
|
25
|
+
function isProcessAlive(pid) {
|
|
26
|
+
if (pid <= 0)
|
|
27
|
+
return false;
|
|
28
|
+
try {
|
|
29
|
+
process.kill(pid, 0);
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (!(error instanceof Error))
|
|
34
|
+
throw error;
|
|
35
|
+
return errorCode(error) !== 'ESRCH';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export class BwSessionStorage {
|
|
39
|
+
host;
|
|
40
|
+
identity;
|
|
41
|
+
homeDir;
|
|
42
|
+
appDataDir;
|
|
43
|
+
processLockDir;
|
|
44
|
+
sessionStatePath;
|
|
45
|
+
constructor(homeDir, host, identity) {
|
|
46
|
+
this.host = host;
|
|
47
|
+
this.identity = identity;
|
|
48
|
+
this.homeDir = homeDir;
|
|
49
|
+
this.appDataDir = join(homeDir, '.bitwarden-cli');
|
|
50
|
+
this.processLockDir = join(this.appDataDir, '.warden-mcp-auth-lock');
|
|
51
|
+
this.sessionStatePath = join(this.appDataDir, '.warden-mcp-session.json');
|
|
52
|
+
}
|
|
53
|
+
baseEnv(extra) {
|
|
54
|
+
return {
|
|
55
|
+
HOME: this.homeDir,
|
|
56
|
+
BITWARDENCLI_APPDATA_DIR: this.appDataDir,
|
|
57
|
+
...(extra ?? {}),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
async currentServerUrl() {
|
|
61
|
+
try {
|
|
62
|
+
const raw = await readFile(join(this.appDataDir, 'data.json'), 'utf8');
|
|
63
|
+
const parsed = JSON.parse(raw);
|
|
64
|
+
const base = parsed.global_environment_environment?.urls?.base;
|
|
65
|
+
if (typeof base === 'string' && base.length > 0)
|
|
66
|
+
return base;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (!(error instanceof Error))
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const { stdout } = await runBw(['status'], {
|
|
74
|
+
env: this.baseEnv(),
|
|
75
|
+
timeoutMs: 30_000,
|
|
76
|
+
});
|
|
77
|
+
const parsed = JSON.parse(stdout);
|
|
78
|
+
return typeof parsed.serverUrl === 'string' ? parsed.serverUrl : null;
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async readSession() {
|
|
85
|
+
try {
|
|
86
|
+
const raw = await readFile(this.sessionStatePath, 'utf8');
|
|
87
|
+
const parsed = JSON.parse(raw);
|
|
88
|
+
if (parsed.version !== 1 ||
|
|
89
|
+
parsed.host !== this.host ||
|
|
90
|
+
parsed.identity !== this.identity ||
|
|
91
|
+
typeof parsed.session !== 'string' ||
|
|
92
|
+
parsed.session.length === 0) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
return parsed.session;
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async writeSession(session) {
|
|
102
|
+
const now = new Date().toISOString();
|
|
103
|
+
const state = {
|
|
104
|
+
version: 1,
|
|
105
|
+
host: this.host,
|
|
106
|
+
identity: this.identity,
|
|
107
|
+
session,
|
|
108
|
+
createdAt: now,
|
|
109
|
+
validatedAt: now,
|
|
110
|
+
};
|
|
111
|
+
await mkdir(this.appDataDir, { recursive: true });
|
|
112
|
+
await writeFile(this.sessionStatePath, JSON.stringify(state), 'utf8');
|
|
113
|
+
}
|
|
114
|
+
async clearSession() {
|
|
115
|
+
await rm(this.sessionStatePath, { force: true }).catch(() => { });
|
|
116
|
+
}
|
|
117
|
+
async clearCliDataFiles() {
|
|
118
|
+
const stateDirs = [
|
|
119
|
+
this.appDataDir,
|
|
120
|
+
join(this.homeDir, '.config', 'Bitwarden CLI'),
|
|
121
|
+
join(this.homeDir, 'Library', 'Application Support', 'Bitwarden CLI'),
|
|
122
|
+
join(this.homeDir, 'AppData', 'Roaming', 'Bitwarden CLI'),
|
|
123
|
+
];
|
|
124
|
+
for (const dir of stateDirs) {
|
|
125
|
+
await rm(join(dir, 'data.json'), { force: true }).catch(() => { });
|
|
126
|
+
await rm(join(dir, 'config.json'), { force: true }).catch(() => { });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async withAuthLock(fn) {
|
|
130
|
+
const startedAt = Date.now();
|
|
131
|
+
await mkdir(this.appDataDir, { recursive: true });
|
|
132
|
+
while (true) {
|
|
133
|
+
try {
|
|
134
|
+
await mkdir(this.processLockDir);
|
|
135
|
+
try {
|
|
136
|
+
await this.writeLockOwner();
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
await rm(this.processLockDir, { recursive: true, force: true });
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
if (errorCode(error) !== 'EEXIST')
|
|
146
|
+
throw error;
|
|
147
|
+
if (await this.reclaimLockIfStale())
|
|
148
|
+
continue;
|
|
149
|
+
if (Date.now() - startedAt >= PROCESS_LOCK_TIMEOUT_MS) {
|
|
150
|
+
throw new Error(`Timed out waiting for process auth lock after ${PROCESS_LOCK_TIMEOUT_MS}ms`);
|
|
151
|
+
}
|
|
152
|
+
await sleep(PROCESS_LOCK_WAIT_MS);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
return await fn();
|
|
157
|
+
}
|
|
158
|
+
finally {
|
|
159
|
+
await rm(this.processLockDir, { recursive: true, force: true }).catch(() => { });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
async writeLockOwner() {
|
|
163
|
+
const owner = {
|
|
164
|
+
pid: process.pid,
|
|
165
|
+
createdAt: new Date().toISOString(),
|
|
166
|
+
};
|
|
167
|
+
await writeFile(join(this.processLockDir, PROCESS_LOCK_OWNER_FILE), JSON.stringify(owner), 'utf8');
|
|
168
|
+
}
|
|
169
|
+
async reclaimLockIfStale() {
|
|
170
|
+
const ownerPath = join(this.processLockDir, PROCESS_LOCK_OWNER_FILE);
|
|
171
|
+
try {
|
|
172
|
+
const parsed = JSON.parse(await readFile(ownerPath, 'utf8'));
|
|
173
|
+
if (isProcessLockOwner(parsed) && !isProcessAlive(parsed.pid)) {
|
|
174
|
+
await rm(this.processLockDir, { recursive: true, force: true });
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
if (!(error instanceof SyntaxError) && errorCode(error) !== 'ENOENT') {
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const lockStats = await stat(this.processLockDir).catch(() => null);
|
|
184
|
+
if (lockStats &&
|
|
185
|
+
Date.now() - lockStats.mtimeMs >= PROCESS_LOCK_TIMEOUT_MS) {
|
|
186
|
+
await rm(this.processLockDir, { recursive: true, force: true });
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
}
|
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.36",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "Vaultwarden/Bitwarden MCP server backed by Bitwarden CLI (bw).",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"@bitwarden/cli": "2026.8.0",
|
|
48
48
|
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
49
49
|
"express": "^5.2.1",
|
|
50
|
-
"zod": "^4.5.
|
|
50
|
+
"zod": "^4.5.4"
|
|
51
51
|
},
|
|
52
52
|
"overrides": {
|
|
53
53
|
"form-data": "4.0.6",
|
|
@@ -56,9 +56,9 @@
|
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@biomejs/biome": "^2.5.11",
|
|
58
58
|
"@types/express": "^5.0.6",
|
|
59
|
-
"@types/node": "^26.4.
|
|
59
|
+
"@types/node": "^26.4.1",
|
|
60
60
|
"playwright": "1.62.1",
|
|
61
|
-
"tsx": "^4.23.
|
|
61
|
+
"tsx": "^4.23.13",
|
|
62
62
|
"typescript": "^7.0.2"
|
|
63
63
|
},
|
|
64
64
|
"publishConfig": {
|
|
@@ -67,6 +67,6 @@
|
|
|
67
67
|
},
|
|
68
68
|
"engines": {
|
|
69
69
|
"node": "~22",
|
|
70
|
-
"npm": "~10"
|
|
70
|
+
"npm": "~10.9.9"
|
|
71
71
|
}
|
|
72
72
|
}
|