@g33ks/foundryvtt-mcp 2.2.1 → 3.0.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 +70 -411
- package/dist/foundry/auth.js +40 -1
- package/dist/foundry/client.js +151 -24
- package/dist/foundry/scene-lighting.js +41 -0
- package/dist/mcp-server.js +12 -5
- package/dist/tools/definitions.js +32 -32
- package/dist/tools/handlers/actor-mutations.js +5 -3
- package/dist/tools/handlers/combat-mutations.js +3 -3
- package/dist/tools/handlers/journal-mutations.js +6 -3
- package/dist/tools/handlers/roll-tables.js +3 -3
- package/dist/tools/handlers/scene-mutations.js +8 -3
- package/dist/tools/handlers/scenes.js +7 -2
- package/dist/tools/handlers/utils.js +7 -0
- package/dist/tools/handlers/world-documents.js +5 -5
- package/dist/utils/mcp-errors.js +25 -1
- package/package.json +2 -1
package/dist/foundry/auth.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import axios from 'axios';
|
|
11
11
|
import { io } from 'socket.io-client';
|
|
12
|
+
import { isRecord } from '../utils/guards.js';
|
|
12
13
|
import { logger } from '../utils/logger.js';
|
|
13
14
|
import { RefusedError } from '../utils/refusal.js';
|
|
14
15
|
/**
|
|
@@ -151,6 +152,44 @@ async function resolveUserId(baseUrl, user, session) {
|
|
|
151
152
|
logger.debug('Resolved user document _id', { displayName: user, _id: found._id });
|
|
152
153
|
return found._id;
|
|
153
154
|
}
|
|
155
|
+
/** A redirect path as FoundryVTT gives it, without its query or fragment. */
|
|
156
|
+
function redirectPath(value) {
|
|
157
|
+
if (typeof value !== 'string' || !value) {
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
return value.split(/[?#]/, 1)[0] || undefined;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Logs the HTTP status of a POST /join answer and where it redirects to,
|
|
164
|
+
* for diagnosis after a FoundryVTT update. Never the headers (cookie), the
|
|
165
|
+
* body or the request.
|
|
166
|
+
*/
|
|
167
|
+
function logJoinAnswer(response) {
|
|
168
|
+
if (typeof response.status !== 'number') {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const data = response.data;
|
|
172
|
+
const redirect = redirectPath(isRecord(data) ? data.redirect : undefined) ??
|
|
173
|
+
redirectPath(isRecord(response.headers) ? response.headers.location : undefined);
|
|
174
|
+
logger.info('POST /join answered', {
|
|
175
|
+
status: response.status,
|
|
176
|
+
...(redirect ? { redirect } : {}),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
/** `axios.post` that logs the status of the answer, including one axios throws on. */
|
|
180
|
+
async function postJoin(...args) {
|
|
181
|
+
try {
|
|
182
|
+
const response = await axios.post(...args);
|
|
183
|
+
logJoinAnswer(response);
|
|
184
|
+
return response;
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
if (isRecord(error) && isRecord(error.response)) {
|
|
188
|
+
logJoinAnswer(error.response);
|
|
189
|
+
}
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
154
193
|
/**
|
|
155
194
|
* Authenticates with FoundryVTT using the proven 4-step flow.
|
|
156
195
|
*
|
|
@@ -183,7 +222,7 @@ export async function authenticateFoundry(baseUrl, user, password) {
|
|
|
183
222
|
// Step 2: Resolve user to document _id
|
|
184
223
|
const userId = await resolveUserId(baseUrl, user, session);
|
|
185
224
|
// Step 3: POST /join as JSON with document _id
|
|
186
|
-
const joinRes = await
|
|
225
|
+
const joinRes = await postJoin(`${baseUrl}/join`, {
|
|
187
226
|
action: 'join',
|
|
188
227
|
userid: userId, // Foundry v13
|
|
189
228
|
userId, // Foundry v14 reads camelCase
|
package/dist/foundry/client.js
CHANGED
|
@@ -20,6 +20,7 @@ import { summarizeDocumentBroadcast, WorldEventLog } from './event-log.js';
|
|
|
20
20
|
import { JournalConflictFile } from './journal-conflicts.js';
|
|
21
21
|
import { JournalHistory } from './journal-history.js';
|
|
22
22
|
import { conflictAuthor, JOURNAL_CONFLICT_SPEAKER, JournalWatch, journalConflictMessage, journalContribution, journalVersion, OVERWRITTEN_BY_CONNECTOR, RECENT_AUTOSAVE_MS, } from './journal-integrity.js';
|
|
23
|
+
import { readSceneLighting, sceneLightingUpdate, } from './scene-lighting.js';
|
|
23
24
|
import { ACTIVE_EFFECT_MODES, OWNERSHIP_LEVELS, USER_ROLES, VISIBILITY_LEVELS } from './types.js';
|
|
24
25
|
import { applyDocumentBroadcast, applyUserActivity, mergePatch, parseDocumentBatch, parseDocumentBroadcast, parseUserActivity, } from './world-cache.js';
|
|
25
26
|
/** Who overwrote a page when an editor autosave did: FoundryVTT does not say. */
|
|
@@ -297,6 +298,27 @@ export function gamemasterAccountWarning(shared, account) {
|
|
|
297
298
|
'it: that page may become the active GM instead of a human Gamemaster (equal roles go to ' +
|
|
298
299
|
'the smaller user id) and run the game system automations. Make this account an Assistant GM.');
|
|
299
300
|
}
|
|
301
|
+
/** A Socket.IO request whose acknowledgement did not arrive within the timeout. */
|
|
302
|
+
class AckTimeoutError extends Error {
|
|
303
|
+
}
|
|
304
|
+
/** Response note of a deletion that only a re-read could confirm. */
|
|
305
|
+
export const DELETE_VERIFIED_NOTE = 'FoundryVTT deleted the document but its acknowledgement timed out; verified by re-reading it.';
|
|
306
|
+
/** Action, document type and id of a `modifyDocument` request, for the log. */
|
|
307
|
+
function describeAckRequest(payload) {
|
|
308
|
+
if (!isRecord(payload)) {
|
|
309
|
+
return {};
|
|
310
|
+
}
|
|
311
|
+
const operation = isRecord(payload.operation) ? payload.operation : {};
|
|
312
|
+
const first = (list) => (Array.isArray(list) ? list[0] : undefined);
|
|
313
|
+
const data = first(operation.data) ?? first(operation.updates);
|
|
314
|
+
const query = isRecord(operation.query) ? operation.query : {};
|
|
315
|
+
const id = first(operation.ids) ?? (isRecord(data) ? data._id : undefined) ?? query._id;
|
|
316
|
+
return {
|
|
317
|
+
...(typeof payload.action === 'string' ? { action: payload.action } : {}),
|
|
318
|
+
...(typeof payload.type === 'string' ? { type: payload.type } : {}),
|
|
319
|
+
...(typeof id === 'string' ? { id } : {}),
|
|
320
|
+
};
|
|
321
|
+
}
|
|
300
322
|
export class FoundryClient {
|
|
301
323
|
http;
|
|
302
324
|
socket = null;
|
|
@@ -341,6 +363,10 @@ export class FoundryClient {
|
|
|
341
363
|
outage = null;
|
|
342
364
|
/** Counts socket drops: a reload that saw one leaves the cache stale. */
|
|
343
365
|
socketDrops = 0;
|
|
366
|
+
/** When the server's last ping reached the socket; `null` since it connected. */
|
|
367
|
+
lastPingAt = null;
|
|
368
|
+
/** Socket.IO requests still waiting for their acknowledgement within their timeout. */
|
|
369
|
+
pendingAcks = 0;
|
|
344
370
|
/**
|
|
345
371
|
* Last observed outcome of a REST request: false once one failed to reach
|
|
346
372
|
* FoundryVTT at all (laurigates/foundryvtt-mcp#217). Unused in Socket.IO mode.
|
|
@@ -760,13 +786,22 @@ export class FoundryClient {
|
|
|
760
786
|
socket.on('userActivity', this.onUserActivity);
|
|
761
787
|
// Collaborative-editor autosaves bypass `modifyDocument` entirely.
|
|
762
788
|
socket.on('pm.autosave', this.onEditorAutosave);
|
|
789
|
+
// The server's pings, for the age logged when a request times out
|
|
790
|
+
// (test doubles have no manager).
|
|
791
|
+
this.lastPingAt = null;
|
|
792
|
+
socket.io?.on('ping', this.onServerPing);
|
|
763
793
|
}
|
|
764
794
|
detachDataListeners(socket) {
|
|
765
795
|
socket.off('modifyDocument', this.onDocumentBroadcast);
|
|
766
796
|
socket.off('modifyDocumentBatch', this.onDocumentBatchBroadcast);
|
|
767
797
|
socket.off('userActivity', this.onUserActivity);
|
|
768
798
|
socket.off('pm.autosave', this.onEditorAutosave);
|
|
799
|
+
socket.io?.off('ping', this.onServerPing);
|
|
769
800
|
}
|
|
801
|
+
/** Bound field rather than a method so the same reference reaches `off()`. */
|
|
802
|
+
onServerPing = () => {
|
|
803
|
+
this.lastPingAt = Date.now();
|
|
804
|
+
};
|
|
770
805
|
/** The handshake: FoundryVTT's first `session` event must name a user. */
|
|
771
806
|
waitForSessionUser(socket, timeoutMs, timeoutMessage) {
|
|
772
807
|
return new Promise((resolve, reject) => {
|
|
@@ -1121,6 +1156,7 @@ export class FoundryClient {
|
|
|
1121
1156
|
// Only the transport is back: FoundryVTT may not know the session any
|
|
1122
1157
|
// more (a server restart forgets every session). `onSessionEvent` decides.
|
|
1123
1158
|
this.awaitingSession = true;
|
|
1159
|
+
this.lastPingAt = null;
|
|
1124
1160
|
logger.debug('FoundryVTT socket transport reconnected — waiting for its session');
|
|
1125
1161
|
};
|
|
1126
1162
|
/**
|
|
@@ -2132,6 +2168,10 @@ export class FoundryClient {
|
|
|
2132
2168
|
* Emits a Socket.IO event with an acknowledgement callback, resolving the
|
|
2133
2169
|
* server's response and rejecting on timeout. Mirrors the ack pattern used by
|
|
2134
2170
|
* the `world` event in {@link connectAndLoadWorld}/{@link refreshWorldData}.
|
|
2171
|
+
*
|
|
2172
|
+
* The timeout error carries the age of the server's last ping, the socket's
|
|
2173
|
+
* state and the requests still waiting; an acknowledgement that arrives
|
|
2174
|
+
* after it is logged and otherwise ignored.
|
|
2135
2175
|
*/
|
|
2136
2176
|
emitWithAck(event, payload) {
|
|
2137
2177
|
return new Promise((resolve, reject) => {
|
|
@@ -2148,9 +2188,30 @@ export class FoundryClient {
|
|
|
2148
2188
|
return;
|
|
2149
2189
|
}
|
|
2150
2190
|
const timeoutMs = this.config.timeout || 10000;
|
|
2151
|
-
const
|
|
2191
|
+
const sentAt = Date.now();
|
|
2192
|
+
let timedOut = false;
|
|
2193
|
+
this.pendingAcks += 1;
|
|
2194
|
+
const timeout = setTimeout(() => {
|
|
2195
|
+
timedOut = true;
|
|
2196
|
+
const lastPing = this.lastPingAt === null
|
|
2197
|
+
? 'none since connect'
|
|
2198
|
+
: `${Date.now() - this.lastPingAt} ms ago`;
|
|
2199
|
+
reject(new AckTimeoutError(`Timeout waiting for '${event}' response (${timeoutMs}ms) ` +
|
|
2200
|
+
`[last ping: ${lastPing}; socket: ${socket.connected ? 'connected' : 'disconnected'}; ` +
|
|
2201
|
+
`pending requests: ${this.pendingAcks}]`));
|
|
2202
|
+
this.pendingAcks -= 1;
|
|
2203
|
+
}, timeoutMs);
|
|
2152
2204
|
socket.emit(event, payload, (response) => {
|
|
2205
|
+
if (timedOut) {
|
|
2206
|
+
logger.warn(`Late acknowledgement for '${event}' ignored`, {
|
|
2207
|
+
...describeAckRequest(payload),
|
|
2208
|
+
delayMs: Date.now() - sentAt,
|
|
2209
|
+
timeoutMs,
|
|
2210
|
+
});
|
|
2211
|
+
return;
|
|
2212
|
+
}
|
|
2153
2213
|
clearTimeout(timeout);
|
|
2214
|
+
this.pendingAcks -= 1;
|
|
2154
2215
|
resolve(response);
|
|
2155
2216
|
});
|
|
2156
2217
|
});
|
|
@@ -2212,6 +2273,63 @@ export class FoundryClient {
|
|
|
2212
2273
|
this.changeCache((worldData) => applyDocumentBroadcast(worldData, ownWrite));
|
|
2213
2274
|
return result;
|
|
2214
2275
|
}
|
|
2276
|
+
/**
|
|
2277
|
+
* Deletes one world document. When the acknowledgement times out, reads
|
|
2278
|
+
* the document back once over the socket instead of reporting a failure
|
|
2279
|
+
* FoundryVTT may not have had: gone means deleted (the cache is updated as
|
|
2280
|
+
* an acknowledgement would have); still there, or the read failing, keeps
|
|
2281
|
+
* the timeout error. Never sends the deletion again. A scene confirmed this
|
|
2282
|
+
* way also marks the cache stale: FoundryVTT deletes its combats with it,
|
|
2283
|
+
* and their deletion was never received.
|
|
2284
|
+
*
|
|
2285
|
+
* @returns a note for the response when only the re-read confirmed the deletion
|
|
2286
|
+
*/
|
|
2287
|
+
async deleteWorldDocument(type, id) {
|
|
2288
|
+
try {
|
|
2289
|
+
await this.modifyDocument(type, 'delete', { ids: [id] });
|
|
2290
|
+
return undefined;
|
|
2291
|
+
}
|
|
2292
|
+
catch (error) {
|
|
2293
|
+
if (!(error instanceof AckTimeoutError)) {
|
|
2294
|
+
throw error;
|
|
2295
|
+
}
|
|
2296
|
+
let exists;
|
|
2297
|
+
try {
|
|
2298
|
+
exists = await this.worldDocumentExists(type, id);
|
|
2299
|
+
}
|
|
2300
|
+
catch (readError) {
|
|
2301
|
+
const reason = readError instanceof Error ? readError.message : String(readError);
|
|
2302
|
+
throw new Error(`${error.message} (re-reading the document to check failed: ${reason})`);
|
|
2303
|
+
}
|
|
2304
|
+
if (exists) {
|
|
2305
|
+
throw error;
|
|
2306
|
+
}
|
|
2307
|
+
logger.warn('Deletion acknowledgement timed out, but re-reading shows the document is gone', {
|
|
2308
|
+
type,
|
|
2309
|
+
id,
|
|
2310
|
+
});
|
|
2311
|
+
this.changeCache((worldData) => applyDocumentBroadcast(worldData, { type, action: 'delete', result: [id] }));
|
|
2312
|
+
if (type === 'Scene') {
|
|
2313
|
+
this.markWorldDataStale(true, 'a scene deletion was only confirmed by re-reading it; its combats may still be cached');
|
|
2314
|
+
}
|
|
2315
|
+
return DELETE_VERIFIED_NOTE;
|
|
2316
|
+
}
|
|
2317
|
+
}
|
|
2318
|
+
/** Whether the server still has this world document (`modifyDocument` `get`). */
|
|
2319
|
+
async worldDocumentExists(type, id) {
|
|
2320
|
+
const response = await this.emitWithAck('modifyDocument', {
|
|
2321
|
+
type,
|
|
2322
|
+
action: 'get',
|
|
2323
|
+
operation: { query: { _id: id }, pack: null },
|
|
2324
|
+
});
|
|
2325
|
+
if (response?.error) {
|
|
2326
|
+
throw new Error(response.error.message || 'unknown error');
|
|
2327
|
+
}
|
|
2328
|
+
if (!Array.isArray(response?.result)) {
|
|
2329
|
+
throw new Error('unexpected answer');
|
|
2330
|
+
}
|
|
2331
|
+
return response.result.length > 0;
|
|
2332
|
+
}
|
|
2215
2333
|
// ==========================================================================
|
|
2216
2334
|
// Item mutation methods (WRITE — Socket.IO modifyDocument)
|
|
2217
2335
|
// ==========================================================================
|
|
@@ -2343,13 +2461,14 @@ export class FoundryClient {
|
|
|
2343
2461
|
* Ends (deletes) the active combat encounter (FR-018).
|
|
2344
2462
|
*
|
|
2345
2463
|
* @param combatId - 16-char alphanumeric Combat document id
|
|
2464
|
+
* @returns a note when only a re-read confirmed the deletion ({@link deleteWorldDocument})
|
|
2346
2465
|
*/
|
|
2347
2466
|
async endCombat(combatId) {
|
|
2348
2467
|
this.assertWriteable();
|
|
2349
2468
|
if (!FOUNDRY_ID_PATTERN.test(combatId)) {
|
|
2350
2469
|
throw new Error(`Invalid combatId format: ${combatId}`);
|
|
2351
2470
|
}
|
|
2352
|
-
|
|
2471
|
+
return this.deleteWorldDocument('Combat', combatId);
|
|
2353
2472
|
}
|
|
2354
2473
|
/**
|
|
2355
2474
|
* Sets a combatant's initiative (FR-018).
|
|
@@ -2835,7 +2954,7 @@ export class FoundryClient {
|
|
|
2835
2954
|
title: w.title || 'Unknown World',
|
|
2836
2955
|
description: w.description || '',
|
|
2837
2956
|
system: s.id || 'unknown',
|
|
2838
|
-
coreVersion: r
|
|
2957
|
+
coreVersion: coreVersionLabel(r),
|
|
2839
2958
|
systemVersion: s.version || 'unknown',
|
|
2840
2959
|
playtime: 0,
|
|
2841
2960
|
created: new Date().toISOString(),
|
|
@@ -3539,25 +3658,27 @@ export class FoundryClient {
|
|
|
3539
3658
|
* Permanently deletes a top-level Actor document (mirrors {@link createWorldActor}).
|
|
3540
3659
|
*
|
|
3541
3660
|
* @param actorId - 16-char alphanumeric Actor document id to delete
|
|
3661
|
+
* @returns a note when only a re-read confirmed the deletion ({@link deleteWorldDocument})
|
|
3542
3662
|
*/
|
|
3543
3663
|
async deleteWorldActor(actorId) {
|
|
3544
3664
|
this.assertWriteable();
|
|
3545
3665
|
if (!FOUNDRY_ID_PATTERN.test(actorId)) {
|
|
3546
3666
|
throw new Error(`Invalid actorId format: ${actorId}`);
|
|
3547
3667
|
}
|
|
3548
|
-
|
|
3668
|
+
return this.deleteWorldDocument('Actor', actorId);
|
|
3549
3669
|
}
|
|
3550
3670
|
/**
|
|
3551
3671
|
* Permanently deletes a Scene document (mirrors {@link switchScene}).
|
|
3552
3672
|
*
|
|
3553
3673
|
* @param sceneId - 16-char alphanumeric Scene document id to delete
|
|
3674
|
+
* @returns a note when only a re-read confirmed the deletion ({@link deleteWorldDocument})
|
|
3554
3675
|
*/
|
|
3555
3676
|
async deleteScene(sceneId) {
|
|
3556
3677
|
this.assertWriteable();
|
|
3557
3678
|
if (!FOUNDRY_ID_PATTERN.test(sceneId)) {
|
|
3558
3679
|
throw new Error(`Invalid sceneId format: ${sceneId}`);
|
|
3559
3680
|
}
|
|
3560
|
-
|
|
3681
|
+
return this.deleteWorldDocument('Scene', sceneId);
|
|
3561
3682
|
}
|
|
3562
3683
|
/**
|
|
3563
3684
|
* Updates document ownership permissions for users.
|
|
@@ -3607,13 +3728,14 @@ export class FoundryClient {
|
|
|
3607
3728
|
* Permanently deletes a JournalEntry document (mirrors {@link createJournalEntry}).
|
|
3608
3729
|
*
|
|
3609
3730
|
* @param journalId - 16-char alphanumeric JournalEntry document id to delete
|
|
3731
|
+
* @returns a note when only a re-read confirmed the deletion ({@link deleteWorldDocument})
|
|
3610
3732
|
*/
|
|
3611
3733
|
async deleteJournalEntry(journalId) {
|
|
3612
3734
|
this.assertWriteable();
|
|
3613
3735
|
if (!FOUNDRY_ID_PATTERN.test(journalId)) {
|
|
3614
3736
|
throw new Error(`Invalid journalId format: ${journalId}`);
|
|
3615
3737
|
}
|
|
3616
|
-
|
|
3738
|
+
return this.deleteWorldDocument('JournalEntry', journalId);
|
|
3617
3739
|
}
|
|
3618
3740
|
// ==========================================================================
|
|
3619
3741
|
// Roll tables (read + local weighted draw)
|
|
@@ -3741,7 +3863,7 @@ export class FoundryClient {
|
|
|
3741
3863
|
if (!FOUNDRY_ID_PATTERN.test(tableId)) {
|
|
3742
3864
|
throw new Error(`Invalid tableId format: ${tableId}`);
|
|
3743
3865
|
}
|
|
3744
|
-
|
|
3866
|
+
return this.deleteWorldDocument('RollTable', tableId);
|
|
3745
3867
|
}
|
|
3746
3868
|
// ==========================================================================
|
|
3747
3869
|
// Folder methods
|
|
@@ -3851,7 +3973,7 @@ export class FoundryClient {
|
|
|
3851
3973
|
if (!FOUNDRY_ID_PATTERN.test(macroId)) {
|
|
3852
3974
|
throw new Error(`Invalid macroId format: ${macroId}`);
|
|
3853
3975
|
}
|
|
3854
|
-
|
|
3976
|
+
return this.deleteWorldDocument('Macro', macroId);
|
|
3855
3977
|
}
|
|
3856
3978
|
// ==========================================================================
|
|
3857
3979
|
// Playlist methods
|
|
@@ -3964,7 +4086,7 @@ export class FoundryClient {
|
|
|
3964
4086
|
if (!FOUNDRY_ID_PATTERN.test(playlistId)) {
|
|
3965
4087
|
throw new Error(`Invalid playlistId format: ${playlistId}`);
|
|
3966
4088
|
}
|
|
3967
|
-
|
|
4089
|
+
return this.deleteWorldDocument('Playlist', playlistId);
|
|
3968
4090
|
}
|
|
3969
4091
|
// ==========================================================================
|
|
3970
4092
|
// World settings methods
|
|
@@ -4092,9 +4214,9 @@ export class FoundryClient {
|
|
|
4092
4214
|
// Scene lighting mutation methods (WRITE — Socket.IO modifyDocument)
|
|
4093
4215
|
// ==========================================================================
|
|
4094
4216
|
/**
|
|
4095
|
-
* Sets a scene's ambient darkness level and/or global illumination fields
|
|
4096
|
-
*
|
|
4097
|
-
*
|
|
4217
|
+
* Sets a scene's ambient darkness level and/or global illumination fields,
|
|
4218
|
+
* under the `Scene` document's `environment` (see scene-lighting.ts) — no
|
|
4219
|
+
* canvas access required.
|
|
4098
4220
|
*
|
|
4099
4221
|
* @param sceneId - 16-char alphanumeric Scene document id
|
|
4100
4222
|
* @param options - at least one of darkness (0-1), globalLight, globalLightThreshold
|
|
@@ -4108,16 +4230,7 @@ export class FoundryClient {
|
|
|
4108
4230
|
(!Number.isFinite(options.darkness) || options.darkness < 0 || options.darkness > 1)) {
|
|
4109
4231
|
throw new Error(`Invalid darkness: ${options.darkness} (must be a number between 0 and 1)`);
|
|
4110
4232
|
}
|
|
4111
|
-
const update = { _id: sceneId };
|
|
4112
|
-
if (options.darkness !== undefined) {
|
|
4113
|
-
update.darkness = options.darkness;
|
|
4114
|
-
}
|
|
4115
|
-
if (options.globalLight !== undefined) {
|
|
4116
|
-
update.globalLight = options.globalLight;
|
|
4117
|
-
}
|
|
4118
|
-
if (options.globalLightThreshold !== undefined) {
|
|
4119
|
-
update.globalLightThreshold = options.globalLightThreshold;
|
|
4120
|
-
}
|
|
4233
|
+
const update = { _id: sceneId, ...sceneLightingUpdate(options) };
|
|
4121
4234
|
if (Object.keys(update).length === 1) {
|
|
4122
4235
|
throw new Error('At least one of darkness, globalLight, globalLightThreshold is required');
|
|
4123
4236
|
}
|
|
@@ -6017,6 +6130,21 @@ function worldActorToFoundry(a) {
|
|
|
6017
6130
|
}
|
|
6018
6131
|
return actor;
|
|
6019
6132
|
}
|
|
6133
|
+
/**
|
|
6134
|
+
* The core version from the world's `release`. FoundryVTT sends its
|
|
6135
|
+
* ReleaseData without the `version` getter (`<generation>.<build>`), so it is
|
|
6136
|
+
* rebuilt from `generation` and `build`; a bare `version` is used as it is.
|
|
6137
|
+
*/
|
|
6138
|
+
function coreVersionLabel(release) {
|
|
6139
|
+
const { generation, build, version } = release ?? {};
|
|
6140
|
+
if (typeof generation === 'number' && typeof build === 'number') {
|
|
6141
|
+
return `${generation}.${build} (generation ${generation}, build ${build})`;
|
|
6142
|
+
}
|
|
6143
|
+
if (typeof version === 'string' && version) {
|
|
6144
|
+
return version;
|
|
6145
|
+
}
|
|
6146
|
+
return typeof generation === 'number' ? String(generation) : 'unknown';
|
|
6147
|
+
}
|
|
6020
6148
|
function worldSceneToFoundry(s) {
|
|
6021
6149
|
const scene = {
|
|
6022
6150
|
_id: s._id,
|
|
@@ -6028,8 +6156,7 @@ function worldSceneToFoundry(s) {
|
|
|
6028
6156
|
padding: s.padding,
|
|
6029
6157
|
shiftX: 0,
|
|
6030
6158
|
shiftY: 0,
|
|
6031
|
-
|
|
6032
|
-
darkness: s.darkness,
|
|
6159
|
+
...readSceneLighting(s),
|
|
6033
6160
|
};
|
|
6034
6161
|
if (s.img) {
|
|
6035
6162
|
scene.img = s.img;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview A scene's darkness and global light.
|
|
3
|
+
*
|
|
4
|
+
* Since v12, FoundryVTT keeps them under `environment`: `darknessLevel`,
|
|
5
|
+
* `globalLight.enabled` and `globalLight.darkness.max` (the darkness above
|
|
6
|
+
* which the global light goes out). It migrated the top-level `darkness`,
|
|
7
|
+
* `globalLight` and `globalLightThreshold` there when loading a document,
|
|
8
|
+
* and v14 (14.370) drops them from an update. They are read only as a
|
|
9
|
+
* fallback, for a payload that still carries them.
|
|
10
|
+
*/
|
|
11
|
+
import { isRecord } from '../utils/guards.js';
|
|
12
|
+
/** The darkness and global light of a scene, from `environment` or the older fields. */
|
|
13
|
+
export function readSceneLighting(scene) {
|
|
14
|
+
const environment = isRecord(scene.environment) ? scene.environment : {};
|
|
15
|
+
const light = isRecord(environment.globalLight) ? environment.globalLight : {};
|
|
16
|
+
const darkness = typeof environment.darknessLevel === 'number' ? environment.darknessLevel : scene.darkness;
|
|
17
|
+
const globalLight = typeof light.enabled === 'boolean' ? light.enabled : scene.globalLight;
|
|
18
|
+
return {
|
|
19
|
+
...(typeof darkness === 'number' && Number.isFinite(darkness) ? { darkness } : {}),
|
|
20
|
+
...(typeof globalLight === 'boolean' ? { globalLight } : {}),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** The Scene update fields for a lighting change, under `environment`. */
|
|
24
|
+
export function sceneLightingUpdate(change) {
|
|
25
|
+
const globalLight = {};
|
|
26
|
+
if (change.globalLight !== undefined) {
|
|
27
|
+
globalLight.enabled = change.globalLight;
|
|
28
|
+
}
|
|
29
|
+
if (change.globalLightThreshold !== undefined) {
|
|
30
|
+
globalLight.darkness = { max: change.globalLightThreshold };
|
|
31
|
+
}
|
|
32
|
+
const environment = {};
|
|
33
|
+
if (change.darkness !== undefined) {
|
|
34
|
+
environment.darknessLevel = change.darkness;
|
|
35
|
+
}
|
|
36
|
+
if (Object.keys(globalLight).length > 0) {
|
|
37
|
+
environment.globalLight = globalLight;
|
|
38
|
+
}
|
|
39
|
+
return Object.keys(environment).length > 0 ? { environment } : {};
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=scene-lighting.js.map
|
package/dist/mcp-server.js
CHANGED
|
@@ -14,7 +14,7 @@ import { packageVersion } from './shared/version.js';
|
|
|
14
14
|
import { getAllTools } from './tools/definitions.js';
|
|
15
15
|
import { getAllResources } from './tools/resources.js';
|
|
16
16
|
import { logger } from './utils/logger.js';
|
|
17
|
-
import { withBareErrors } from './utils/mcp-errors.js';
|
|
17
|
+
import { toolErrorResult, withBareErrors } from './utils/mcp-errors.js';
|
|
18
18
|
export function createMcpServer(backend) {
|
|
19
19
|
// Answers `initialize` even with no usable configuration yet: tool calls
|
|
20
20
|
// then say what is missing (see the relay and the all-in-one server).
|
|
@@ -42,13 +42,20 @@ export function createMcpServer(backend) {
|
|
|
42
42
|
resources: getAllResources(),
|
|
43
43
|
};
|
|
44
44
|
});
|
|
45
|
-
// Handle tool calls.
|
|
46
|
-
//
|
|
45
|
+
// Handle tool calls. A failure is an isError result carrying one
|
|
46
|
+
// "MCP error <code>: " prefix; an unknown tool stays a JSON-RPC error
|
|
47
|
+
// (see toolErrorResult).
|
|
47
48
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
48
49
|
const { name, arguments: args } = request.params;
|
|
49
|
-
|
|
50
|
+
try {
|
|
51
|
+
return await backend.callTool(name, args || {});
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
return toolErrorResult(error);
|
|
55
|
+
}
|
|
50
56
|
});
|
|
51
|
-
// Handle resource reads
|
|
57
|
+
// Handle resource reads. Errors leave with their bare message: the client
|
|
58
|
+
// adds the "MCP error <code>: " prefix itself (see withBareErrors).
|
|
52
59
|
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
|
|
53
60
|
return withBareErrors(() => backend.readResource(request.params.uri));
|
|
54
61
|
});
|