@g33ks/foundryvtt-mcp 0.0.0-stage → 2.0.0
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/LICENSE +22 -0
- package/README.md +468 -2
- package/dist/character/manager.js +208 -0
- package/dist/cli/checks.js +307 -0
- package/dist/cli/config-file.js +120 -0
- package/dist/cli/init.js +410 -0
- package/dist/cli/prompt.js +117 -0
- package/dist/cli/snippets.js +49 -0
- package/dist/combat/manager.js +371 -0
- package/dist/config/env-layers.js +49 -0
- package/dist/config/index.js +298 -0
- package/dist/config/sources.js +201 -0
- package/dist/core.js +415 -0
- package/dist/diagnostics/client.js +353 -0
- package/dist/diagnostics/types.js +94 -0
- package/dist/foundry/auth.js +220 -0
- package/dist/foundry/browser-detect.js +170 -0
- package/dist/foundry/client.js +5555 -0
- package/dist/foundry/dice-formula.js +154 -0
- package/dist/foundry/event-log.js +299 -0
- package/dist/foundry/headless-gm-session.js +740 -0
- package/dist/foundry/journal-history.js +90 -0
- package/dist/foundry/journal-integrity.js +306 -0
- package/dist/foundry/module-bridge.js +413 -0
- package/dist/foundry/types.js +52 -0
- package/dist/foundry/world-cache.js +386 -0
- package/dist/index.js +107 -0
- package/dist/mcp-server.js +63 -0
- package/dist/server.js +159 -0
- package/dist/shared/build-order.js +134 -0
- package/dist/shared/config-hash.js +62 -0
- package/dist/shared/config-notice.js +55 -0
- package/dist/shared/control.js +255 -0
- package/dist/shared/daemon-link.js +135 -0
- package/dist/shared/daemon.js +313 -0
- package/dist/shared/ipc-client.js +200 -0
- package/dist/shared/ipc-server.js +316 -0
- package/dist/shared/paths.js +79 -0
- package/dist/shared/protocol.js +230 -0
- package/dist/shared/relay-config.js +65 -0
- package/dist/shared/relay.js +120 -0
- package/dist/shared/rotating-log.js +57 -0
- package/dist/shared/spawn.js +39 -0
- package/dist/shared/state-file.js +33 -0
- package/dist/shared/token.js +62 -0
- package/dist/shared/version.js +57 -0
- package/dist/tools/base.js +74 -0
- package/dist/tools/definitions.js +3109 -0
- package/dist/tools/handlers/actor-mutations.js +128 -0
- package/dist/tools/handlers/actors.js +114 -0
- package/dist/tools/handlers/chat-mutations.js +49 -0
- package/dist/tools/handlers/chat.js +36 -0
- package/dist/tools/handlers/combat-mutations.js +215 -0
- package/dist/tools/handlers/combat-order.js +94 -0
- package/dist/tools/handlers/combat.js +51 -0
- package/dist/tools/handlers/compendium.js +83 -0
- package/dist/tools/handlers/diagnostics.js +438 -0
- package/dist/tools/handlers/dice.js +54 -0
- package/dist/tools/handlers/effects.js +103 -0
- package/dist/tools/handlers/events.js +50 -0
- package/dist/tools/handlers/generation.js +258 -0
- package/dist/tools/handlers/item-mutations.js +132 -0
- package/dist/tools/handlers/items.js +53 -0
- package/dist/tools/handlers/journal-mutations.js +230 -0
- package/dist/tools/handlers/journals.js +80 -0
- package/dist/tools/handlers/module-bridge.js +421 -0
- package/dist/tools/handlers/placeable-mutations.js +272 -0
- package/dist/tools/handlers/resources.js +203 -0
- package/dist/tools/handlers/roll-tables.js +97 -0
- package/dist/tools/handlers/scene-mutations.js +159 -0
- package/dist/tools/handlers/scenes.js +355 -0
- package/dist/tools/handlers/settings.js +53 -0
- package/dist/tools/handlers/tile-mutations.js +56 -0
- package/dist/tools/handlers/token-mutations.js +356 -0
- package/dist/tools/handlers/users.js +59 -0
- package/dist/tools/handlers/utils.js +47 -0
- package/dist/tools/handlers/wall-mutations.js +98 -0
- package/dist/tools/handlers/world-documents.js +222 -0
- package/dist/tools/handlers/world.js +92 -0
- package/dist/tools/index.js +25 -0
- package/dist/tools/new-router.js +97 -0
- package/dist/tools/registry.js +79 -0
- package/dist/tools/resources.js +63 -0
- package/dist/tools/router.js +688 -0
- package/dist/tools/write-tools.js +97 -0
- package/dist/utils/cache-instance.js +30 -0
- package/dist/utils/cache.js +205 -0
- package/dist/utils/diagnostics.js +332 -0
- package/dist/utils/guards.js +17 -0
- package/dist/utils/image-size.js +160 -0
- package/dist/utils/log-args.js +47 -0
- package/dist/utils/logger.js +203 -0
- package/dist/utils/refusal.js +30 -0
- package/dist/utils/sanitize.js +22 -0
- package/dist/utils/serial-queue.js +22 -0
- package/package.json +124 -4
package/dist/cli/init.js
ADDED
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview `foundryvtt-mcp init`: writes a working configuration file,
|
|
3
|
+
* checking each answer against the live FoundryVTT, then prints what to
|
|
4
|
+
* paste into Claude desktop and Claude Code. `init --check` runs the same
|
|
5
|
+
* checks on the existing configuration, asking and writing nothing.
|
|
6
|
+
*
|
|
7
|
+
* The two-account mode (`FOUNDRY_HEADLESS_GM_USERNAME`) stays out of it: an
|
|
8
|
+
* advanced option, set by hand in the file.
|
|
9
|
+
*
|
|
10
|
+
* Nothing printed here carries a password: secrets show as `<set>`.
|
|
11
|
+
*/
|
|
12
|
+
import { readConfigFile, resolveSettings } from '../config/sources.js';
|
|
13
|
+
import { IDENTITY_SETTINGS } from '../shared/config-hash.js';
|
|
14
|
+
import { packageName } from '../shared/version.js';
|
|
15
|
+
import { checkBrowser, describeLoginError, evaluateModule, evaluateRole, loginAndReadWorld, normalizeUrl, probeJoin, } from './checks.js';
|
|
16
|
+
import { quoteValue, UnwritableValueError, writeConfig } from './config-file.js';
|
|
17
|
+
import { PromptCancelled, terminalProblem, terminalPrompter } from './prompt.js';
|
|
18
|
+
import { claudeCodeCommand, claudeDesktopSnippet, connectorCommand, installKind, } from './snippets.js';
|
|
19
|
+
const MANAGED_KEYS = [
|
|
20
|
+
'FOUNDRY_URL',
|
|
21
|
+
'FOUNDRY_USERNAME',
|
|
22
|
+
'FOUNDRY_PASSWORD',
|
|
23
|
+
'FOUNDRY_MODULE_BRIDGE_ENABLED',
|
|
24
|
+
'FOUNDRY_WRITE_ENABLED',
|
|
25
|
+
];
|
|
26
|
+
const ACCOUNT_ADVICE = [
|
|
27
|
+
'The connector logs in to FoundryVTT as a user of its own. Create one for it (Game Settings →',
|
|
28
|
+
'User Management): an Assistant GM — the companion module and the invisible GM browser session',
|
|
29
|
+
'need a GM role, and an Assistant GM never takes over from your own Gamemaster — with a long,',
|
|
30
|
+
'unique password used nowhere else.',
|
|
31
|
+
];
|
|
32
|
+
function writeAdvice(command) {
|
|
33
|
+
return [
|
|
34
|
+
'Allow changes to the game? With "yes", the assistant can create, edit and delete documents',
|
|
35
|
+
'(actors, journals, scenes, tokens, combat…) in the world. With "no", it can only read.',
|
|
36
|
+
'To change your mind later: edit FOUNDRY_WRITE_ENABLED in the configuration file, then run',
|
|
37
|
+
`\`${command} stop\` so the shared process restarts with it.`,
|
|
38
|
+
];
|
|
39
|
+
}
|
|
40
|
+
/** Why `init` prints no client entry when it runs from npx's cache, and what to do. */
|
|
41
|
+
function npxAdvice(entry) {
|
|
42
|
+
return [
|
|
43
|
+
"No client entry printed: this connector runs from npx's cache,",
|
|
44
|
+
` ${entry}`,
|
|
45
|
+
'which npm replaces or deletes when it updates or cleans that cache. Install it once instead:',
|
|
46
|
+
` npm install -g ${packageName()}`,
|
|
47
|
+
'then run:',
|
|
48
|
+
' foundryvtt-mcp init',
|
|
49
|
+
'to get the Claude desktop and Claude Code entries. The configuration written above is kept.',
|
|
50
|
+
];
|
|
51
|
+
}
|
|
52
|
+
function ok(print, message) {
|
|
53
|
+
print(`✔ ${message}`);
|
|
54
|
+
}
|
|
55
|
+
function fail(print, message) {
|
|
56
|
+
print(`✖ ${message}`);
|
|
57
|
+
}
|
|
58
|
+
function warn(print, message) {
|
|
59
|
+
if (message) {
|
|
60
|
+
print(`⚠ ${message}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/** Settings this shell would take from somewhere else than the file. */
|
|
64
|
+
function overriddenInShell(settings) {
|
|
65
|
+
return MANAGED_KEYS.filter((key) => {
|
|
66
|
+
const source = settings.sources[key];
|
|
67
|
+
return source === 'env' || source === '.env';
|
|
68
|
+
}).map((key) => `${key} (from ${settings.sources[key] === 'env' ? 'the environment' : settings.dotenvPath})`);
|
|
69
|
+
}
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
// init --check
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
async function runCheck(deps) {
|
|
74
|
+
const { print } = deps;
|
|
75
|
+
const settings = deps.resolve();
|
|
76
|
+
const v = settings.values;
|
|
77
|
+
print(`Configuration file: ${settings.file.path}${settings.file.found ? '' : ' (not found)'}`);
|
|
78
|
+
if (settings.file.error) {
|
|
79
|
+
warn(print, settings.file.error);
|
|
80
|
+
}
|
|
81
|
+
warn(print, settings.file.warning ?? undefined);
|
|
82
|
+
let healthy = true;
|
|
83
|
+
const url = normalizeUrl(v.FOUNDRY_URL ?? '');
|
|
84
|
+
if (!v.FOUNDRY_URL || !url.ok) {
|
|
85
|
+
fail(print, v.FOUNDRY_URL
|
|
86
|
+
? url.message
|
|
87
|
+
: `FOUNDRY_URL is not set: run \`${deps.command} init\` to write it.`);
|
|
88
|
+
healthy = false;
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
warn(print, url.warning);
|
|
92
|
+
const join = await deps.probeJoin(url.value);
|
|
93
|
+
(join.ok ? ok : fail)(print, join.message);
|
|
94
|
+
healthy &&= join.ok;
|
|
95
|
+
const username = v.FOUNDRY_USERNAME ?? v.FOUNDRY_USER_ID;
|
|
96
|
+
if (!join.ok) {
|
|
97
|
+
print(' (login, role and module not checked)');
|
|
98
|
+
}
|
|
99
|
+
else if (!username || !v.FOUNDRY_PASSWORD) {
|
|
100
|
+
fail(print, `FOUNDRY_USERNAME or FOUNDRY_PASSWORD is not set: run \`${deps.command} init\` to write them.`);
|
|
101
|
+
healthy = false;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
try {
|
|
105
|
+
const world = await deps.login(url.value, username, v.FOUNDRY_PASSWORD);
|
|
106
|
+
const role = evaluateRole(username, world.role);
|
|
107
|
+
(role.ok ? ok : fail)(print, role.message);
|
|
108
|
+
if (role.ok) {
|
|
109
|
+
warn(print, role.warning);
|
|
110
|
+
}
|
|
111
|
+
const module = evaluateModule(world.modules);
|
|
112
|
+
(module.ok ? ok : fail)(print, module.message.replace(/\nThen press Enter here to check again\.$/, ''));
|
|
113
|
+
healthy &&= role.ok && module.ok;
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
fail(print, describeLoginError(url.value, username, error).message);
|
|
117
|
+
healthy = false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (v.FOUNDRY_MODULE_BRIDGE_ENABLED !== 'true') {
|
|
122
|
+
warn(print, 'FOUNDRY_MODULE_BRIDGE_ENABLED is not true: the companion module, and the tools that need it ' +
|
|
123
|
+
'(capture_scene…), stay off.');
|
|
124
|
+
}
|
|
125
|
+
if (v.FOUNDRY_HEADLESS_GM_ENABLED === 'false') {
|
|
126
|
+
print(' Headless GM session disabled (FOUNDRY_HEADLESS_GM_ENABLED=false): browser not checked.');
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
const browser = await deps.checkBrowser(v.FOUNDRY_HEADLESS_GM_EXECUTABLE_PATH);
|
|
130
|
+
(browser.ok ? ok : fail)(print, browser.message);
|
|
131
|
+
healthy &&= browser.ok;
|
|
132
|
+
}
|
|
133
|
+
print(healthy ? 'All checks passed.' : 'Some checks failed: see above.');
|
|
134
|
+
return healthy ? 0 : 1;
|
|
135
|
+
}
|
|
136
|
+
async function askUrl(deps, current) {
|
|
137
|
+
const { print, prompter } = deps;
|
|
138
|
+
let typed = current;
|
|
139
|
+
for (;;) {
|
|
140
|
+
const answer = await prompter.ask('FoundryVTT address (e.g. https://foundry.example.com)', typed);
|
|
141
|
+
typed = answer || typed;
|
|
142
|
+
const url = normalizeUrl(answer);
|
|
143
|
+
if (!url.ok) {
|
|
144
|
+
fail(print, url.message);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
warn(print, url.warning);
|
|
148
|
+
const join = await deps.probeJoin(url.value);
|
|
149
|
+
if (join.ok) {
|
|
150
|
+
ok(print, join.message);
|
|
151
|
+
return url.value;
|
|
152
|
+
}
|
|
153
|
+
fail(print, join.message);
|
|
154
|
+
print('Press Enter to try again with the same address, or type another one.');
|
|
155
|
+
typed = url.value;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async function askAccount(deps, username, hasPassword) {
|
|
159
|
+
const { prompter, print } = deps;
|
|
160
|
+
let name = '';
|
|
161
|
+
while (name === '') {
|
|
162
|
+
name = (await prompter.ask('FoundryVTT user for the connector', username)).trim();
|
|
163
|
+
if (name === '') {
|
|
164
|
+
fail(print, 'Type the name of the FoundryVTT user.');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
for (;;) {
|
|
168
|
+
const password = await prompter.askSecret(`Password of "${name}"`, hasPassword);
|
|
169
|
+
if (password === '') {
|
|
170
|
+
fail(print, 'The password cannot be empty.');
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
// Refused now rather than after every check: the file could not hold it.
|
|
175
|
+
if (password !== null) {
|
|
176
|
+
quoteValue('FOUNDRY_PASSWORD', password);
|
|
177
|
+
}
|
|
178
|
+
quoteValue('FOUNDRY_USERNAME', name);
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
fail(print, error instanceof Error ? error.message : String(error));
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
return { username: name, password };
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/** Login, role and module, until they all pass; may send the user back to the URL. */
|
|
188
|
+
async function checkAccount(deps, fileValues, first) {
|
|
189
|
+
const { print, prompter } = deps;
|
|
190
|
+
const answers = { ...first };
|
|
191
|
+
for (;;) {
|
|
192
|
+
const password = answers.password ?? fileValues.FOUNDRY_PASSWORD ?? '';
|
|
193
|
+
let world;
|
|
194
|
+
try {
|
|
195
|
+
world = await deps.login(answers.url, answers.username, password);
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
const { message, retry } = describeLoginError(answers.url, answers.username, error);
|
|
199
|
+
fail(print, message);
|
|
200
|
+
if (retry === 'world') {
|
|
201
|
+
await prompter.pause('Press Enter once the world is running.');
|
|
202
|
+
}
|
|
203
|
+
else if (retry === 'url') {
|
|
204
|
+
answers.url = await askUrl(deps, answers.url);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
const account = await askAccount(deps, answers.username, answers.password !== null || !!fileValues.FOUNDRY_PASSWORD);
|
|
208
|
+
answers.username = account.username;
|
|
209
|
+
answers.password = account.password ?? answers.password;
|
|
210
|
+
}
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
const role = evaluateRole(answers.username, world.role);
|
|
214
|
+
if (!role.ok) {
|
|
215
|
+
fail(print, role.message);
|
|
216
|
+
await prompter.pause('Press Enter to log in again.');
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
ok(print, role.message);
|
|
220
|
+
warn(print, role.warning);
|
|
221
|
+
const module = evaluateModule(world.modules);
|
|
222
|
+
if (!module.ok) {
|
|
223
|
+
fail(print, module.message);
|
|
224
|
+
await prompter.pause('Press Enter to check again.');
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
ok(print, module.message);
|
|
228
|
+
return answers;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
async function checkBrowserStep(deps, executablePath) {
|
|
232
|
+
const { print, prompter } = deps;
|
|
233
|
+
for (;;) {
|
|
234
|
+
print(executablePath
|
|
235
|
+
? `Browser configured: ${executablePath}`
|
|
236
|
+
: 'Looking for a Chromium-based browser…');
|
|
237
|
+
const browser = await deps.checkBrowser(executablePath);
|
|
238
|
+
if (browser.ok) {
|
|
239
|
+
ok(print, browser.message);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
fail(print, browser.message);
|
|
243
|
+
const retry = await prompter.confirm('Try again? ("no" goes on without it: the tools that need the companion module stay unavailable)', true);
|
|
244
|
+
if (!retry) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
async function runInteractive(deps) {
|
|
250
|
+
const { print, prompter } = deps;
|
|
251
|
+
const settings = deps.resolve();
|
|
252
|
+
const file = settings.file.path;
|
|
253
|
+
const fileValues = settings.file.found ? deps.readFile(file) : {};
|
|
254
|
+
print('FoundryVTT MCP — setup');
|
|
255
|
+
print(`Configuration file: ${file}${settings.file.found ? ' (exists: its values are the defaults)' : ' (will be created)'}`);
|
|
256
|
+
if (settings.file.error) {
|
|
257
|
+
fail(print, `${settings.file.error}. Fix or move the file, then run init again.`);
|
|
258
|
+
return 1;
|
|
259
|
+
}
|
|
260
|
+
const overridden = overriddenInShell(settings);
|
|
261
|
+
if (overridden.length > 0) {
|
|
262
|
+
warn(print, `Set outside the file in this shell, and winning over it here: ${overridden.join(', ')}. ` +
|
|
263
|
+
'The values below go to the file only.');
|
|
264
|
+
}
|
|
265
|
+
print('');
|
|
266
|
+
// 2. URL
|
|
267
|
+
const url = await askUrl(deps, fileValues.FOUNDRY_URL);
|
|
268
|
+
print('');
|
|
269
|
+
// 3–5. Account, role, module
|
|
270
|
+
for (const line of ACCOUNT_ADVICE) {
|
|
271
|
+
print(line);
|
|
272
|
+
}
|
|
273
|
+
const account = await askAccount(deps, fileValues.FOUNDRY_USERNAME, !!fileValues.FOUNDRY_PASSWORD);
|
|
274
|
+
const answers = await checkAccount(deps, fileValues, { url, ...account });
|
|
275
|
+
print('');
|
|
276
|
+
// 6. Browser
|
|
277
|
+
await checkBrowserStep(deps, fileValues.FOUNDRY_HEADLESS_GM_EXECUTABLE_PATH);
|
|
278
|
+
print('');
|
|
279
|
+
// Writes
|
|
280
|
+
for (const line of writeAdvice(deps.command)) {
|
|
281
|
+
print(line);
|
|
282
|
+
}
|
|
283
|
+
const allowWrites = await prompter.confirm('Allow changes to the game?', settings.file.found ? fileValues.FOUNDRY_WRITE_ENABLED === 'true' : false);
|
|
284
|
+
print('');
|
|
285
|
+
// 7. File
|
|
286
|
+
const wanted = {
|
|
287
|
+
FOUNDRY_URL: answers.url,
|
|
288
|
+
FOUNDRY_USERNAME: answers.username,
|
|
289
|
+
...(answers.password !== null ? { FOUNDRY_PASSWORD: answers.password } : {}),
|
|
290
|
+
FOUNDRY_MODULE_BRIDGE_ENABLED: 'true',
|
|
291
|
+
// false is the default: written only to change a value already in the file.
|
|
292
|
+
...(allowWrites || fileValues.FOUNDRY_WRITE_ENABLED !== undefined
|
|
293
|
+
? { FOUNDRY_WRITE_ENABLED: allowWrites ? 'true' : 'false' }
|
|
294
|
+
: {}),
|
|
295
|
+
};
|
|
296
|
+
const updates = MANAGED_KEYS.flatMap((key) => {
|
|
297
|
+
const value = wanted[key];
|
|
298
|
+
return value !== undefined && value !== fileValues[key] ? [[key, value]] : [];
|
|
299
|
+
});
|
|
300
|
+
if (updates.length === 0) {
|
|
301
|
+
ok(print, `Nothing to change in ${file}.`);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
try {
|
|
305
|
+
const result = deps.write(file, updates);
|
|
306
|
+
if (result.backup) {
|
|
307
|
+
print(`Previous file copied to ${result.backup}`);
|
|
308
|
+
}
|
|
309
|
+
ok(print, `${result.created ? 'Created' : 'Updated'} ${file} (${updates.map(([key]) => key).join(', ')}), read back and checked.`);
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
fail(print, error instanceof Error ? error.message : String(error));
|
|
313
|
+
return 1;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
print('');
|
|
317
|
+
// 8. Snippets
|
|
318
|
+
if (installKind(deps.entry) === 'npx') {
|
|
319
|
+
for (const line of npxAdvice(deps.entry)) {
|
|
320
|
+
print(line);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
print('Claude desktop — Settings → Developer → Edit Config opens claude_desktop_config.json.');
|
|
325
|
+
print('Add this entry (merge it into "mcpServers" if the file already has one):');
|
|
326
|
+
print(claudeDesktopSnippet(deps.node, deps.entry));
|
|
327
|
+
print('Then quit Claude desktop completely and start it again.');
|
|
328
|
+
print('');
|
|
329
|
+
print('Claude Code — register the connector for all your projects:');
|
|
330
|
+
print(` ${claudeCodeCommand(deps.node, deps.entry)}`);
|
|
331
|
+
print('or for the current project only, on this machine:');
|
|
332
|
+
print(` ${claudeCodeCommand(deps.node, deps.entry, 'local')}`);
|
|
333
|
+
print('');
|
|
334
|
+
print(`Both name this Node.js: ${deps.node}. After changing or updating Node.js (nvm, installer), run \`${deps.command} init\` again for new ones.`);
|
|
335
|
+
}
|
|
336
|
+
print('');
|
|
337
|
+
// 9. Shared process
|
|
338
|
+
const identityChanged = updates.some(([key]) => key in IDENTITY_SETTINGS);
|
|
339
|
+
if (identityChanged && (await deps.sharedProcessRunning())) {
|
|
340
|
+
print('The shared process is running with the previous connection settings.');
|
|
341
|
+
if (await prompter.confirm('Stop it now? It restarts with the new settings on the next tool call.', false)) {
|
|
342
|
+
await deps.stopSharedProcess();
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
print(`It keeps the previous settings until \`${deps.command} stop\`.`);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
ok(print, `Setup complete. To check it again later: ${deps.command} init --check`);
|
|
349
|
+
return 0;
|
|
350
|
+
}
|
|
351
|
+
// ---------------------------------------------------------------------------
|
|
352
|
+
// Entry point
|
|
353
|
+
// ---------------------------------------------------------------------------
|
|
354
|
+
async function defaultDeps(entry) {
|
|
355
|
+
const { isEndpointAlive } = await import('../shared/ipc-server.js');
|
|
356
|
+
const { resolveSharedPaths } = await import('../shared/paths.js');
|
|
357
|
+
const { runStop } = await import('../shared/control.js');
|
|
358
|
+
const command = connectorCommand(entry);
|
|
359
|
+
return {
|
|
360
|
+
prompter: terminalPrompter(),
|
|
361
|
+
print: (line) => process.stdout.write(`${line}\n`),
|
|
362
|
+
entry,
|
|
363
|
+
node: process.execPath,
|
|
364
|
+
command,
|
|
365
|
+
resolve: () => resolveSettings(),
|
|
366
|
+
readFile: (file) => readConfigFile(file).values,
|
|
367
|
+
probeJoin: (url) => probeJoin(url),
|
|
368
|
+
login: loginAndReadWorld,
|
|
369
|
+
checkBrowser,
|
|
370
|
+
write: (file, updates) => writeConfig(file, updates),
|
|
371
|
+
sharedProcessRunning: () => isEndpointAlive(resolveSharedPaths().endpoint),
|
|
372
|
+
stopSharedProcess: () => runStop(),
|
|
373
|
+
terminalProblem: () => terminalProblem(process.stdin, process.stdout, command),
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
/** `init [--check]`; returns the exit code. */
|
|
377
|
+
export async function runInit(args, entry, injected) {
|
|
378
|
+
const deps = { ...(await defaultDeps(entry)), ...injected };
|
|
379
|
+
// The connector's own log lines (authentication…) would interleave with the questions.
|
|
380
|
+
const { logger } = await import('../utils/logger.js');
|
|
381
|
+
logger.setLevel('error');
|
|
382
|
+
const unknown = args.filter((arg) => arg !== '--check');
|
|
383
|
+
if (unknown.length > 0) {
|
|
384
|
+
deps.print(`Unknown option: ${unknown.join(' ')}\nUsage: ${deps.command} init [--check]`);
|
|
385
|
+
return 2;
|
|
386
|
+
}
|
|
387
|
+
if (args.includes('--check')) {
|
|
388
|
+
return await runCheck(deps);
|
|
389
|
+
}
|
|
390
|
+
const problem = deps.terminalProblem();
|
|
391
|
+
if (problem) {
|
|
392
|
+
deps.print(problem);
|
|
393
|
+
return 2;
|
|
394
|
+
}
|
|
395
|
+
try {
|
|
396
|
+
return await runInteractive(deps);
|
|
397
|
+
}
|
|
398
|
+
catch (error) {
|
|
399
|
+
if (error instanceof PromptCancelled) {
|
|
400
|
+
deps.print('Cancelled: nothing was written.');
|
|
401
|
+
return 130;
|
|
402
|
+
}
|
|
403
|
+
if (error instanceof UnwritableValueError) {
|
|
404
|
+
deps.print(`✖ ${error.message}`);
|
|
405
|
+
return 1;
|
|
406
|
+
}
|
|
407
|
+
throw error;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
//# sourceMappingURL=init.js.map
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Terminal questions for `init`.
|
|
3
|
+
*
|
|
4
|
+
* Every answer is read in raw mode, key by key, so that a password is never
|
|
5
|
+
* echoed: no character, no mask, nothing that shows its length. Works in
|
|
6
|
+
* PowerShell, Windows Terminal and cmd (Node's console handling under
|
|
7
|
+
* ConPTY) as in POSIX terminals; Git Bash's mintty without winpty is no
|
|
8
|
+
* terminal for Node, and is refused like a redirected input.
|
|
9
|
+
*/
|
|
10
|
+
import { emitKeypressEvents } from 'node:readline';
|
|
11
|
+
/** The user pressed Ctrl+C (or Ctrl+D on an empty answer). */
|
|
12
|
+
export class PromptCancelled extends Error {
|
|
13
|
+
name = 'PromptCancelled';
|
|
14
|
+
constructor() {
|
|
15
|
+
super('Cancelled.');
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** Why `init` cannot ask anything here, or `null` when it can. */
|
|
19
|
+
export function terminalProblem(stdin = process.stdin, stdout = process.stdout, command = 'node dist/index.js') {
|
|
20
|
+
if (stdin.isTTY && stdout.isTTY) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
return [
|
|
24
|
+
'`init` asks questions and needs an interactive terminal, but its input or output is redirected.',
|
|
25
|
+
`Run it directly in PowerShell, Windows Terminal, cmd or a terminal window (in Git Bash: \`winpty ${command} init\`).`,
|
|
26
|
+
`To check an existing configuration without questions, run \`${command} init --check\`.`,
|
|
27
|
+
].join('\n');
|
|
28
|
+
}
|
|
29
|
+
function hasControlCharacter(text) {
|
|
30
|
+
return Array.from(text).some((char) => {
|
|
31
|
+
const code = char.codePointAt(0) ?? 0;
|
|
32
|
+
return code < 0x20 || code === 0x7f;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/** Reads one answer in raw mode; `echo` false shows nothing at all. */
|
|
36
|
+
function readAnswer(prompt, echo) {
|
|
37
|
+
const { stdin, stdout } = process;
|
|
38
|
+
return new Promise((resolve, reject) => {
|
|
39
|
+
let answer = '';
|
|
40
|
+
emitKeypressEvents(stdin);
|
|
41
|
+
const wasRaw = stdin.isRaw;
|
|
42
|
+
stdin.setRawMode(true);
|
|
43
|
+
stdin.resume();
|
|
44
|
+
stdout.write(prompt);
|
|
45
|
+
const done = (outcome) => {
|
|
46
|
+
stdin.off('keypress', onKey);
|
|
47
|
+
stdin.setRawMode(wasRaw);
|
|
48
|
+
stdin.pause();
|
|
49
|
+
stdout.write('\n');
|
|
50
|
+
outcome();
|
|
51
|
+
};
|
|
52
|
+
const onKey = (text, key) => {
|
|
53
|
+
if (key?.ctrl && (key.name === 'c' || (key.name === 'd' && answer === ''))) {
|
|
54
|
+
done(() => reject(new PromptCancelled()));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (key?.name === 'return' || key?.name === 'enter') {
|
|
58
|
+
done(() => resolve(answer));
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (key?.name === 'backspace') {
|
|
62
|
+
if (answer.length > 0) {
|
|
63
|
+
const chars = Array.from(answer);
|
|
64
|
+
chars.pop();
|
|
65
|
+
answer = chars.join('');
|
|
66
|
+
if (echo) {
|
|
67
|
+
stdout.write('\b \b');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
// Arrows, function keys and other control sequences carry no text.
|
|
73
|
+
if (key?.ctrl || key?.meta || text === undefined || hasControlCharacter(text)) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
answer += text;
|
|
77
|
+
if (echo) {
|
|
78
|
+
stdout.write(text);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
stdin.on('keypress', onKey);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/** The prompter for an interactive terminal. */
|
|
85
|
+
export function terminalPrompter() {
|
|
86
|
+
return {
|
|
87
|
+
async ask(question, defaultValue) {
|
|
88
|
+
const hint = defaultValue ? ` [${defaultValue}]` : '';
|
|
89
|
+
const answer = (await readAnswer(`${question}${hint}: `, true)).trim();
|
|
90
|
+
return answer === '' ? (defaultValue ?? '') : answer;
|
|
91
|
+
},
|
|
92
|
+
async askSecret(question, keepable) {
|
|
93
|
+
const hint = keepable ? ' [<set> — Enter keeps it]' : '';
|
|
94
|
+
const answer = await readAnswer(`${question}${hint} (typing stays hidden): `, false);
|
|
95
|
+
return answer === '' && keepable ? null : answer;
|
|
96
|
+
},
|
|
97
|
+
async confirm(question, defaultValue) {
|
|
98
|
+
const hint = defaultValue ? '[Y/n]' : '[y/N]';
|
|
99
|
+
for (;;) {
|
|
100
|
+
const answer = (await readAnswer(`${question} ${hint} `, true)).trim().toLowerCase();
|
|
101
|
+
if (answer === '') {
|
|
102
|
+
return defaultValue;
|
|
103
|
+
}
|
|
104
|
+
if (['y', 'yes', 'o', 'oui'].includes(answer)) {
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
if (['n', 'no', 'non'].includes(answer)) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
async pause(message) {
|
|
113
|
+
await readAnswer(`${message} `, true);
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=prompt.js.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview What `init` prints for the user to paste into their MCP clients.
|
|
3
|
+
*
|
|
4
|
+
* No `env` block: every setting lives in the shared configuration file.
|
|
5
|
+
* Both clients get absolute paths, of Node and of `dist/index.js`: an MCP
|
|
6
|
+
* client may start servers with a reduced PATH (macOS, nvm) and an
|
|
7
|
+
* undefined working directory.
|
|
8
|
+
*/
|
|
9
|
+
import { packageName } from '../shared/version.js';
|
|
10
|
+
/** Server name in both clients. */
|
|
11
|
+
export const SERVER_NAME = 'foundryvtt';
|
|
12
|
+
/** The package's `bin` name, on the PATH after `npm install -g`. */
|
|
13
|
+
const BIN_NAME = 'foundryvtt-mcp';
|
|
14
|
+
export function installKind(entry) {
|
|
15
|
+
// Both separators: the same rules apply to Windows and POSIX paths.
|
|
16
|
+
const segments = entry.split(/[\\/]/);
|
|
17
|
+
if (segments.includes('_npx')) {
|
|
18
|
+
return 'npx';
|
|
19
|
+
}
|
|
20
|
+
return segments.includes('node_modules') ? 'global' : 'clone';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* How to run the connector from a terminal, for the commands `init` prints:
|
|
24
|
+
* the `bin` name for a global install, `npx <package>` from npx's cache,
|
|
25
|
+
* otherwise `node "<entry>"`, which works from any folder after a clone.
|
|
26
|
+
*/
|
|
27
|
+
export function connectorCommand(entry) {
|
|
28
|
+
switch (installKind(entry)) {
|
|
29
|
+
case 'global':
|
|
30
|
+
return BIN_NAME;
|
|
31
|
+
case 'npx':
|
|
32
|
+
return `npx ${packageName()}`;
|
|
33
|
+
case 'clone':
|
|
34
|
+
return `node "${entry}"`;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** The `mcpServers` entry for Claude desktop's `claude_desktop_config.json`. */
|
|
38
|
+
export function claudeDesktopSnippet(node, entry) {
|
|
39
|
+
return JSON.stringify({ mcpServers: { [SERVER_NAME]: { command: node, args: [entry] } } }, null, 2);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* `claude mcp add` command registering the connector in Claude Code. The
|
|
43
|
+
* paths go between double quotes, which PowerShell, cmd and POSIX shells
|
|
44
|
+
* all read the same way for a path (no `"` in a path on Windows).
|
|
45
|
+
*/
|
|
46
|
+
export function claudeCodeCommand(node, entry, scope = 'user') {
|
|
47
|
+
return `claude mcp add ${SERVER_NAME} --scope ${scope} -- "${node}" "${entry}"`;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=snippets.js.map
|