@g33ks/foundryvtt-mcp 0.0.0-stage β 2.1.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 +457 -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 +414 -0
- package/dist/cli/prompt.js +117 -0
- package/dist/cli/snippets.js +64 -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 +481 -0
- package/dist/diagnostics/client.js +291 -0
- package/dist/diagnostics/types.js +94 -0
- package/dist/foundry/auth.js +224 -0
- package/dist/foundry/browser-detect.js +170 -0
- package/dist/foundry/client.js +5874 -0
- package/dist/foundry/dice-formula.js +154 -0
- package/dist/foundry/event-log.js +298 -0
- package/dist/foundry/headless-gm-session.js +857 -0
- package/dist/foundry/journal-conflicts.js +141 -0
- package/dist/foundry/journal-history.js +90 -0
- package/dist/foundry/journal-integrity.js +322 -0
- package/dist/foundry/message-author.js +19 -0
- package/dist/foundry/module-bridge.js +441 -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 +65 -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 +291 -0
- package/dist/shared/daemon-link.js +135 -0
- package/dist/shared/daemon.js +311 -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 +38 -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 +450 -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 +48 -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/tool-dependencies.js +194 -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/mcp-errors.js +47 -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
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Canvas-only tool handlers, relayed through the companion
|
|
3
|
+
* Foundry module over {@link ModuleBridge} rather than FoundryVTT's own
|
|
4
|
+
* Socket.IO document API β see module-bridge.ts for why.
|
|
5
|
+
*/
|
|
6
|
+
import { readFile } from 'node:fs/promises';
|
|
7
|
+
import { extname } from 'node:path';
|
|
8
|
+
import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';
|
|
9
|
+
import { withToolError } from './utils.js';
|
|
10
|
+
function isCaptureSceneResult(value) {
|
|
11
|
+
return (typeof value === 'object' &&
|
|
12
|
+
value !== null &&
|
|
13
|
+
typeof value.image === 'string' &&
|
|
14
|
+
typeof value.mimeType === 'string');
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Handles capturing a screenshot of the current scene, with a grid
|
|
18
|
+
* coordinate overlay for spatial reasoning. Requires the companion Foundry
|
|
19
|
+
* module to be installed, enabled, and connected to the bridge.
|
|
20
|
+
*/
|
|
21
|
+
export async function handleCaptureScene(_args, moduleBridge) {
|
|
22
|
+
if (!moduleBridge) {
|
|
23
|
+
throw new McpError(ErrorCode.InvalidRequest, 'The module bridge is disabled. Set FOUNDRY_MODULE_BRIDGE_ENABLED=true and install the companion Foundry module.');
|
|
24
|
+
}
|
|
25
|
+
return withToolError('capture scene', async () => {
|
|
26
|
+
const result = await moduleBridge.send('capture_scene', {});
|
|
27
|
+
if (!isCaptureSceneResult(result)) {
|
|
28
|
+
throw new McpError(ErrorCode.InternalError, 'Module returned an unexpected capture_scene shape');
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
content: [
|
|
32
|
+
{
|
|
33
|
+
type: 'text',
|
|
34
|
+
text: `π· **Scene Captured: ${result.sceneName}**\n**Dimensions:** ${result.width}x${result.height}`,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
type: 'image',
|
|
38
|
+
data: result.image,
|
|
39
|
+
mimeType: result.mimeType,
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function isDocumentSchemaResult(value) {
|
|
46
|
+
return (typeof value === 'object' &&
|
|
47
|
+
value !== null &&
|
|
48
|
+
typeof value.documentType === 'string' &&
|
|
49
|
+
typeof value.type === 'string' &&
|
|
50
|
+
typeof value.fields === 'object');
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Handles introspecting a game system's real DataModel schema for an Actor
|
|
54
|
+
* or Item type β the exact field names, types, choices, and defaults
|
|
55
|
+
* `create_world_actor`/`create_actor_item`/`create_full_actor` expect,
|
|
56
|
+
* for whichever system is installed. Requires the companion Foundry module.
|
|
57
|
+
*/
|
|
58
|
+
export async function handleGetDocumentSchema(args, moduleBridge) {
|
|
59
|
+
const { documentType, type } = args;
|
|
60
|
+
if (documentType !== 'Actor' && documentType !== 'Item') {
|
|
61
|
+
throw new McpError(ErrorCode.InvalidParams, 'documentType must be "Actor" or "Item"');
|
|
62
|
+
}
|
|
63
|
+
if (!type || typeof type !== 'string') {
|
|
64
|
+
throw new McpError(ErrorCode.InvalidParams, 'type is required and must be a string');
|
|
65
|
+
}
|
|
66
|
+
if (!moduleBridge) {
|
|
67
|
+
throw new McpError(ErrorCode.InvalidRequest, 'The module bridge is disabled. Set FOUNDRY_MODULE_BRIDGE_ENABLED=true and install the companion Foundry module.');
|
|
68
|
+
}
|
|
69
|
+
return withToolError('get document schema', async () => {
|
|
70
|
+
const result = await moduleBridge.send('get_document_schema', { documentType, type });
|
|
71
|
+
if (!isDocumentSchemaResult(result)) {
|
|
72
|
+
throw new McpError(ErrorCode.InternalError, 'Module returned an unexpected get_document_schema shape');
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
content: [
|
|
76
|
+
{
|
|
77
|
+
type: 'text',
|
|
78
|
+
text: `𧬠**${result.documentType} Schema: ${result.type}**\n\`\`\`json\n${JSON.stringify(result.fields, null, 2)}\n\`\`\``,
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function isSearchCompendiumContentResult(value) {
|
|
85
|
+
return (typeof value === 'object' &&
|
|
86
|
+
value !== null &&
|
|
87
|
+
Array.isArray(value.results));
|
|
88
|
+
}
|
|
89
|
+
export async function handleSearchCompendiumContent(args, moduleBridge) {
|
|
90
|
+
if (!moduleBridge) {
|
|
91
|
+
throw new McpError(ErrorCode.InvalidRequest, 'The module bridge is disabled. Set FOUNDRY_MODULE_BRIDGE_ENABLED=true and install the companion Foundry module.');
|
|
92
|
+
}
|
|
93
|
+
return withToolError('search compendium content', async () => {
|
|
94
|
+
const result = await moduleBridge.send('search_compendium_content', args);
|
|
95
|
+
if (!isSearchCompendiumContentResult(result)) {
|
|
96
|
+
throw new McpError(ErrorCode.InternalError, 'Module returned an unexpected search_compendium_content shape');
|
|
97
|
+
}
|
|
98
|
+
if (result.results.length === 0) {
|
|
99
|
+
return {
|
|
100
|
+
content: [
|
|
101
|
+
{ type: 'text', text: `π **No compendium matches found for "${args.query ?? ''}".**` },
|
|
102
|
+
],
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const lines = result.results.map((r) => {
|
|
106
|
+
const matchTag = r.matchedIn === 'content' ? ' [content match]' : '';
|
|
107
|
+
const snippetText = r.snippet ? `\n > "${r.snippet}"` : '';
|
|
108
|
+
return `- **${r.documentName}** (\`${r.documentId}\`) in _${r.packLabel}_ (\`${r.packId}\`)${matchTag}${snippetText}`;
|
|
109
|
+
});
|
|
110
|
+
return {
|
|
111
|
+
content: [
|
|
112
|
+
{
|
|
113
|
+
type: 'text',
|
|
114
|
+
text: `π **Compendium Content Matches** (${result.results.length})\n${lines.join('\n')}`,
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
};
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
function isRollAndPostResult(value) {
|
|
121
|
+
return (typeof value === 'object' &&
|
|
122
|
+
value !== null &&
|
|
123
|
+
typeof value.formula === 'string' &&
|
|
124
|
+
typeof value.total === 'number');
|
|
125
|
+
}
|
|
126
|
+
export async function handleRollAndPost(args, moduleBridge) {
|
|
127
|
+
const { formula } = args;
|
|
128
|
+
if (!formula || typeof formula !== 'string') {
|
|
129
|
+
throw new McpError(ErrorCode.InvalidParams, 'formula is required and must be a string');
|
|
130
|
+
}
|
|
131
|
+
if (!moduleBridge) {
|
|
132
|
+
throw new McpError(ErrorCode.InvalidRequest, 'The module bridge is disabled. Set FOUNDRY_MODULE_BRIDGE_ENABLED=true and install the companion Foundry module.');
|
|
133
|
+
}
|
|
134
|
+
return withToolError('roll and post', async () => {
|
|
135
|
+
const result = await moduleBridge.send('roll_and_post', args);
|
|
136
|
+
if (!isRollAndPostResult(result)) {
|
|
137
|
+
throw new McpError(ErrorCode.InternalError, 'Module returned an unexpected roll_and_post shape');
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
content: [
|
|
141
|
+
{
|
|
142
|
+
type: 'text',
|
|
143
|
+
text: `π² **Dice Rolled & Posted to Chat**\n**Formula:** ${result.formula}\n**Total:** ${result.total}${args.flavor ? `\n**Flavor:** ${args.flavor}` : ''}`,
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Handles importing a compendium Actor (with its embedded items/effects) as
|
|
151
|
+
* a new top-level world Actor β the gap `spawn_token` cannot close on its
|
|
152
|
+
* own, since it requires an actor that already exists in the world.
|
|
153
|
+
*
|
|
154
|
+
* Best-effort on embedded documents: if the system rejects an Actor create
|
|
155
|
+
* carrying `items`/`effects` inline, the actor is recreated without them and
|
|
156
|
+
* each item is seeded individually via `createActorItem`, mirroring
|
|
157
|
+
* `createFullActor`'s per-item error reporting rather than losing the whole
|
|
158
|
+
* import over one bad item.
|
|
159
|
+
*/
|
|
160
|
+
export async function handleImportCompendiumActor(args, foundryClient, moduleBridge) {
|
|
161
|
+
const { compendiumId, actorId, folderId, name } = args;
|
|
162
|
+
if (!compendiumId || typeof compendiumId !== 'string') {
|
|
163
|
+
throw new McpError(ErrorCode.InvalidParams, 'compendiumId is required and must be a string');
|
|
164
|
+
}
|
|
165
|
+
if (!actorId || typeof actorId !== 'string') {
|
|
166
|
+
throw new McpError(ErrorCode.InvalidParams, 'actorId is required and must be a string');
|
|
167
|
+
}
|
|
168
|
+
if (!moduleBridge) {
|
|
169
|
+
throw new McpError(ErrorCode.InvalidRequest, 'The module bridge is disabled. Set FOUNDRY_MODULE_BRIDGE_ENABLED=true and install the companion Foundry module.');
|
|
170
|
+
}
|
|
171
|
+
return withToolError('import compendium actor', async () => {
|
|
172
|
+
const rawDoc = (await moduleBridge.send('get_compendium_document', {
|
|
173
|
+
packId: compendiumId,
|
|
174
|
+
documentId: actorId,
|
|
175
|
+
}));
|
|
176
|
+
if (typeof rawDoc.name !== 'string' || !rawDoc.name) {
|
|
177
|
+
throw new McpError(ErrorCode.InvalidParams, `Compendium document ${compendiumId}/${actorId} has no usable name`);
|
|
178
|
+
}
|
|
179
|
+
if (typeof rawDoc.type !== 'string' || !rawDoc.type) {
|
|
180
|
+
throw new McpError(ErrorCode.InvalidParams, `Compendium document ${compendiumId}/${actorId} has no usable type`);
|
|
181
|
+
}
|
|
182
|
+
const overrides = {};
|
|
183
|
+
if (name) {
|
|
184
|
+
overrides.name = name;
|
|
185
|
+
}
|
|
186
|
+
if (folderId) {
|
|
187
|
+
overrides.folder = folderId;
|
|
188
|
+
}
|
|
189
|
+
const rawItems = Array.isArray(rawDoc.items) ? rawDoc.items : [];
|
|
190
|
+
const itemErrors = [];
|
|
191
|
+
let seededItems = rawItems.length;
|
|
192
|
+
let fellBack = false;
|
|
193
|
+
const actor = await foundryClient
|
|
194
|
+
.createActorFromData(rawDoc, overrides)
|
|
195
|
+
.catch(async (error) => {
|
|
196
|
+
if (rawItems.length === 0) {
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
fellBack = true;
|
|
200
|
+
seededItems = 0;
|
|
201
|
+
return foundryClient.createActorFromData(rawDoc, { ...overrides, includeEmbedded: false });
|
|
202
|
+
});
|
|
203
|
+
if (fellBack) {
|
|
204
|
+
for (const seed of rawItems) {
|
|
205
|
+
const itemRecord = typeof seed === 'object' && seed !== null ? { ...seed } : {};
|
|
206
|
+
delete itemRecord._id;
|
|
207
|
+
delete itemRecord._stats;
|
|
208
|
+
const itemName = typeof itemRecord.name === 'string' ? itemRecord.name : 'Unnamed item';
|
|
209
|
+
try {
|
|
210
|
+
await foundryClient.createActorItem(actor._id, { type: 'inline', item: itemRecord });
|
|
211
|
+
seededItems++;
|
|
212
|
+
}
|
|
213
|
+
catch (itemError) {
|
|
214
|
+
itemErrors.push({
|
|
215
|
+
name: itemName,
|
|
216
|
+
error: itemError instanceof Error ? itemError.message : String(itemError),
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const lines = [
|
|
222
|
+
'π¦ **Actor Imported**',
|
|
223
|
+
`**Name:** ${actor.name}`,
|
|
224
|
+
`**ID:** ${actor._id}`,
|
|
225
|
+
`**Source:** compendium ${compendiumId} / ${actorId}`,
|
|
226
|
+
`**Items:** ${seededItems}/${rawItems.length}`,
|
|
227
|
+
];
|
|
228
|
+
if (fellBack) {
|
|
229
|
+
lines.push('_The system rejected embedded items on create; items were seeded individually._');
|
|
230
|
+
}
|
|
231
|
+
if (itemErrors.length > 0) {
|
|
232
|
+
lines.push('', '**Item errors:**', ...itemErrors.map((e) => `- ${e.name}: ${e.error}`));
|
|
233
|
+
}
|
|
234
|
+
return { content: [{ type: 'text', text: lines.join('\n') }] };
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
function isSetTargetResult(value) {
|
|
238
|
+
return (typeof value === 'object' &&
|
|
239
|
+
value !== null &&
|
|
240
|
+
typeof value.targeted === 'boolean' &&
|
|
241
|
+
typeof value.count === 'number');
|
|
242
|
+
}
|
|
243
|
+
export async function handleSetTarget(args, moduleBridge) {
|
|
244
|
+
if (!moduleBridge) {
|
|
245
|
+
throw new McpError(ErrorCode.InvalidRequest, 'The module bridge is disabled. Set FOUNDRY_MODULE_BRIDGE_ENABLED=true and install the companion Foundry module.');
|
|
246
|
+
}
|
|
247
|
+
return withToolError('set target', async () => {
|
|
248
|
+
const result = await moduleBridge.send('set_target', args);
|
|
249
|
+
if (!isSetTargetResult(result)) {
|
|
250
|
+
throw new McpError(ErrorCode.InternalError, 'Module returned an unexpected set_target shape');
|
|
251
|
+
}
|
|
252
|
+
const label = result.targeted ? 'Targeted' : 'Un-targeted';
|
|
253
|
+
const tokenList = result.tokens.length > 0 ? ` (${result.tokens.join(', ')})` : '';
|
|
254
|
+
return {
|
|
255
|
+
content: [
|
|
256
|
+
{
|
|
257
|
+
type: 'text',
|
|
258
|
+
text: `π― **${label} ${result.count} Token${result.count === 1 ? '' : 's'}**${tokenList}`,
|
|
259
|
+
},
|
|
260
|
+
],
|
|
261
|
+
};
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
function isGetTargetsResult(value) {
|
|
265
|
+
return (typeof value === 'object' && value !== null && Array.isArray(value.users));
|
|
266
|
+
}
|
|
267
|
+
export async function handleGetTargets(_args, moduleBridge) {
|
|
268
|
+
if (!moduleBridge) {
|
|
269
|
+
throw new McpError(ErrorCode.InvalidRequest, 'The module bridge is disabled. Set FOUNDRY_MODULE_BRIDGE_ENABLED=true and install the companion Foundry module.');
|
|
270
|
+
}
|
|
271
|
+
return withToolError('get targets', async () => {
|
|
272
|
+
const result = await moduleBridge.send('get_targets', {});
|
|
273
|
+
if (!isGetTargetsResult(result)) {
|
|
274
|
+
throw new McpError(ErrorCode.InternalError, 'Module returned an unexpected get_targets shape');
|
|
275
|
+
}
|
|
276
|
+
if (result.users.length === 0) {
|
|
277
|
+
return {
|
|
278
|
+
content: [{ type: 'text', text: 'π― **No tokens are currently targeted by any user.**' }],
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
const lines = result.users.map((u) => `- **${u.userName}** (\`${u.userId}\`): ${u.tokenNames.join(', ') || 'none'}`);
|
|
282
|
+
return {
|
|
283
|
+
content: [{ type: 'text', text: `π― **Current Target Selections**\n${lines.join('\n')}` }],
|
|
284
|
+
};
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
function isPingCanvasResult(value) {
|
|
288
|
+
return (typeof value === 'object' &&
|
|
289
|
+
value !== null &&
|
|
290
|
+
typeof value.x === 'number' &&
|
|
291
|
+
typeof value.y === 'number');
|
|
292
|
+
}
|
|
293
|
+
export async function handlePingCanvas(args, moduleBridge) {
|
|
294
|
+
if (typeof args.x !== 'number' || typeof args.y !== 'number') {
|
|
295
|
+
throw new McpError(ErrorCode.InvalidParams, 'x and y coordinates are required and must be numbers');
|
|
296
|
+
}
|
|
297
|
+
if (!moduleBridge) {
|
|
298
|
+
throw new McpError(ErrorCode.InvalidRequest, 'The module bridge is disabled. Set FOUNDRY_MODULE_BRIDGE_ENABLED=true and install the companion Foundry module.');
|
|
299
|
+
}
|
|
300
|
+
return withToolError('ping canvas', async () => {
|
|
301
|
+
const result = await moduleBridge.send('ping_canvas', args);
|
|
302
|
+
if (!isPingCanvasResult(result)) {
|
|
303
|
+
throw new McpError(ErrorCode.InternalError, 'Module returned an unexpected ping_canvas shape');
|
|
304
|
+
}
|
|
305
|
+
return {
|
|
306
|
+
content: [
|
|
307
|
+
{
|
|
308
|
+
type: 'text',
|
|
309
|
+
text: `π **Canvas Pinged at (${result.x}, ${result.y})**${result.sceneId ? ` on scene \`${result.sceneId}\`` : ''}`,
|
|
310
|
+
},
|
|
311
|
+
],
|
|
312
|
+
};
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
function isSetPauseResult(value) {
|
|
316
|
+
return (typeof value === 'object' &&
|
|
317
|
+
value !== null &&
|
|
318
|
+
typeof value.paused === 'boolean');
|
|
319
|
+
}
|
|
320
|
+
export async function handleSetPause(args, moduleBridge) {
|
|
321
|
+
if (!moduleBridge) {
|
|
322
|
+
throw new McpError(ErrorCode.InvalidRequest, 'The module bridge is disabled. Set FOUNDRY_MODULE_BRIDGE_ENABLED=true and install the companion Foundry module.');
|
|
323
|
+
}
|
|
324
|
+
return withToolError('set pause', async () => {
|
|
325
|
+
const result = await moduleBridge.send('set_pause', args);
|
|
326
|
+
if (!isSetPauseResult(result)) {
|
|
327
|
+
throw new McpError(ErrorCode.InternalError, 'Module returned an unexpected set_pause shape');
|
|
328
|
+
}
|
|
329
|
+
return {
|
|
330
|
+
content: [
|
|
331
|
+
{
|
|
332
|
+
type: 'text',
|
|
333
|
+
text: result.paused ? 'βΈοΈ **Game Paused**' : 'βΆοΈ **Game Resumed**',
|
|
334
|
+
},
|
|
335
|
+
],
|
|
336
|
+
};
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
function isUploadAssetResult(value) {
|
|
340
|
+
return (typeof value === 'object' &&
|
|
341
|
+
value !== null &&
|
|
342
|
+
typeof value.path === 'string');
|
|
343
|
+
}
|
|
344
|
+
const MIME_EXTENSIONS = {
|
|
345
|
+
'.png': 'image/png',
|
|
346
|
+
'.jpg': 'image/jpeg',
|
|
347
|
+
'.jpeg': 'image/jpeg',
|
|
348
|
+
'.webp': 'image/webp',
|
|
349
|
+
'.svg': 'image/svg+xml',
|
|
350
|
+
'.gif': 'image/gif',
|
|
351
|
+
'.mp3': 'audio/mpeg',
|
|
352
|
+
'.ogg': 'audio/ogg',
|
|
353
|
+
'.wav': 'audio/wav',
|
|
354
|
+
'.m4a': 'audio/mp4',
|
|
355
|
+
'.webm': 'video/webm',
|
|
356
|
+
'.mp4': 'video/mp4',
|
|
357
|
+
'.json': 'application/json',
|
|
358
|
+
'.pdf': 'application/pdf',
|
|
359
|
+
};
|
|
360
|
+
const MAX_UPLOAD_BYTES = 8 * 1024 * 1024; // 8 MiB
|
|
361
|
+
export async function handleUploadAsset(args, moduleBridge) {
|
|
362
|
+
const { targetDir, filename, contentBase64, sourcePath } = args;
|
|
363
|
+
if (!targetDir || typeof targetDir !== 'string') {
|
|
364
|
+
throw new McpError(ErrorCode.InvalidParams, 'targetDir is required and must be a string');
|
|
365
|
+
}
|
|
366
|
+
if (!filename || typeof filename !== 'string') {
|
|
367
|
+
throw new McpError(ErrorCode.InvalidParams, 'filename is required and must be a string');
|
|
368
|
+
}
|
|
369
|
+
const hasBase64 = typeof contentBase64 === 'string' && contentBase64.length > 0;
|
|
370
|
+
const hasSource = typeof sourcePath === 'string' && sourcePath.length > 0;
|
|
371
|
+
if ((!hasBase64 && !hasSource) || (hasBase64 && hasSource)) {
|
|
372
|
+
throw new McpError(ErrorCode.InvalidParams, 'Exactly one of contentBase64 or sourcePath must be provided');
|
|
373
|
+
}
|
|
374
|
+
if (!moduleBridge) {
|
|
375
|
+
throw new McpError(ErrorCode.InvalidRequest, 'The module bridge is disabled. Set FOUNDRY_MODULE_BRIDGE_ENABLED=true and install the companion Foundry module.');
|
|
376
|
+
}
|
|
377
|
+
return withToolError('upload asset', async () => {
|
|
378
|
+
let base64Payload;
|
|
379
|
+
let mimeType = args.mimeType;
|
|
380
|
+
if (hasSource && sourcePath) {
|
|
381
|
+
const buffer = await readFile(sourcePath);
|
|
382
|
+
if (buffer.byteLength > MAX_UPLOAD_BYTES) {
|
|
383
|
+
throw new McpError(ErrorCode.InvalidParams, `File size (${buffer.byteLength} bytes) exceeds the 8 MiB upload limit`);
|
|
384
|
+
}
|
|
385
|
+
base64Payload = buffer.toString('base64');
|
|
386
|
+
if (!mimeType) {
|
|
387
|
+
const ext = extname(sourcePath).toLowerCase();
|
|
388
|
+
mimeType = MIME_EXTENSIONS[ext] ?? 'application/octet-stream';
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
base64Payload = contentBase64 ?? '';
|
|
393
|
+
const estimatedBytes = Math.ceil((base64Payload.length * 3) / 4);
|
|
394
|
+
if (estimatedBytes > MAX_UPLOAD_BYTES) {
|
|
395
|
+
throw new McpError(ErrorCode.InvalidParams, `Estimated payload size (${estimatedBytes} bytes) exceeds the 8 MiB upload limit`);
|
|
396
|
+
}
|
|
397
|
+
if (!mimeType) {
|
|
398
|
+
const ext = extname(filename).toLowerCase();
|
|
399
|
+
mimeType = MIME_EXTENSIONS[ext] ?? 'application/octet-stream';
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
const result = await moduleBridge.send('upload_asset', {
|
|
403
|
+
targetDir,
|
|
404
|
+
filename,
|
|
405
|
+
contentBase64: base64Payload,
|
|
406
|
+
mimeType,
|
|
407
|
+
});
|
|
408
|
+
if (!isUploadAssetResult(result)) {
|
|
409
|
+
throw new McpError(ErrorCode.InternalError, 'Module returned an unexpected upload_asset shape');
|
|
410
|
+
}
|
|
411
|
+
return {
|
|
412
|
+
content: [
|
|
413
|
+
{
|
|
414
|
+
type: 'text',
|
|
415
|
+
text: `π€ **Asset Uploaded**\n**Path:** \`${result.path}\`\n**Target Directory:** \`${targetDir}\`\n**Filename:** \`${filename}\`\n_Pass this path directly to create_scene (backgroundSrc), create_tile (src), or create_sound (path)._`,
|
|
416
|
+
},
|
|
417
|
+
],
|
|
418
|
+
};
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
//# sourceMappingURL=module-bridge.js.map
|