digital-ai-testing-mcp 2.3.3 → 2.4.0-beta.1
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/README.md +77 -34
- package/dist/api/access-level.d.ts +14 -0
- package/dist/api/access-level.d.ts.map +1 -1
- package/dist/api/access-level.js +17 -0
- package/dist/api/access-level.js.map +1 -1
- package/dist/api/client.d.ts +2 -2
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +15 -14
- package/dist/api/client.js.map +1 -1
- package/dist/api/update-check.d.ts +29 -0
- package/dist/api/update-check.d.ts.map +1 -0
- package/dist/api/update-check.js +71 -0
- package/dist/api/update-check.js.map +1 -0
- package/dist/cli/clients.d.ts +92 -0
- package/dist/cli/clients.d.ts.map +1 -0
- package/dist/cli/clients.js +372 -0
- package/dist/cli/clients.js.map +1 -0
- package/dist/cli/setup.d.ts +22 -0
- package/dist/cli/setup.d.ts.map +1 -0
- package/dist/cli/setup.js +681 -0
- package/dist/cli/setup.js.map +1 -0
- package/dist/index.js +52 -10
- package/dist/index.js.map +1 -1
- package/dist/tools/meta-tools.d.ts.map +1 -1
- package/dist/tools/meta-tools.js +24 -6
- package/dist/tools/meta-tools.js.map +1 -1
- package/dist/utils/credential-store.d.ts +117 -0
- package/dist/utils/credential-store.d.ts.map +1 -0
- package/dist/utils/credential-store.js +343 -0
- package/dist/utils/credential-store.js.map +1 -0
- package/dist/utils/profile-loader.d.ts +42 -23
- package/dist/utils/profile-loader.d.ts.map +1 -1
- package/dist/utils/profile-loader.js +108 -21
- package/dist/utils/profile-loader.js.map +1 -1
- package/dist/utils/remediation.d.ts +2 -1
- package/dist/utils/remediation.d.ts.map +1 -1
- package/dist/utils/remediation.js +3 -2
- package/dist/utils/remediation.js.map +1 -1
- package/dist/utils/schema.d.ts +1 -1
- package/dist/utils/setup-guidance.d.ts +21 -0
- package/dist/utils/setup-guidance.d.ts.map +1 -0
- package/dist/utils/setup-guidance.js +87 -0
- package/dist/utils/setup-guidance.js.map +1 -0
- package/dist/utils/update-check.d.ts +66 -0
- package/dist/utils/update-check.d.ts.map +1 -0
- package/dist/utils/update-check.js +195 -0
- package/dist/utils/update-check.js.map +1 -0
- package/package.json +5 -1
|
@@ -0,0 +1,681 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `digital-ai-testing-mcp setup …` — manages the local credential store (src/utils/credential-store.ts) from a terminal.
|
|
3
|
+
* src/index.ts runs this instead of the server when the first argument is `setup`.
|
|
4
|
+
*
|
|
5
|
+
* The access key is typed at a hidden prompt, or piped with --key-stdin; it is never a command-line argument (those end
|
|
6
|
+
* up in shell history and process lists) and is never printed. Every key is checked against the API before it is saved
|
|
7
|
+
* unless --skip-check is given. Keys go to the OS keychain by default; --plain-file keeps one in profiles.json.
|
|
8
|
+
*
|
|
9
|
+
* All terminal I/O goes through SetupIO so tests can drive the command without a terminal.
|
|
10
|
+
*/
|
|
11
|
+
import { parseArgs } from 'node:util';
|
|
12
|
+
import { createInterface } from 'node:readline';
|
|
13
|
+
import { verifyCredential } from '../api/access-level.js';
|
|
14
|
+
import { isKnownNotCloudAdmin } from '../utils/access-level.js';
|
|
15
|
+
import { deleteKeyFromKeychain, deleteStoreFiles, findRawProfileName, isStoreEnabled, KEYCHAIN_SERVICE, PROFILE_NAME_RE, readStore, readStoreFileRaw, restoreStoreBackup, saveKeyToKeychain, storePath, validUrl, writeStore, } from '../utils/credential-store.js';
|
|
16
|
+
import { existsSync } from 'node:fs';
|
|
17
|
+
import { buildLaunchEntry, claudeCodeCommand, clientStatus, defaultClientContext, knownClients, manualClients, pasteSnippet, removeClientEntry, writeClientEntry, SERVER_ENTRY_NAME, } from './clients.js';
|
|
18
|
+
const USAGE = `Manage the Digital.ai Testing connection profiles this MCP server can use.
|
|
19
|
+
|
|
20
|
+
Usage: digital-ai-testing-mcp setup [command] [options]
|
|
21
|
+
|
|
22
|
+
Commands:
|
|
23
|
+
(none) Add a profile, step by step (in a terminal)
|
|
24
|
+
add [name] Add a profile, or replace one with the same name
|
|
25
|
+
list Show the profiles and where their keys are kept
|
|
26
|
+
default <name> Make <name> the profile the server starts on
|
|
27
|
+
remove <name> Delete a profile (and its key in the OS keychain)
|
|
28
|
+
test [name] Check one profile, or all, against the API
|
|
29
|
+
path Show where the profiles file is
|
|
30
|
+
repair Restore the profiles file from its backup
|
|
31
|
+
reset Delete the profiles file, its backups and the keys it keeps in the OS keychain
|
|
32
|
+
clients Show which AI clients are set up to start this server
|
|
33
|
+
clients add [client…] Add the server to AI clients (every one installed, or the ones named)
|
|
34
|
+
clients remove [client…] Remove it from AI clients
|
|
35
|
+
help Show this help
|
|
36
|
+
|
|
37
|
+
Options for add:
|
|
38
|
+
--url <url> Your Digital.ai Testing URL, e.g. https://yourcompany.experitest.com
|
|
39
|
+
--key-stdin Read the access key from stdin (for scripts) instead of a hidden prompt
|
|
40
|
+
--plain-file Keep the key in the profiles file instead of the OS keychain
|
|
41
|
+
--default Make it the profile the server starts on
|
|
42
|
+
--skip-check Save without checking the key against the API
|
|
43
|
+
Options for every command:
|
|
44
|
+
--yes, -y Answer yes to confirmations (replace, remove, repair, reset)
|
|
45
|
+
|
|
46
|
+
The access key is in the Digital.ai Testing portal: click your name (top right) → Access Key.
|
|
47
|
+
Environment variables in an AI client's configuration (DIGITAL_AI_BASE_URL, DIGITAL_AI_ACCESS_KEY, DAI_PROFILE_*)
|
|
48
|
+
always take precedence over these profiles.`;
|
|
49
|
+
const RESTART = 'Restart your AI client, or reconnect the digital-ai-testing MCP server, to use the change.';
|
|
50
|
+
class SetupError extends Error {
|
|
51
|
+
code;
|
|
52
|
+
constructor(message, code = 1) {
|
|
53
|
+
super(message);
|
|
54
|
+
this.code = code;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// ─── Default terminal I/O ─────────────────────────────────────────────────────
|
|
58
|
+
function ask(question) {
|
|
59
|
+
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
60
|
+
return new Promise((resolve) => {
|
|
61
|
+
rl.question(question, (answer) => {
|
|
62
|
+
rl.close();
|
|
63
|
+
resolve(answer.trim());
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
function askSecret(question) {
|
|
68
|
+
const stdin = process.stdin;
|
|
69
|
+
if (!stdin.isTTY)
|
|
70
|
+
return ask(question);
|
|
71
|
+
process.stderr.write(question);
|
|
72
|
+
return new Promise((resolve) => {
|
|
73
|
+
let value = '';
|
|
74
|
+
const finish = () => {
|
|
75
|
+
stdin.removeListener('data', onData);
|
|
76
|
+
stdin.setRawMode(false);
|
|
77
|
+
stdin.pause();
|
|
78
|
+
process.stderr.write('\n');
|
|
79
|
+
};
|
|
80
|
+
const onData = (chunk) => {
|
|
81
|
+
for (const ch of chunk.toString('utf8')) {
|
|
82
|
+
if (ch === '\r' || ch === '\n') {
|
|
83
|
+
finish();
|
|
84
|
+
return resolve(value.trim());
|
|
85
|
+
}
|
|
86
|
+
if (ch === '\u0003') {
|
|
87
|
+
finish();
|
|
88
|
+
process.exit(130); // Ctrl+C
|
|
89
|
+
}
|
|
90
|
+
if (ch === '\u007f' || ch === '\b')
|
|
91
|
+
value = value.slice(0, -1);
|
|
92
|
+
else if (ch >= ' ')
|
|
93
|
+
value += ch;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
stdin.setRawMode(true);
|
|
97
|
+
stdin.resume();
|
|
98
|
+
stdin.on('data', onData);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
async function readStdin() {
|
|
102
|
+
const chunks = [];
|
|
103
|
+
for await (const c of process.stdin)
|
|
104
|
+
chunks.push(Buffer.isBuffer(c) ? c : Buffer.from(String(c)));
|
|
105
|
+
return Buffer.concat(chunks).toString('utf8').trim();
|
|
106
|
+
}
|
|
107
|
+
export function terminalIO() {
|
|
108
|
+
return {
|
|
109
|
+
out: (t) => void process.stdout.write(t + '\n'),
|
|
110
|
+
err: (t) => void process.stderr.write(t + '\n'),
|
|
111
|
+
ask,
|
|
112
|
+
askSecret,
|
|
113
|
+
readStdin,
|
|
114
|
+
interactive: Boolean(process.stdin.isTTY),
|
|
115
|
+
verify: verifyCredential,
|
|
116
|
+
clients: defaultClientContext(),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
120
|
+
async function confirm(io, yes, question, nonInteractiveHint) {
|
|
121
|
+
if (yes)
|
|
122
|
+
return true;
|
|
123
|
+
if (!io.interactive)
|
|
124
|
+
throw new SetupError(`${question.replace(/\s*\[y\/N\]\s*$/, '')} — not in a terminal, so nothing was changed. ${nonInteractiveHint}`, 2);
|
|
125
|
+
return /^y(es)?$/i.test(await io.ask(question));
|
|
126
|
+
}
|
|
127
|
+
function loadForEdit() {
|
|
128
|
+
const raw = readStoreFileRaw();
|
|
129
|
+
if (!raw.data) {
|
|
130
|
+
throw new SetupError(`The profiles file ${storePath()} cannot be changed: ${raw.error}.\n` +
|
|
131
|
+
`Fix it in a text editor, or run "digital-ai-testing-mcp setup repair" to restore its backup.`);
|
|
132
|
+
}
|
|
133
|
+
return raw.data;
|
|
134
|
+
}
|
|
135
|
+
function normalizeName(name) {
|
|
136
|
+
const n = name.trim().toLowerCase();
|
|
137
|
+
if (!PROFILE_NAME_RE.test(n))
|
|
138
|
+
throw new SetupError(`"${name}" is not a valid profile name: use letters, digits, "_" and "-" (up to 64).`, 2);
|
|
139
|
+
return n;
|
|
140
|
+
}
|
|
141
|
+
function suggestName(check) {
|
|
142
|
+
if (check?.ok && check.info.level === 'cloud-admin')
|
|
143
|
+
return 'admin';
|
|
144
|
+
const project = check?.ok ? check.info.projectName : undefined;
|
|
145
|
+
const slug = project?.toLowerCase().replace(/[^a-z0-9_-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 64);
|
|
146
|
+
return slug && PROFILE_NAME_RE.test(slug) ? slug : 'main';
|
|
147
|
+
}
|
|
148
|
+
const URL_HINT = 'Enter the address you open the Digital.ai Testing portal with, e.g. https://yourcompany.experitest.com';
|
|
149
|
+
/** What a person typed as the URL → a normalized http(s) URL, or null. "host.com" gets https://; spaces never pass. */
|
|
150
|
+
function parseUrlInput(typed) {
|
|
151
|
+
const t = typed.trim();
|
|
152
|
+
if (!t || /\s/.test(t))
|
|
153
|
+
return null;
|
|
154
|
+
return validUrl(/^[a-z][a-z0-9+.-]*:\/\//i.test(t) ? t : `https://${t}`);
|
|
155
|
+
}
|
|
156
|
+
/** A person-facing role line ("Project Admin of project "X""), not the agent-facing describeLevel wording. */
|
|
157
|
+
function roleLine(info) {
|
|
158
|
+
const project = info.projectName ? ` of project "${info.projectName}"` : '';
|
|
159
|
+
switch (info.level) {
|
|
160
|
+
case 'cloud-admin':
|
|
161
|
+
return 'Cloud Admin';
|
|
162
|
+
case 'project-admin':
|
|
163
|
+
return `Project Admin${project}`;
|
|
164
|
+
case 'project-user':
|
|
165
|
+
return `Project User${project}`;
|
|
166
|
+
case 'project-limited':
|
|
167
|
+
return `project-level access${project}`;
|
|
168
|
+
default:
|
|
169
|
+
return 'key accepted (access level not determined)';
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Profile names switch_environment treats as a Cloud Admin request (the exact name wins over the alias). */
|
|
173
|
+
const ADMIN_ALIAS_NAMES = ['admin', 'cloud', 'cloudadmin', 'cloud-admin', 'cloud_admin'];
|
|
174
|
+
function describeCheck(check, url) {
|
|
175
|
+
if (check.ok)
|
|
176
|
+
return roleLine(check.info);
|
|
177
|
+
switch (check.problem) {
|
|
178
|
+
case 'rejected':
|
|
179
|
+
return `the access key was refused (HTTP ${check.status}). Copy it again from the portal: your name (top right) → Access Key`;
|
|
180
|
+
case 'unreachable':
|
|
181
|
+
return `${url} could not be reached. Check the URL and your network or VPN`;
|
|
182
|
+
case 'not-api':
|
|
183
|
+
return `${url} answered${check.status ? ` (HTTP ${check.status})` : ''}, but not as the Digital.ai Testing API. Check the URL — it is the address you open the portal with`;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function storeOffNote(io) {
|
|
187
|
+
if (!isStoreEnabled())
|
|
188
|
+
io.err('Note: MCP_CREDENTIAL_STORE is set to off in this shell. A server started with that setting ignores these profiles.');
|
|
189
|
+
}
|
|
190
|
+
async function cmdAdd(io, nameArg, flags) {
|
|
191
|
+
const data = loadForEdit();
|
|
192
|
+
storeOffNote(io);
|
|
193
|
+
// URL
|
|
194
|
+
let url;
|
|
195
|
+
if (flags.url) {
|
|
196
|
+
url = parseUrlInput(flags.url);
|
|
197
|
+
if (!url)
|
|
198
|
+
throw new SetupError(`"${flags.url}" is not a URL. ${URL_HINT}`, 2);
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
if (!io.interactive)
|
|
202
|
+
throw new SetupError('No URL: pass --url when not running in a terminal.', 2);
|
|
203
|
+
// In a terminal a typo is asked again (a few times) instead of ending the command.
|
|
204
|
+
url = null;
|
|
205
|
+
for (let attempt = 1; !url; attempt++) {
|
|
206
|
+
const typed = await io.ask('Digital.ai Testing URL (e.g. https://yourcompany.experitest.com): ');
|
|
207
|
+
url = parseUrlInput(typed);
|
|
208
|
+
if (url)
|
|
209
|
+
break;
|
|
210
|
+
if (attempt === 3)
|
|
211
|
+
throw new SetupError(`"${typed}" is not a URL. Nothing was saved. ${URL_HINT}`, 2);
|
|
212
|
+
io.err(`"${typed}" is not a URL. ${URL_HINT}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
// Key
|
|
216
|
+
let key;
|
|
217
|
+
if (flags['key-stdin'])
|
|
218
|
+
key = await io.readStdin();
|
|
219
|
+
else if (io.interactive)
|
|
220
|
+
key = await io.askSecret('Access key (input hidden): ');
|
|
221
|
+
else
|
|
222
|
+
throw new SetupError('No access key: pass --key-stdin and pipe the key in when not running in a terminal.', 2);
|
|
223
|
+
if (!key)
|
|
224
|
+
throw new SetupError('No access key was entered.', 2);
|
|
225
|
+
// Check
|
|
226
|
+
let check;
|
|
227
|
+
if (!flags['skip-check']) {
|
|
228
|
+
io.out(`Checking the key with ${url} …`);
|
|
229
|
+
check = await io.verify(url, key);
|
|
230
|
+
if (!check.ok) {
|
|
231
|
+
throw new SetupError(`Not saved: ${describeCheck(check, url)}.` + (check.problem === 'unreachable' ? ' To save it anyway, add --skip-check.' : ''));
|
|
232
|
+
}
|
|
233
|
+
io.out(`OK — ${describeCheck(check, url)}.`);
|
|
234
|
+
}
|
|
235
|
+
// Name
|
|
236
|
+
let name = nameArg;
|
|
237
|
+
if (!name) {
|
|
238
|
+
const suggested = suggestName(check);
|
|
239
|
+
name = io.interactive ? (await io.ask(`Profile name [${suggested}]: `)) || suggested : suggested;
|
|
240
|
+
}
|
|
241
|
+
name = normalizeName(name);
|
|
242
|
+
const existing = findRawProfileName(data, name);
|
|
243
|
+
if (existing && !(await confirm(io, !!flags.yes, `A profile named "${name}" exists. Replace it? [y/N] `, 'Add --yes to replace it.'))) {
|
|
244
|
+
throw new SetupError('Nothing was changed.', 1);
|
|
245
|
+
}
|
|
246
|
+
const previous = existing ? data.profiles[existing] : undefined;
|
|
247
|
+
if (existing)
|
|
248
|
+
delete data.profiles[existing];
|
|
249
|
+
// Key location
|
|
250
|
+
let keyLocation = flags['plain-file'] ? 'file' : 'os';
|
|
251
|
+
if (keyLocation === 'os') {
|
|
252
|
+
const saved = saveKeyToKeychain(name, key);
|
|
253
|
+
if (!saved.ok) {
|
|
254
|
+
io.err(`The OS keychain could not store the key: ${saved.error}.`);
|
|
255
|
+
const fallback = await confirm(io, false, 'Keep the key in the profiles file instead (readable by your user account only)? [y/N] ', 'Add --plain-file to keep the key in the profiles file.');
|
|
256
|
+
if (!fallback)
|
|
257
|
+
throw new SetupError('Nothing was saved.', 1);
|
|
258
|
+
keyLocation = 'file';
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
data.profiles[name] = keyLocation === 'os' ? { url, keyStore: 'os' } : { url, key };
|
|
262
|
+
// Default
|
|
263
|
+
const currentDefault = typeof data.default === 'string' ? data.default.trim().toLowerCase() : undefined;
|
|
264
|
+
const defaultExists = !!currentDefault && !!findRawProfileName(data, currentDefault);
|
|
265
|
+
let makeDefault = !!flags.default || !defaultExists || currentDefault === name;
|
|
266
|
+
if (!makeDefault && io.interactive && !flags.yes) {
|
|
267
|
+
makeDefault = /^y(es)?$/i.test(await io.ask(`Make "${name}" the profile the server starts on (now "${currentDefault}")? [y/N] `));
|
|
268
|
+
}
|
|
269
|
+
if (makeDefault)
|
|
270
|
+
data.default = name;
|
|
271
|
+
const path = writeStore(data);
|
|
272
|
+
// Moved from the keychain to the file: remove the old keychain copy — only now that the file holds the key.
|
|
273
|
+
if (keyLocation === 'file' && previous?.keyStore === 'os')
|
|
274
|
+
deleteKeyFromKeychain(name);
|
|
275
|
+
// Read back through the same code the server uses — proves the key is retrievable where it was put.
|
|
276
|
+
const back = readStore();
|
|
277
|
+
if (!back.profiles[name]) {
|
|
278
|
+
const why = back.skipped.find((s) => s.name === name)?.reason ?? back.error ?? 'unknown reason';
|
|
279
|
+
throw new SetupError(`The profile was written to ${path}, but reading it back failed: ${why}.`);
|
|
280
|
+
}
|
|
281
|
+
io.out(`Saved profile "${name}" (${url}) — key in ${keyLocation === 'os' ? `the OS keychain (service "${KEYCHAIN_SERVICE}", account "${name}")` : 'the profiles file'}.`);
|
|
282
|
+
io.out(makeDefault ? `The server starts on "${name}".` : `The server still starts on "${currentDefault}"; switch with switch_environment("${name}").`);
|
|
283
|
+
// A Cloud-Admin-sounding name on a project key: "switch to admin" would land here, not on a Cloud Admin profile.
|
|
284
|
+
if (check?.ok && isKnownNotCloudAdmin(check.info.level) && ADMIN_ALIAS_NAMES.includes(name)) {
|
|
285
|
+
io.out(`Note: "${name}" holds a ${roleLine(check.info).split(' of ')[0]} key. switch_environment("${name}") always picks this ` +
|
|
286
|
+
'profile; ask for "cloud admin" to reach a Cloud Admin profile.');
|
|
287
|
+
}
|
|
288
|
+
io.out(`Profiles file: ${path}`);
|
|
289
|
+
io.out('If your AI client\'s configuration sets DIGITAL_AI_BASE_URL / DIGITAL_AI_ACCESS_KEY, those still win — remove them to use these profiles.');
|
|
290
|
+
if (io.interactive && !flags.yes)
|
|
291
|
+
await offerClients(io);
|
|
292
|
+
else
|
|
293
|
+
io.out('To add the server to your AI clients: digital-ai-testing-mcp setup clients add');
|
|
294
|
+
io.out(RESTART);
|
|
295
|
+
}
|
|
296
|
+
function cmdList(io) {
|
|
297
|
+
const path = storePath();
|
|
298
|
+
storeOffNote(io);
|
|
299
|
+
const raw = readStoreFileRaw();
|
|
300
|
+
if (!raw.exists) {
|
|
301
|
+
io.out(`No profiles yet (${path} does not exist). Add one with: digital-ai-testing-mcp setup`);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
io.out(`Profiles file: ${path}`);
|
|
305
|
+
if (!raw.data) {
|
|
306
|
+
io.out(`It cannot be used: ${raw.error}. Fix it in a text editor, or run "digital-ai-testing-mcp setup repair".`);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const store = readStore();
|
|
310
|
+
const names = Object.keys(raw.data.profiles);
|
|
311
|
+
if (names.length === 0)
|
|
312
|
+
io.out('No profiles in it. Add one with: digital-ai-testing-mcp setup');
|
|
313
|
+
for (const rawName of names) {
|
|
314
|
+
const n = rawName.trim().toLowerCase();
|
|
315
|
+
const ok = store.profiles[n];
|
|
316
|
+
const isDefault = store.defaultProfile === n;
|
|
317
|
+
const where = ok ? (ok.keyStore === 'os' ? 'key in the OS keychain' : 'key in this file') : '';
|
|
318
|
+
const problem = store.skipped.find((s) => s.name === rawName || s.name === n)?.reason;
|
|
319
|
+
io.out(` ${rawName}${isDefault ? ' (default)' : ''}: ${ok ? `${ok.url} — ${where}` : `NOT USABLE — ${problem ?? 'unknown reason'}`}`);
|
|
320
|
+
}
|
|
321
|
+
const defaultProblem = store.skipped.find((s) => s.name === '(default)');
|
|
322
|
+
if (defaultProblem)
|
|
323
|
+
io.out(` Default: ${defaultProblem.reason}. Fix with: digital-ai-testing-mcp setup default <name>`);
|
|
324
|
+
}
|
|
325
|
+
function cmdDefault(io, nameArg) {
|
|
326
|
+
if (!nameArg)
|
|
327
|
+
throw new SetupError('Which profile? Usage: digital-ai-testing-mcp setup default <name>', 2);
|
|
328
|
+
const data = loadForEdit();
|
|
329
|
+
const name = normalizeName(nameArg);
|
|
330
|
+
if (!findRawProfileName(data, name))
|
|
331
|
+
throw new SetupError(`There is no profile named "${name}". See: digital-ai-testing-mcp setup list`);
|
|
332
|
+
data.default = name;
|
|
333
|
+
writeStore(data);
|
|
334
|
+
const usable = readStore().profiles[name];
|
|
335
|
+
io.out(`The server now starts on "${name}".`);
|
|
336
|
+
if (!usable)
|
|
337
|
+
io.err(`Warning: "${name}" is not usable at the moment — see: digital-ai-testing-mcp setup list`);
|
|
338
|
+
io.out(RESTART);
|
|
339
|
+
}
|
|
340
|
+
async function cmdRemove(io, nameArg, flags) {
|
|
341
|
+
if (!nameArg)
|
|
342
|
+
throw new SetupError('Which profile? Usage: digital-ai-testing-mcp setup remove <name>', 2);
|
|
343
|
+
const data = loadForEdit();
|
|
344
|
+
const name = normalizeName(nameArg);
|
|
345
|
+
const existing = findRawProfileName(data, name);
|
|
346
|
+
if (!existing)
|
|
347
|
+
throw new SetupError(`There is no profile named "${name}". See: digital-ai-testing-mcp setup list`);
|
|
348
|
+
if (!(await confirm(io, !!flags.yes, `Remove profile "${name}"? [y/N] `, 'Add --yes to remove it.')))
|
|
349
|
+
throw new SetupError('Nothing was changed.', 1);
|
|
350
|
+
const entry = data.profiles[existing];
|
|
351
|
+
delete data.profiles[existing];
|
|
352
|
+
const wasDefault = typeof data.default === 'string' && data.default.trim().toLowerCase() === name;
|
|
353
|
+
if (wasDefault)
|
|
354
|
+
delete data.default;
|
|
355
|
+
writeStore(data);
|
|
356
|
+
io.out(`Removed profile "${name}".`);
|
|
357
|
+
if (entry.keyStore === 'os') {
|
|
358
|
+
const del = deleteKeyFromKeychain(name);
|
|
359
|
+
if (!del.ok)
|
|
360
|
+
io.err(`Its key could not be removed from the OS keychain: ${del.error}. Remove "${KEYCHAIN_SERVICE}" / "${name}" there by hand.`);
|
|
361
|
+
else if (del.existed)
|
|
362
|
+
io.out('Its key was removed from the OS keychain.');
|
|
363
|
+
}
|
|
364
|
+
if (wasDefault) {
|
|
365
|
+
const left = Object.keys(data.profiles);
|
|
366
|
+
io.out(left.length === 1
|
|
367
|
+
? `It was the default; the server now starts on the only profile left, "${left[0]}".`
|
|
368
|
+
: left.length === 0
|
|
369
|
+
? 'It was the last profile.'
|
|
370
|
+
: `It was the default. Choose another: digital-ai-testing-mcp setup default <name> (${left.join(', ')})`);
|
|
371
|
+
}
|
|
372
|
+
io.out(RESTART);
|
|
373
|
+
}
|
|
374
|
+
async function cmdTest(io, nameArg) {
|
|
375
|
+
const store = readStore();
|
|
376
|
+
if (store.error)
|
|
377
|
+
throw new SetupError(`The profiles file ${store.path} cannot be used: ${store.error}.`);
|
|
378
|
+
const wanted = nameArg ? normalizeName(nameArg) : undefined;
|
|
379
|
+
const names = wanted ? [wanted] : Object.keys(store.profiles);
|
|
380
|
+
if (names.length === 0 && store.skipped.length === 0) {
|
|
381
|
+
io.out('No profiles to check. Add one with: digital-ai-testing-mcp setup');
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
let failed = 0;
|
|
385
|
+
for (const n of names) {
|
|
386
|
+
const p = store.profiles[n];
|
|
387
|
+
if (!p) {
|
|
388
|
+
const reason = store.skipped.find((s) => s.name.toLowerCase() === n)?.reason;
|
|
389
|
+
if (!reason && wanted)
|
|
390
|
+
throw new SetupError(`There is no profile named "${n}". See: digital-ai-testing-mcp setup list`);
|
|
391
|
+
io.out(` ${n}: NOT USABLE — ${reason}`);
|
|
392
|
+
failed++;
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
const check = await io.verify(p.url, p.key);
|
|
396
|
+
io.out(` ${n}: ${check.ok ? 'OK' : 'FAILED'} — ${describeCheck(check, p.url)}`);
|
|
397
|
+
if (!check.ok)
|
|
398
|
+
failed++;
|
|
399
|
+
}
|
|
400
|
+
if (!wanted) {
|
|
401
|
+
for (const s of store.skipped.filter((s) => s.name !== '(default)')) {
|
|
402
|
+
io.out(` ${s.name}: NOT USABLE — ${s.reason}`);
|
|
403
|
+
failed++;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
if (failed > 0)
|
|
407
|
+
throw new SetupError(`${failed} profile${failed === 1 ? '' : 's'} failed.`, 1);
|
|
408
|
+
}
|
|
409
|
+
function cmdPath(io) {
|
|
410
|
+
const path = storePath();
|
|
411
|
+
io.out(path);
|
|
412
|
+
if (!existsSync(path))
|
|
413
|
+
io.err('(The file does not exist yet.)');
|
|
414
|
+
}
|
|
415
|
+
async function cmdRepair(io, flags) {
|
|
416
|
+
const path = storePath();
|
|
417
|
+
if (!existsSync(`${path}.bak`))
|
|
418
|
+
throw new SetupError(`There is no backup to restore (${path}.bak).`);
|
|
419
|
+
if (!(await confirm(io, !!flags.yes, `Replace ${path} with its backup? [y/N] `, 'Add --yes to restore it.')))
|
|
420
|
+
throw new SetupError('Nothing was changed.', 1);
|
|
421
|
+
const r = restoreStoreBackup();
|
|
422
|
+
if (!r.ok)
|
|
423
|
+
throw new SetupError(`Not restored: ${r.error}.`);
|
|
424
|
+
io.out(`Restored the profiles file from its backup.${r.keptAs ? ` The replaced file is kept as ${r.keptAs}.` : ''}`);
|
|
425
|
+
io.out('Keys in the OS keychain are not affected. Check the result with: digital-ai-testing-mcp setup list');
|
|
426
|
+
io.out(RESTART);
|
|
427
|
+
}
|
|
428
|
+
async function cmdReset(io, flags) {
|
|
429
|
+
const raw = readStoreFileRaw();
|
|
430
|
+
const path = storePath();
|
|
431
|
+
if (!raw.exists && !existsSync(`${path}.bak`)) {
|
|
432
|
+
io.out('There is nothing to reset.');
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
if (!(await confirm(io, !!flags.yes, 'Delete every profile, the profiles file and its backups, and the keys it keeps in the OS keychain? [y/N] ', 'Add --yes to reset.'))) {
|
|
436
|
+
throw new SetupError('Nothing was changed.', 1);
|
|
437
|
+
}
|
|
438
|
+
const keychainNames = raw.data
|
|
439
|
+
? Object.entries(raw.data.profiles).filter(([, p]) => p?.keyStore === 'os').map(([n]) => n.trim().toLowerCase())
|
|
440
|
+
: [];
|
|
441
|
+
for (const n of keychainNames) {
|
|
442
|
+
const del = deleteKeyFromKeychain(n);
|
|
443
|
+
if (!del.ok)
|
|
444
|
+
io.err(`The key of "${n}" could not be removed from the OS keychain: ${del.error}. Remove "${KEYCHAIN_SERVICE}" / "${n}" there by hand.`);
|
|
445
|
+
}
|
|
446
|
+
const removed = deleteStoreFiles();
|
|
447
|
+
io.out(`Removed ${removed.length} file${removed.length === 1 ? '' : 's'}${keychainNames.length ? ` and ${keychainNames.length} OS keychain entr${keychainNames.length === 1 ? 'y' : 'ies'}` : ''}.`);
|
|
448
|
+
if (!raw.data && raw.exists)
|
|
449
|
+
io.err('The profiles file could not be read, so keys it kept in the OS keychain (if any) were not removed.');
|
|
450
|
+
io.out(RESTART);
|
|
451
|
+
}
|
|
452
|
+
// ─── AI clients ───────────────────────────────────────────────────────────────
|
|
453
|
+
const RESTART_CLIENTS = 'Restart the AI clients you changed (or reconnect the server in them) to use the change.';
|
|
454
|
+
function describeEntry(io, entry) {
|
|
455
|
+
io.out(`Entry "${SERVER_ENTRY_NAME}" (no access key in it — the server reads the credential store):`);
|
|
456
|
+
io.out(` ${entry.command} ${entry.args.join(' ')}`);
|
|
457
|
+
for (const n of entry.notes)
|
|
458
|
+
io.out(` Note: ${n}`);
|
|
459
|
+
}
|
|
460
|
+
function statusLine(t, entry) {
|
|
461
|
+
if (!t.detected)
|
|
462
|
+
return 'not installed';
|
|
463
|
+
const s = clientStatus(t, entry);
|
|
464
|
+
const others = s.otherNames.length ? ` It also starts this server as "${s.otherNames.join('", "')}" — remove that entry to avoid two copies.` : '';
|
|
465
|
+
if (s.unreadable)
|
|
466
|
+
return `not changed by setup — ${s.unreadable}. "setup clients add ${t.id}" prints the entry to paste.${others}`;
|
|
467
|
+
if (!s.hasEntry)
|
|
468
|
+
return `installed, not set up${others}`;
|
|
469
|
+
if (s.entryHasKeys)
|
|
470
|
+
return `set up with its own access key in the entry (that key wins over the credential store)${others}`;
|
|
471
|
+
return (s.upToDate ? 'set up' : 'set up with a different launch command ("setup clients add" updates it)') + others;
|
|
472
|
+
}
|
|
473
|
+
function cmdClientsList(io, ctx) {
|
|
474
|
+
const entry = buildLaunchEntry(ctx);
|
|
475
|
+
describeEntry(io, entry);
|
|
476
|
+
io.out('');
|
|
477
|
+
for (const t of knownClients(ctx))
|
|
478
|
+
io.out(` ${t.id.padEnd(22)} ${t.label}: ${statusLine(t, entry)}`);
|
|
479
|
+
io.out('');
|
|
480
|
+
io.out('Set up by hand:');
|
|
481
|
+
for (const m of manualClients())
|
|
482
|
+
io.out(` ${m.id.padEnd(22)} ${m.label}: ${m.how}`);
|
|
483
|
+
io.out('');
|
|
484
|
+
io.out('Claude Code — run once in a terminal:');
|
|
485
|
+
io.out(` ${claudeCodeCommand(entry, ctx.platform)}`);
|
|
486
|
+
}
|
|
487
|
+
/** The named clients, or every installed one when none is named. */
|
|
488
|
+
function pickTargets(ctx, ids) {
|
|
489
|
+
const known = knownClients(ctx);
|
|
490
|
+
if (ids.length === 0)
|
|
491
|
+
return known.filter((t) => t.detected);
|
|
492
|
+
return ids.map((id) => {
|
|
493
|
+
const t = known.find((k) => k.id === id.toLowerCase());
|
|
494
|
+
if (t)
|
|
495
|
+
return t;
|
|
496
|
+
const manual = manualClients().find((m) => m.id === id.toLowerCase());
|
|
497
|
+
if (manual)
|
|
498
|
+
throw new SetupError(`${manual.label}: ${manual.how} See: digital-ai-testing-mcp setup clients`, 2);
|
|
499
|
+
throw new SetupError(`Unknown client "${id}". Known: ${known.map((k) => k.id).join(', ')}.`, 2);
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
/** Adds or updates the entry in each target. Returns how many were left unchanged and need attention. */
|
|
503
|
+
async function addToClients(io, ctx, targets, yes) {
|
|
504
|
+
const entry = buildLaunchEntry(ctx);
|
|
505
|
+
let attention = 0;
|
|
506
|
+
for (const t of targets) {
|
|
507
|
+
const s = clientStatus(t, entry);
|
|
508
|
+
if (s.unreadable) {
|
|
509
|
+
io.out(`${t.label}: not changed — ${s.unreadable}. Add this to ${t.file} by hand:`);
|
|
510
|
+
io.out(pasteSnippet(t.format, entry));
|
|
511
|
+
attention++;
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
if (s.upToDate) {
|
|
515
|
+
io.out(`${t.label}: already set up.`);
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
if (s.hasEntry) {
|
|
519
|
+
const question = s.entryHasKeys
|
|
520
|
+
? `${t.label} already has a "${SERVER_ENTRY_NAME}" entry with its own access key. Replace it with one that uses the credential store? [y/N] `
|
|
521
|
+
: `${t.label} already has a "${SERVER_ENTRY_NAME}" entry with a different launch command. Replace it? [y/N] `;
|
|
522
|
+
const replace = yes || (io.interactive && /^y(es)?$/i.test(await io.ask(question)));
|
|
523
|
+
if (!replace) {
|
|
524
|
+
io.out(`${t.label}: not changed${io.interactive ? '' : ' — add --yes to replace its existing entry'}.`);
|
|
525
|
+
attention++;
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
const r = writeClientEntry(t, entry);
|
|
530
|
+
if (!r.ok) {
|
|
531
|
+
io.err(`${t.label}: not changed — ${r.error}`);
|
|
532
|
+
attention++;
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
535
|
+
io.out(`${t.label}: ${s.hasEntry ? 'updated' : 'added'} (${t.file}${r.backup ? `; previous version kept as ${r.backup}` : ''}).`);
|
|
536
|
+
for (const n of s.otherNames)
|
|
537
|
+
io.out(` It also starts this server as "${n}" — remove that entry in ${t.file} to avoid two copies.`);
|
|
538
|
+
}
|
|
539
|
+
return attention;
|
|
540
|
+
}
|
|
541
|
+
/** After a guided `setup`: offer the installed clients that are not set up yet. */
|
|
542
|
+
async function offerClients(io) {
|
|
543
|
+
const ctx = io.clients ?? defaultClientContext();
|
|
544
|
+
const entry = buildLaunchEntry(ctx);
|
|
545
|
+
const pending = knownClients(ctx).filter((t) => t.detected && !clientStatus(t, entry).upToDate);
|
|
546
|
+
if (pending.length === 0)
|
|
547
|
+
return;
|
|
548
|
+
const answer = await io.ask(`Add the server to your AI clients too (${pending.map((t) => t.label).join(', ')})? [Y/n] `);
|
|
549
|
+
if (/^n(o)?$/i.test(answer)) {
|
|
550
|
+
io.out('Later: digital-ai-testing-mcp setup clients add');
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
await addToClients(io, ctx, pending, false);
|
|
554
|
+
io.out(`Claude Code: run this once in a terminal: ${claudeCodeCommand(entry, ctx.platform)}`);
|
|
555
|
+
}
|
|
556
|
+
async function cmdClients(io, sub, ids, flags) {
|
|
557
|
+
const ctx = io.clients ?? defaultClientContext();
|
|
558
|
+
if (sub === undefined || sub === 'list') {
|
|
559
|
+
if (ids.length)
|
|
560
|
+
throw new SetupError(`Unexpected argument: ${ids[0]}`, 2);
|
|
561
|
+
cmdClientsList(io, ctx);
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
if (sub === 'add') {
|
|
565
|
+
const targets = pickTargets(ctx, ids);
|
|
566
|
+
if (targets.length === 0) {
|
|
567
|
+
throw new SetupError('No AI clients were found on this machine. Name one: digital-ai-testing-mcp setup clients add <client> (list: setup clients)', 1);
|
|
568
|
+
}
|
|
569
|
+
describeEntry(io, buildLaunchEntry(ctx));
|
|
570
|
+
const attention = await addToClients(io, ctx, targets, !!flags.yes);
|
|
571
|
+
io.out(RESTART_CLIENTS);
|
|
572
|
+
if (attention > 0)
|
|
573
|
+
throw new SetupError(`${attention} client${attention === 1 ? ' was' : 's were'} not changed (see above).`, 1);
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
if (sub === 'remove') {
|
|
577
|
+
const entry = buildLaunchEntry(ctx);
|
|
578
|
+
const targets = pickTargets(ctx, ids).filter((t) => clientStatus(t, entry).hasEntry);
|
|
579
|
+
if (targets.length === 0) {
|
|
580
|
+
io.out(`No AI client has a "${SERVER_ENTRY_NAME}" entry to remove.`);
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
const names = targets.map((t) => t.label).join(', ');
|
|
584
|
+
if (!(await confirm(io, !!flags.yes, `Remove the "${SERVER_ENTRY_NAME}" entry from ${names}? [y/N] `, 'Add --yes to remove it.'))) {
|
|
585
|
+
throw new SetupError('Nothing was changed.', 1);
|
|
586
|
+
}
|
|
587
|
+
for (const t of targets) {
|
|
588
|
+
const r = removeClientEntry(t);
|
|
589
|
+
if (!r.ok)
|
|
590
|
+
io.err(`${t.label}: not changed — ${r.error}`);
|
|
591
|
+
else
|
|
592
|
+
io.out(`${t.label}: removed${r.backup ? ` (previous version kept as ${r.backup})` : ''}.`);
|
|
593
|
+
}
|
|
594
|
+
io.out(RESTART_CLIENTS);
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
throw new SetupError(`Unknown clients command: ${sub}. Use: setup clients | setup clients add [client…] | setup clients remove [client…]`, 2);
|
|
598
|
+
}
|
|
599
|
+
// ─── Entry point ──────────────────────────────────────────────────────────────
|
|
600
|
+
/** Runs `setup` with the arguments after it. Returns the process exit code: 0 done, 1 failed, 2 usage error. */
|
|
601
|
+
export async function runSetupCli(argv, io = terminalIO()) {
|
|
602
|
+
try {
|
|
603
|
+
let parsed;
|
|
604
|
+
try {
|
|
605
|
+
parsed = parseArgs({
|
|
606
|
+
args: argv,
|
|
607
|
+
allowPositionals: true,
|
|
608
|
+
strict: true,
|
|
609
|
+
options: {
|
|
610
|
+
url: { type: 'string' },
|
|
611
|
+
'key-stdin': { type: 'boolean' },
|
|
612
|
+
'plain-file': { type: 'boolean' },
|
|
613
|
+
default: { type: 'boolean' },
|
|
614
|
+
'skip-check': { type: 'boolean' },
|
|
615
|
+
yes: { type: 'boolean', short: 'y' },
|
|
616
|
+
help: { type: 'boolean', short: 'h' },
|
|
617
|
+
},
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
catch (e) {
|
|
621
|
+
throw new SetupError(`${e.message}\n\n${USAGE}`, 2);
|
|
622
|
+
}
|
|
623
|
+
const [command, name, ...extra] = parsed.positionals;
|
|
624
|
+
if (command === 'clients' && !parsed.values.help) {
|
|
625
|
+
await cmdClients(io, name, extra, parsed.values);
|
|
626
|
+
return 0;
|
|
627
|
+
}
|
|
628
|
+
const flags = parsed.values;
|
|
629
|
+
if (flags.help || command === 'help') {
|
|
630
|
+
io.out(USAGE);
|
|
631
|
+
return 0;
|
|
632
|
+
}
|
|
633
|
+
if (extra.length > 0)
|
|
634
|
+
throw new SetupError(`Unexpected argument: ${extra[0]}\n\n${USAGE}`, 2);
|
|
635
|
+
switch (command) {
|
|
636
|
+
case undefined:
|
|
637
|
+
if (!io.interactive) {
|
|
638
|
+
io.out(USAGE);
|
|
639
|
+
return 2;
|
|
640
|
+
}
|
|
641
|
+
await cmdAdd(io, undefined, flags);
|
|
642
|
+
break;
|
|
643
|
+
case 'add':
|
|
644
|
+
await cmdAdd(io, name, flags);
|
|
645
|
+
break;
|
|
646
|
+
case 'list':
|
|
647
|
+
cmdList(io);
|
|
648
|
+
break;
|
|
649
|
+
case 'default':
|
|
650
|
+
cmdDefault(io, name);
|
|
651
|
+
break;
|
|
652
|
+
case 'remove':
|
|
653
|
+
await cmdRemove(io, name, flags);
|
|
654
|
+
break;
|
|
655
|
+
case 'test':
|
|
656
|
+
await cmdTest(io, name);
|
|
657
|
+
break;
|
|
658
|
+
case 'path':
|
|
659
|
+
cmdPath(io);
|
|
660
|
+
break;
|
|
661
|
+
case 'repair':
|
|
662
|
+
await cmdRepair(io, flags);
|
|
663
|
+
break;
|
|
664
|
+
case 'reset':
|
|
665
|
+
await cmdReset(io, flags);
|
|
666
|
+
break;
|
|
667
|
+
default:
|
|
668
|
+
throw new SetupError(`Unknown command: ${command}\n\n${USAGE}`, 2);
|
|
669
|
+
}
|
|
670
|
+
return 0;
|
|
671
|
+
}
|
|
672
|
+
catch (e) {
|
|
673
|
+
if (e instanceof SetupError) {
|
|
674
|
+
io.err(e.message);
|
|
675
|
+
return e.code;
|
|
676
|
+
}
|
|
677
|
+
io.err(`Unexpected error: ${e.message}`);
|
|
678
|
+
return 1;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
//# sourceMappingURL=setup.js.map
|