@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/core.js
ADDED
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Everything that talks to FoundryVTT: the Socket.IO client,
|
|
3
|
+
* the companion-module bridge, the headless GM session, and tool/resource
|
|
4
|
+
* routing over them.
|
|
5
|
+
*
|
|
6
|
+
* Owned either by the all-in-one stdio server (`FOUNDRY_MCP_SHARED=false`)
|
|
7
|
+
* or by the shared process that relays forward calls to.
|
|
8
|
+
*/
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { ErrorCode, McpError, } from '@modelcontextprotocol/sdk/types.js';
|
|
11
|
+
import { config, configSettings } from './config/index.js';
|
|
12
|
+
import { summarizeSources } from './config/sources.js';
|
|
13
|
+
import { DiagnosticsClient } from './diagnostics/client.js';
|
|
14
|
+
import { NoActiveWorldError } from './foundry/auth.js';
|
|
15
|
+
import { FoundryClient, gamemasterAccountWarning, roleLabel, } from './foundry/client.js';
|
|
16
|
+
import { HeadlessGmSession } from './foundry/headless-gm-session.js';
|
|
17
|
+
import { BRIDGE_PROTOCOL_VERSION, ModuleBridge } from './foundry/module-bridge.js';
|
|
18
|
+
import { defaultStateDir } from './shared/paths.js';
|
|
19
|
+
import { routeResourceRequest, routeToolRequest } from './tools/index.js';
|
|
20
|
+
import { isWriteTool } from './tools/write-tools.js';
|
|
21
|
+
import { DiagnosticSystem } from './utils/diagnostics.js';
|
|
22
|
+
import { redactToolArgs } from './utils/log-args.js';
|
|
23
|
+
import { logger } from './utils/logger.js';
|
|
24
|
+
import { isRefusal, refusalError } from './utils/refusal.js';
|
|
25
|
+
import { SerialQueue } from './utils/serial-queue.js';
|
|
26
|
+
/**
|
|
27
|
+
* How long a tool call waits for a FoundryVTT reconnection in progress
|
|
28
|
+
* before it is refused. A reauthentication takes about a second; a
|
|
29
|
+
* socket.io transport reconnection a few.
|
|
30
|
+
*/
|
|
31
|
+
export const TOOL_RECONNECT_WAIT_MS = 15_000;
|
|
32
|
+
/** Tools that report the connection itself, and so must not wait for it. */
|
|
33
|
+
const NO_RECONNECT_WAIT_TOOLS = new Set(['get_health_status']);
|
|
34
|
+
/**
|
|
35
|
+
* True when the headless GM page shares the connector's account (and
|
|
36
|
+
* session): no FOUNDRY_HEADLESS_GM_USERNAME, or the same name as
|
|
37
|
+
* FOUNDRY_USERNAME.
|
|
38
|
+
*/
|
|
39
|
+
export function isSharedAccount(headlessUsername, foundryUsername) {
|
|
40
|
+
return !headlessUsername || headlessUsername === foundryUsername;
|
|
41
|
+
}
|
|
42
|
+
/** One line for get_health_status: the configuration file and where settings come from. */
|
|
43
|
+
function configurationSummary() {
|
|
44
|
+
let settings;
|
|
45
|
+
try {
|
|
46
|
+
settings = configSettings();
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
const { file, sources } = settings;
|
|
52
|
+
const state = file.error ? file.error : file.found ? 'found' : 'not found';
|
|
53
|
+
return { file: `${file.path} (${state})`, sources: summarizeSources(sources) };
|
|
54
|
+
}
|
|
55
|
+
export class FoundryCore {
|
|
56
|
+
options;
|
|
57
|
+
foundryClient;
|
|
58
|
+
diagnosticsClient;
|
|
59
|
+
diagnosticSystem;
|
|
60
|
+
moduleBridge;
|
|
61
|
+
headlessGmSession;
|
|
62
|
+
/** Why the headless GM session's browser could not be launched, if it could not. */
|
|
63
|
+
headlessLaunchError = null;
|
|
64
|
+
/** Why the module bridge failed to start, if it did. */
|
|
65
|
+
bridgeError = null;
|
|
66
|
+
/** A headless GM session was configured (it may since have been dropped). */
|
|
67
|
+
headlessGmConfigured;
|
|
68
|
+
/** The headless GM page shares the connector's account; see {@link isSharedAccount}. */
|
|
69
|
+
sharedAccount;
|
|
70
|
+
/** Serialises every write tool, in arrival order; reads bypass it. */
|
|
71
|
+
writeQueue = new SerialQueue();
|
|
72
|
+
constructor(options = {}) {
|
|
73
|
+
this.options = options;
|
|
74
|
+
// Initialize FoundryVTT client with configuration
|
|
75
|
+
const clientConfig = {
|
|
76
|
+
baseUrl: config.foundry.url,
|
|
77
|
+
socketPath: config.foundry.socketPath,
|
|
78
|
+
timeout: config.foundry.timeout,
|
|
79
|
+
retryAttempts: config.foundry.retryAttempts,
|
|
80
|
+
retryDelay: config.foundry.retryDelay,
|
|
81
|
+
writeEnabled: config.foundry.writeEnabled,
|
|
82
|
+
eventBufferSize: config.events.bufferSize,
|
|
83
|
+
};
|
|
84
|
+
if (config.foundry.apiKey) {
|
|
85
|
+
clientConfig.apiKey = config.foundry.apiKey;
|
|
86
|
+
}
|
|
87
|
+
if (config.foundry.username) {
|
|
88
|
+
clientConfig.username = config.foundry.username;
|
|
89
|
+
}
|
|
90
|
+
if (config.foundry.password) {
|
|
91
|
+
clientConfig.password = config.foundry.password;
|
|
92
|
+
}
|
|
93
|
+
if (config.foundry.userId) {
|
|
94
|
+
clientConfig.userId = config.foundry.userId;
|
|
95
|
+
}
|
|
96
|
+
if (config.assets.dataPath) {
|
|
97
|
+
clientConfig.dataPath = config.assets.dataPath;
|
|
98
|
+
}
|
|
99
|
+
// Same state directory as the shared process's daemon.json, in both modes.
|
|
100
|
+
clientConfig.journalHistoryDir = path.join(defaultStateDir(), 'journal-history');
|
|
101
|
+
// Shared account: the page reuses the connector's own session, so the
|
|
102
|
+
// connector's user id already covers it. Separate account: its changes
|
|
103
|
+
// are the connector's own too.
|
|
104
|
+
this.sharedAccount = isSharedAccount(config.headlessGm.username, config.foundry.username);
|
|
105
|
+
if (!this.sharedAccount && config.headlessGm.username) {
|
|
106
|
+
clientConfig.connectorUsernames = [config.headlessGm.username];
|
|
107
|
+
}
|
|
108
|
+
this.foundryClient = new FoundryClient(clientConfig);
|
|
109
|
+
this.foundryClient.onSessionChanged(() => this.warnIfGamemaster());
|
|
110
|
+
// A detected journal overwrite is whispered to the GMs. The whisper is a
|
|
111
|
+
// write, so it waits its turn in the write queue like any tool write.
|
|
112
|
+
this.foundryClient.onJournalConflict = (conflict) => {
|
|
113
|
+
this.writeQueue
|
|
114
|
+
.run(() => this.foundryClient.whisperJournalConflict(conflict))
|
|
115
|
+
.catch((error) => {
|
|
116
|
+
logger.warn('Could not whisper a journal overwrite to the GMs', {
|
|
117
|
+
journalId: conflict.journalId,
|
|
118
|
+
pageId: conflict.pageId,
|
|
119
|
+
error: error instanceof Error ? error.message : String(error),
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
// Canvas-only tools (scene screenshots, etc.) relay through a companion
|
|
124
|
+
// Foundry module over a plain WebSocket; disabled unless opted in, since
|
|
125
|
+
// most tools need only the Socket.IO document API set up above.
|
|
126
|
+
// Only the module running in a page served from FOUNDRY_URL may connect.
|
|
127
|
+
this.moduleBridge = config.moduleBridge.enabled
|
|
128
|
+
? new ModuleBridge(config.moduleBridge.port, {
|
|
129
|
+
expectedOrigin: new URL(config.foundry.url).origin,
|
|
130
|
+
})
|
|
131
|
+
: null;
|
|
132
|
+
if (this.moduleBridge) {
|
|
133
|
+
// The bridge stays ignorant of FoundryClient; it only relays what the
|
|
134
|
+
// module pushed unsolicited (canvas targeting, pings, …).
|
|
135
|
+
this.moduleBridge.onEvent = (type, payload) => this.foundryClient.recordModuleEvent(type, payload);
|
|
136
|
+
}
|
|
137
|
+
// Invisible GM-tier browser session that hosts the companion module —
|
|
138
|
+
// no human has to manually open and log into a second Foundry tab.
|
|
139
|
+
// Only worth building when there's a bridge for it to connect to, and
|
|
140
|
+
// only possible with username/password credentials (an apiKey-only
|
|
141
|
+
// setup has nothing to log a browser in with). By default it shares the
|
|
142
|
+
// connector's session; FOUNDRY_HEADLESS_GM_USERNAME naming another
|
|
143
|
+
// account gives it a login of its own.
|
|
144
|
+
const socketUser = config.foundry.username ?? config.foundry.userId;
|
|
145
|
+
const headlessUsername = this.sharedAccount ? socketUser : config.headlessGm.username;
|
|
146
|
+
const headlessPassword = this.sharedAccount
|
|
147
|
+
? config.foundry.password
|
|
148
|
+
: (config.headlessGm.password ?? config.foundry.password);
|
|
149
|
+
if (this.moduleBridge && config.headlessGm.enabled && headlessUsername && headlessPassword) {
|
|
150
|
+
const bridge = this.moduleBridge;
|
|
151
|
+
const client = this.foundryClient;
|
|
152
|
+
this.headlessGmSession = new HeadlessGmSession({
|
|
153
|
+
foundryUrl: config.foundry.url,
|
|
154
|
+
username: headlessUsername,
|
|
155
|
+
...(this.sharedAccount
|
|
156
|
+
? {
|
|
157
|
+
sessionSource: {
|
|
158
|
+
currentSession: () => client.currentSession(),
|
|
159
|
+
onSessionChanged: (listener) => client.onSessionChanged(listener),
|
|
160
|
+
reauthenticate: (reason) => client.reauthenticate(reason),
|
|
161
|
+
},
|
|
162
|
+
}
|
|
163
|
+
: { password: headlessPassword }),
|
|
164
|
+
isBridgeConnected: () => this.moduleBridge?.isConnected() ?? false,
|
|
165
|
+
// The module authenticates through the page; the secret stays in the bridge.
|
|
166
|
+
bridgeCredentials: {
|
|
167
|
+
info: () => bridge.info(),
|
|
168
|
+
prove: (serverNonce) => bridge.prove(serverNonce),
|
|
169
|
+
},
|
|
170
|
+
...(config.headlessGm.executablePath
|
|
171
|
+
? { executablePath: config.headlessGm.executablePath }
|
|
172
|
+
: {}),
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
this.headlessGmSession = null;
|
|
177
|
+
if (this.moduleBridge && config.headlessGm.enabled) {
|
|
178
|
+
logger.warn('Headless GM session disabled — no username/password to log a browser in with (set FOUNDRY_USERNAME/FOUNDRY_PASSWORD or FOUNDRY_HEADLESS_GM_USERNAME/FOUNDRY_HEADLESS_GM_PASSWORD). The companion module only connects from that session, so canvas-only tools will be unavailable.');
|
|
179
|
+
}
|
|
180
|
+
else if (this.moduleBridge) {
|
|
181
|
+
logger.warn('Module bridge enabled but FOUNDRY_HEADLESS_GM_ENABLED=false: the companion module only connects from the headless GM session (a manually opened GM tab is no longer supported), so canvas-only tools will be unavailable.');
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
this.headlessGmConfigured = this.headlessGmSession !== null;
|
|
185
|
+
// Initialize DiagnosticsClient
|
|
186
|
+
this.diagnosticsClient = new DiagnosticsClient(this.foundryClient);
|
|
187
|
+
// Initialize DiagnosticSystem
|
|
188
|
+
this.diagnosticSystem = new DiagnosticSystem(this.foundryClient);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Connects to FoundryVTT, then starts the module bridge and the headless
|
|
192
|
+
* GM session (both best-effort).
|
|
193
|
+
*
|
|
194
|
+
* @throws if the FoundryVTT connection fails
|
|
195
|
+
*/
|
|
196
|
+
async start() {
|
|
197
|
+
try {
|
|
198
|
+
// Connect to FoundryVTT. With no world launched yet, keep starting:
|
|
199
|
+
// the client reconnects in the background once one is, and the
|
|
200
|
+
// headless page waits for its session meanwhile.
|
|
201
|
+
try {
|
|
202
|
+
await this.foundryClient.connect();
|
|
203
|
+
logger.info('Connected to FoundryVTT successfully');
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
if (!(error instanceof NoActiveWorldError)) {
|
|
207
|
+
throw error;
|
|
208
|
+
}
|
|
209
|
+
logger.warn(`Could not connect to FoundryVTT yet: ${error.message} Retrying in the background.`);
|
|
210
|
+
this.foundryClient.startReconnecting(error);
|
|
211
|
+
}
|
|
212
|
+
// The module bridge is optional, best-effort infrastructure for a
|
|
213
|
+
// handful of canvas-only tools (capture_scene, etc.) — a failure here
|
|
214
|
+
// (e.g. the port is already taken by a still-shutting-down previous
|
|
215
|
+
// instance) must never take down the FoundryVTT Socket.IO connection
|
|
216
|
+
// and the 40+ tools that don't need a module at all.
|
|
217
|
+
if (this.moduleBridge) {
|
|
218
|
+
try {
|
|
219
|
+
await this.moduleBridge.start();
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
this.bridgeError = error instanceof Error ? error.message : String(error);
|
|
223
|
+
logger.error('Module bridge failed to start — canvas-only tools (capture_scene) will report unavailable; all other tools are unaffected:', error);
|
|
224
|
+
this.moduleBridge = null;
|
|
225
|
+
this.headlessGmSession = null;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
// Same best-effort isolation as the module bridge itself: a browser
|
|
229
|
+
// that fails to launch (none installed) must never take
|
|
230
|
+
// down the Socket.IO connection or the module bridge's WS server —
|
|
231
|
+
// canvas-only tools just report unavailable until this resolves.
|
|
232
|
+
// Login happens in the background after this resolves (Foundry may
|
|
233
|
+
// still be booting); see HeadlessGmSession.start().
|
|
234
|
+
if (this.headlessGmSession) {
|
|
235
|
+
try {
|
|
236
|
+
await this.headlessGmSession.start();
|
|
237
|
+
}
|
|
238
|
+
catch (error) {
|
|
239
|
+
// Kept for status / get_health_status: the session is dropped, so
|
|
240
|
+
// without it they would read "disabled".
|
|
241
|
+
this.headlessLaunchError = error instanceof Error ? error.message : String(error);
|
|
242
|
+
logger.error('Headless GM session failed to launch — the companion module cannot connect, so canvas-only tools will be unavailable:', error);
|
|
243
|
+
this.headlessGmSession = null;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
catch (error) {
|
|
248
|
+
if (isRefusal(error)) {
|
|
249
|
+
// Expected (e.g. no world launched yet): one line, no stack.
|
|
250
|
+
logger.warn(`Could not connect to FoundryVTT: ${error.message}`);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
logger.error('Failed to connect to FoundryVTT:', error);
|
|
254
|
+
}
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/** Disconnects from FoundryVTT and stops the headless session and the bridge. */
|
|
259
|
+
async shutdown() {
|
|
260
|
+
try {
|
|
261
|
+
await this.foundryClient.disconnect();
|
|
262
|
+
if (this.headlessGmSession) {
|
|
263
|
+
await this.headlessGmSession.stop();
|
|
264
|
+
}
|
|
265
|
+
if (this.moduleBridge) {
|
|
266
|
+
await this.moduleBridge.stop();
|
|
267
|
+
}
|
|
268
|
+
logger.info('FoundryVTT MCP Server shutdown completed');
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
logger.error('Error during shutdown:', error);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Runs one tool; failures surface as {@link McpError}. Write tools wait
|
|
276
|
+
* for the writes submitted before them to finish.
|
|
277
|
+
*/
|
|
278
|
+
callTool(name, args) {
|
|
279
|
+
if (isWriteTool(name)) {
|
|
280
|
+
return this.writeQueue.run(() => this.executeTool(name, args));
|
|
281
|
+
}
|
|
282
|
+
return this.executeTool(name, args);
|
|
283
|
+
}
|
|
284
|
+
async executeTool(name, args) {
|
|
285
|
+
logger.info('Executing tool', { name, args: redactToolArgs(args) });
|
|
286
|
+
try {
|
|
287
|
+
await this.waitForReconnection(name);
|
|
288
|
+
return (await routeToolRequest(name, args, this.foundryClient, this.diagnosticsClient, this.diagnosticSystem, this.moduleBridge, this.headlessGmSession, this.healthRuntime()));
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
if (isRefusal(error)) {
|
|
292
|
+
// Turned down by design (e.g. an outdated journal page version): the
|
|
293
|
+
// caller is told why; the log needs one line, not a stack.
|
|
294
|
+
logger.warn('Tool call refused', {
|
|
295
|
+
name,
|
|
296
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
logger.error('Tool execution failed:', error);
|
|
301
|
+
}
|
|
302
|
+
if (error instanceof McpError) {
|
|
303
|
+
throw error;
|
|
304
|
+
}
|
|
305
|
+
throw new McpError(ErrorCode.InternalError, `Tool execution failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* A call made while the connection is being restored waits for it (a write
|
|
310
|
+
* keeps its place in the write queue meanwhile), then is refused with the
|
|
311
|
+
* reason if it is still down: reads would answer from a cache nobody keeps
|
|
312
|
+
* live, and nothing may be sent on an anonymous socket.
|
|
313
|
+
*/
|
|
314
|
+
async waitForReconnection(name) {
|
|
315
|
+
if (NO_RECONNECT_WAIT_TOOLS.has(name) || !this.foundryClient.isReconnecting()) {
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
if (await this.foundryClient.waitUntilConnected(TOOL_RECONNECT_WAIT_MS)) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
const { state, error } = this.foundryClient.connectionStatus();
|
|
322
|
+
throw refusalError(ErrorCode.InvalidRequest, `Reconnecting to FoundryVTT… (${state}${error ? `: ${error}` : ''}). ` +
|
|
323
|
+
'The connector retries on its own; try again in a moment.');
|
|
324
|
+
}
|
|
325
|
+
/** Logs {@link gamemasterAccountWarning} after each authentication. */
|
|
326
|
+
warnIfGamemaster() {
|
|
327
|
+
const warning = gamemasterAccountWarning(this.sharedAccount, this.foundryClient.accountInfo());
|
|
328
|
+
if (warning) {
|
|
329
|
+
logger.warn(warning);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
healthRuntime() {
|
|
333
|
+
const configuration = configurationSummary();
|
|
334
|
+
return {
|
|
335
|
+
...(this.options.relays ? { relays: this.options.relays() } : {}),
|
|
336
|
+
...(this.headlessLaunchError ? { headlessLaunchError: this.headlessLaunchError } : {}),
|
|
337
|
+
bridgeState: this.bridgeState(),
|
|
338
|
+
sharedAccount: this.headlessGmSession?.mode() === 'shared',
|
|
339
|
+
...(configuration ? { configuration } : {}),
|
|
340
|
+
...(this.bridgeError ? { bridgeError: this.bridgeError } : {}),
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
/** Reads one `foundry://` resource; failures surface as {@link McpError}. */
|
|
344
|
+
async readResource(uri) {
|
|
345
|
+
logger.info('Reading resource', { uri });
|
|
346
|
+
try {
|
|
347
|
+
return (await routeResourceRequest(uri, this.foundryClient, this.diagnosticsClient));
|
|
348
|
+
}
|
|
349
|
+
catch (error) {
|
|
350
|
+
logger.error('Resource read failed:', error);
|
|
351
|
+
if (error instanceof McpError) {
|
|
352
|
+
throw error;
|
|
353
|
+
}
|
|
354
|
+
throw new McpError(ErrorCode.InternalError, `Resource read failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
bridgeState() {
|
|
358
|
+
if (!config.moduleBridge.enabled) {
|
|
359
|
+
return 'disabled';
|
|
360
|
+
}
|
|
361
|
+
if (this.bridgeError !== null) {
|
|
362
|
+
return 'failed';
|
|
363
|
+
}
|
|
364
|
+
return this.moduleBridge?.isListening() ? 'listening' : 'not-started';
|
|
365
|
+
}
|
|
366
|
+
headlessGmState() {
|
|
367
|
+
if (this.headlessLaunchError !== null) {
|
|
368
|
+
return 'launch-failed';
|
|
369
|
+
}
|
|
370
|
+
if (!this.headlessGmConfigured) {
|
|
371
|
+
return 'disabled';
|
|
372
|
+
}
|
|
373
|
+
return this.headlessGmSession?.isStarted() ? 'running' : 'not-started';
|
|
374
|
+
}
|
|
375
|
+
status() {
|
|
376
|
+
const bridgeState = this.bridgeState();
|
|
377
|
+
const headlessGmState = this.headlessGmState();
|
|
378
|
+
const account = this.foundryClient.accountInfo();
|
|
379
|
+
return {
|
|
380
|
+
foundryConnected: this.foundryClient.isConnected(),
|
|
381
|
+
foundryConnection: this.foundryClient.connectionStatus(),
|
|
382
|
+
account: account
|
|
383
|
+
? {
|
|
384
|
+
name: account.name,
|
|
385
|
+
role: account.role === null ? null : roleLabel(account.role),
|
|
386
|
+
warning: gamemasterAccountWarning(this.sharedAccount, account),
|
|
387
|
+
}
|
|
388
|
+
: null,
|
|
389
|
+
bridge: {
|
|
390
|
+
// Kept for `status` commands older than `state`: true only while listening.
|
|
391
|
+
enabled: bridgeState === 'listening',
|
|
392
|
+
state: bridgeState,
|
|
393
|
+
error: this.bridgeError,
|
|
394
|
+
port: config.moduleBridge.port,
|
|
395
|
+
moduleConnected: this.moduleBridge?.isConnected() ?? false,
|
|
396
|
+
protocol: BRIDGE_PROTOCOL_VERSION,
|
|
397
|
+
origin: new URL(config.foundry.url).origin,
|
|
398
|
+
moduleConnectedSince: this.moduleBridge?.moduleConnectedSince()?.toISOString() ?? null,
|
|
399
|
+
},
|
|
400
|
+
headlessGm: {
|
|
401
|
+
enabled: headlessGmState !== 'disabled',
|
|
402
|
+
active: this.headlessGmSession?.isActive() ?? false,
|
|
403
|
+
status: this.headlessGmSession?.statusText() ?? null,
|
|
404
|
+
launchError: this.headlessLaunchError,
|
|
405
|
+
state: headlessGmState,
|
|
406
|
+
mode: this.headlessGmSession?.mode() ?? null,
|
|
407
|
+
modeText: this.headlessGmSession?.modeText() ?? null,
|
|
408
|
+
sessionState: this.headlessGmSession?.sessionState() ?? null,
|
|
409
|
+
browser: this.headlessGmSession?.browserInfo() ?? null,
|
|
410
|
+
},
|
|
411
|
+
writeQueue: this.writeQueue.pending,
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
//# sourceMappingURL=core.js.map
|