@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.
Files changed (96) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +468 -2
  3. package/dist/character/manager.js +208 -0
  4. package/dist/cli/checks.js +307 -0
  5. package/dist/cli/config-file.js +120 -0
  6. package/dist/cli/init.js +410 -0
  7. package/dist/cli/prompt.js +117 -0
  8. package/dist/cli/snippets.js +49 -0
  9. package/dist/combat/manager.js +371 -0
  10. package/dist/config/env-layers.js +49 -0
  11. package/dist/config/index.js +298 -0
  12. package/dist/config/sources.js +201 -0
  13. package/dist/core.js +415 -0
  14. package/dist/diagnostics/client.js +353 -0
  15. package/dist/diagnostics/types.js +94 -0
  16. package/dist/foundry/auth.js +220 -0
  17. package/dist/foundry/browser-detect.js +170 -0
  18. package/dist/foundry/client.js +5555 -0
  19. package/dist/foundry/dice-formula.js +154 -0
  20. package/dist/foundry/event-log.js +299 -0
  21. package/dist/foundry/headless-gm-session.js +740 -0
  22. package/dist/foundry/journal-history.js +90 -0
  23. package/dist/foundry/journal-integrity.js +306 -0
  24. package/dist/foundry/module-bridge.js +413 -0
  25. package/dist/foundry/types.js +52 -0
  26. package/dist/foundry/world-cache.js +386 -0
  27. package/dist/index.js +107 -0
  28. package/dist/mcp-server.js +63 -0
  29. package/dist/server.js +159 -0
  30. package/dist/shared/build-order.js +134 -0
  31. package/dist/shared/config-hash.js +62 -0
  32. package/dist/shared/config-notice.js +55 -0
  33. package/dist/shared/control.js +255 -0
  34. package/dist/shared/daemon-link.js +135 -0
  35. package/dist/shared/daemon.js +313 -0
  36. package/dist/shared/ipc-client.js +200 -0
  37. package/dist/shared/ipc-server.js +316 -0
  38. package/dist/shared/paths.js +79 -0
  39. package/dist/shared/protocol.js +230 -0
  40. package/dist/shared/relay-config.js +65 -0
  41. package/dist/shared/relay.js +120 -0
  42. package/dist/shared/rotating-log.js +57 -0
  43. package/dist/shared/spawn.js +39 -0
  44. package/dist/shared/state-file.js +33 -0
  45. package/dist/shared/token.js +62 -0
  46. package/dist/shared/version.js +57 -0
  47. package/dist/tools/base.js +74 -0
  48. package/dist/tools/definitions.js +3109 -0
  49. package/dist/tools/handlers/actor-mutations.js +128 -0
  50. package/dist/tools/handlers/actors.js +114 -0
  51. package/dist/tools/handlers/chat-mutations.js +49 -0
  52. package/dist/tools/handlers/chat.js +36 -0
  53. package/dist/tools/handlers/combat-mutations.js +215 -0
  54. package/dist/tools/handlers/combat-order.js +94 -0
  55. package/dist/tools/handlers/combat.js +51 -0
  56. package/dist/tools/handlers/compendium.js +83 -0
  57. package/dist/tools/handlers/diagnostics.js +438 -0
  58. package/dist/tools/handlers/dice.js +54 -0
  59. package/dist/tools/handlers/effects.js +103 -0
  60. package/dist/tools/handlers/events.js +50 -0
  61. package/dist/tools/handlers/generation.js +258 -0
  62. package/dist/tools/handlers/item-mutations.js +132 -0
  63. package/dist/tools/handlers/items.js +53 -0
  64. package/dist/tools/handlers/journal-mutations.js +230 -0
  65. package/dist/tools/handlers/journals.js +80 -0
  66. package/dist/tools/handlers/module-bridge.js +421 -0
  67. package/dist/tools/handlers/placeable-mutations.js +272 -0
  68. package/dist/tools/handlers/resources.js +203 -0
  69. package/dist/tools/handlers/roll-tables.js +97 -0
  70. package/dist/tools/handlers/scene-mutations.js +159 -0
  71. package/dist/tools/handlers/scenes.js +355 -0
  72. package/dist/tools/handlers/settings.js +53 -0
  73. package/dist/tools/handlers/tile-mutations.js +56 -0
  74. package/dist/tools/handlers/token-mutations.js +356 -0
  75. package/dist/tools/handlers/users.js +59 -0
  76. package/dist/tools/handlers/utils.js +47 -0
  77. package/dist/tools/handlers/wall-mutations.js +98 -0
  78. package/dist/tools/handlers/world-documents.js +222 -0
  79. package/dist/tools/handlers/world.js +92 -0
  80. package/dist/tools/index.js +25 -0
  81. package/dist/tools/new-router.js +97 -0
  82. package/dist/tools/registry.js +79 -0
  83. package/dist/tools/resources.js +63 -0
  84. package/dist/tools/router.js +688 -0
  85. package/dist/tools/write-tools.js +97 -0
  86. package/dist/utils/cache-instance.js +30 -0
  87. package/dist/utils/cache.js +205 -0
  88. package/dist/utils/diagnostics.js +332 -0
  89. package/dist/utils/guards.js +17 -0
  90. package/dist/utils/image-size.js +160 -0
  91. package/dist/utils/log-args.js +47 -0
  92. package/dist/utils/logger.js +203 -0
  93. package/dist/utils/refusal.js +30 -0
  94. package/dist/utils/sanitize.js +22 -0
  95. package/dist/utils/serial-queue.js +22 -0
  96. package/package.json +124 -4
@@ -0,0 +1,272 @@
1
+ /**
2
+ * @fileoverview Placeable document mutation tool handlers (lights, sounds, notes, drawings, templates).
3
+ *
4
+ * All are WRITE operations — require FOUNDRY_WRITE_ENABLED=true and an active Socket.IO connection.
5
+ */
6
+ import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';
7
+ import { resolveSceneId } from './scenes.js';
8
+ import { withToolError } from './utils.js';
9
+ // ============================================================================
10
+ // AmbientLight handlers
11
+ // ============================================================================
12
+ export async function handleCreateLight(args, foundryClient) {
13
+ const sceneId = resolveSceneId(args.sceneId, foundryClient);
14
+ return withToolError('create light', async () => {
15
+ const scene = foundryClient.getScenes().find((s) => s._id === sceneId);
16
+ const created = (await foundryClient.createLight(sceneId, args));
17
+ const lightId = created?._id ?? 'unknown';
18
+ return {
19
+ content: [
20
+ {
21
+ type: 'text',
22
+ text: `💡 **Ambient Light Created**\n**ID:** ${lightId}\n**Scene:** ${scene?.name ?? sceneId} (${sceneId})\n**Dim Radius:** ${args.dim ?? 0}, **Bright Radius:** ${args.bright ?? 0}${args.color ? `, **Color:** ${args.color}` : ''}`,
23
+ },
24
+ ],
25
+ };
26
+ });
27
+ }
28
+ export async function handleUpdateLight(args, foundryClient) {
29
+ const { lightId } = args;
30
+ if (!lightId || typeof lightId !== 'string') {
31
+ throw new McpError(ErrorCode.InvalidParams, 'lightId is required and must be a string');
32
+ }
33
+ const sceneId = resolveSceneId(args.sceneId, foundryClient);
34
+ return withToolError('update light', async () => {
35
+ const scene = foundryClient.getScenes().find((s) => s._id === sceneId);
36
+ await foundryClient.updateLight(sceneId, lightId, args);
37
+ return {
38
+ content: [
39
+ {
40
+ type: 'text',
41
+ text: `💡 **Ambient Light Updated**\n**ID:** ${lightId}\n**Scene:** ${scene?.name ?? sceneId} (${sceneId})`,
42
+ },
43
+ ],
44
+ };
45
+ });
46
+ }
47
+ export async function handleDeleteLight(args, foundryClient) {
48
+ const { lightId } = args;
49
+ if (!lightId || typeof lightId !== 'string') {
50
+ throw new McpError(ErrorCode.InvalidParams, 'lightId is required and must be a string');
51
+ }
52
+ const sceneId = resolveSceneId(args.sceneId, foundryClient);
53
+ return withToolError('delete light', async () => {
54
+ const scene = foundryClient.getScenes().find((s) => s._id === sceneId);
55
+ await foundryClient.deleteLight(sceneId, lightId);
56
+ return {
57
+ content: [
58
+ {
59
+ type: 'text',
60
+ text: `🧹 **Ambient Light Deleted**\n**ID:** ${lightId}\n**Scene:** ${scene?.name ?? sceneId} (${sceneId})`,
61
+ },
62
+ ],
63
+ };
64
+ });
65
+ }
66
+ // ============================================================================
67
+ // AmbientSound handlers
68
+ // ============================================================================
69
+ export async function handleCreateSound(args, foundryClient) {
70
+ const { path } = args;
71
+ if (!path || typeof path !== 'string') {
72
+ throw new McpError(ErrorCode.InvalidParams, 'path is required and must be a string');
73
+ }
74
+ const sceneId = resolveSceneId(args.sceneId, foundryClient);
75
+ return withToolError('create sound', async () => {
76
+ const scene = foundryClient.getScenes().find((s) => s._id === sceneId);
77
+ const created = (await foundryClient.createSound(sceneId, path, args));
78
+ const soundId = created?._id ?? 'unknown';
79
+ return {
80
+ content: [
81
+ {
82
+ type: 'text',
83
+ text: `🔊 **Ambient Sound Created**\n**ID:** ${soundId}\n**Scene:** ${scene?.name ?? sceneId} (${sceneId})\n**Path:** \`${path}\`\n**Radius:** ${args.radius ?? 0}px, **Volume:** ${args.volume ?? 0.5}`,
84
+ },
85
+ ],
86
+ };
87
+ });
88
+ }
89
+ export async function handleDeleteSound(args, foundryClient) {
90
+ const { soundId } = args;
91
+ if (!soundId || typeof soundId !== 'string') {
92
+ throw new McpError(ErrorCode.InvalidParams, 'soundId is required and must be a string');
93
+ }
94
+ const sceneId = resolveSceneId(args.sceneId, foundryClient);
95
+ return withToolError('delete sound', async () => {
96
+ const scene = foundryClient.getScenes().find((s) => s._id === sceneId);
97
+ await foundryClient.deleteSound(sceneId, soundId);
98
+ return {
99
+ content: [
100
+ {
101
+ type: 'text',
102
+ text: `🧹 **Ambient Sound Deleted**\n**ID:** ${soundId}\n**Scene:** ${scene?.name ?? sceneId} (${sceneId})`,
103
+ },
104
+ ],
105
+ };
106
+ });
107
+ }
108
+ // ============================================================================
109
+ // Note handlers
110
+ // ============================================================================
111
+ export async function handleCreateNote(args, foundryClient) {
112
+ const sceneId = resolveSceneId(args.sceneId, foundryClient);
113
+ return withToolError('create note', async () => {
114
+ const scene = foundryClient.getScenes().find((s) => s._id === sceneId);
115
+ const created = (await foundryClient.createNote(sceneId, args));
116
+ const noteId = created?._id ?? 'unknown';
117
+ return {
118
+ content: [
119
+ {
120
+ type: 'text',
121
+ text: `📌 **Map Note Created**\n**ID:** ${noteId}\n**Scene:** ${scene?.name ?? sceneId} (${sceneId})${args.text ? `\n**Label:** "${args.text}"` : ''}${args.entryId ? `\n**Journal Entry:** ${args.entryId}` : ''}`,
122
+ },
123
+ ],
124
+ };
125
+ });
126
+ }
127
+ export async function handleDeleteNote(args, foundryClient) {
128
+ const { noteId } = args;
129
+ if (!noteId || typeof noteId !== 'string') {
130
+ throw new McpError(ErrorCode.InvalidParams, 'noteId is required and must be a string');
131
+ }
132
+ const sceneId = resolveSceneId(args.sceneId, foundryClient);
133
+ return withToolError('delete note', async () => {
134
+ const scene = foundryClient.getScenes().find((s) => s._id === sceneId);
135
+ await foundryClient.deleteNote(sceneId, noteId);
136
+ return {
137
+ content: [
138
+ {
139
+ type: 'text',
140
+ text: `🧹 **Map Note Deleted**\n**ID:** ${noteId}\n**Scene:** ${scene?.name ?? sceneId} (${sceneId})`,
141
+ },
142
+ ],
143
+ };
144
+ });
145
+ }
146
+ // ============================================================================
147
+ // Drawing handlers
148
+ // ============================================================================
149
+ export async function handleCreateDrawing(args, foundryClient) {
150
+ if (typeof args.x !== 'number' ||
151
+ !Number.isFinite(args.x) ||
152
+ typeof args.y !== 'number' ||
153
+ !Number.isFinite(args.y)) {
154
+ throw new McpError(ErrorCode.InvalidParams, 'x and y are required and must be finite numbers');
155
+ }
156
+ const sceneId = resolveSceneId(args.sceneId, foundryClient);
157
+ return withToolError('create drawing', async () => {
158
+ const scene = foundryClient.getScenes().find((s) => s._id === sceneId);
159
+ const created = (await foundryClient.createDrawing(sceneId, args));
160
+ const drawingId = created?._id ?? 'unknown';
161
+ return {
162
+ content: [
163
+ {
164
+ type: 'text',
165
+ text: `🎨 **Drawing Created**\n**ID:** ${drawingId}\n**Scene:** ${scene?.name ?? sceneId} (${sceneId})\n**Shape:** ${args.shape ?? 'r'} at (${args.x}, ${args.y})${args.text ? `\n**Text:** "${args.text}"` : ''}`,
166
+ },
167
+ ],
168
+ };
169
+ });
170
+ }
171
+ export async function handleDeleteDrawing(args, foundryClient) {
172
+ const { drawingId } = args;
173
+ if (!drawingId || typeof drawingId !== 'string') {
174
+ throw new McpError(ErrorCode.InvalidParams, 'drawingId is required and must be a string');
175
+ }
176
+ const sceneId = resolveSceneId(args.sceneId, foundryClient);
177
+ return withToolError('delete drawing', async () => {
178
+ const scene = foundryClient.getScenes().find((s) => s._id === sceneId);
179
+ await foundryClient.deleteDrawing(sceneId, drawingId);
180
+ return {
181
+ content: [
182
+ {
183
+ type: 'text',
184
+ text: `🧹 **Drawing Deleted**\n**ID:** ${drawingId}\n**Scene:** ${scene?.name ?? sceneId} (${sceneId})`,
185
+ },
186
+ ],
187
+ };
188
+ });
189
+ }
190
+ // ============================================================================
191
+ // MeasuredTemplate handlers
192
+ // ============================================================================
193
+ export async function handleCreateTemplate(args, foundryClient) {
194
+ if (typeof args.distance !== 'number' || !Number.isFinite(args.distance) || args.distance < 0) {
195
+ throw new McpError(ErrorCode.InvalidParams, 'distance is required and must be a finite non-negative number');
196
+ }
197
+ const sceneId = resolveSceneId(args.sceneId, foundryClient);
198
+ return withToolError('create template', async () => {
199
+ const scene = foundryClient.getScenes().find((s) => s._id === sceneId);
200
+ const created = (await foundryClient.createTemplate(sceneId, args));
201
+ const templateId = created?._id ?? 'unknown';
202
+ return {
203
+ content: [
204
+ {
205
+ type: 'text',
206
+ text: `📐 **Measured Template Created**\n**ID:** ${templateId}\n**Scene:** ${scene?.name ?? sceneId} (${sceneId})\n**Type:** ${args.t ?? 'circle'}, **Distance:** ${args.distance} grid units`,
207
+ },
208
+ ],
209
+ };
210
+ });
211
+ }
212
+ export async function handleDeleteTemplate(args, foundryClient) {
213
+ const { templateId } = args;
214
+ if (!templateId || typeof templateId !== 'string') {
215
+ throw new McpError(ErrorCode.InvalidParams, 'templateId is required and must be a string');
216
+ }
217
+ const sceneId = resolveSceneId(args.sceneId, foundryClient);
218
+ return withToolError('delete template', async () => {
219
+ const scene = foundryClient.getScenes().find((s) => s._id === sceneId);
220
+ await foundryClient.deleteTemplate(sceneId, templateId);
221
+ return {
222
+ content: [
223
+ {
224
+ type: 'text',
225
+ text: `🧹 **Measured Template Deleted**\n**ID:** ${templateId}\n**Scene:** ${scene?.name ?? sceneId} (${sceneId})`,
226
+ },
227
+ ],
228
+ };
229
+ });
230
+ }
231
+ // ============================================================================
232
+ // Region handlers (FoundryVTT v12+)
233
+ // ============================================================================
234
+ export async function handleCreateRegion(args, foundryClient) {
235
+ if (!args.name || typeof args.name !== 'string') {
236
+ throw new McpError(ErrorCode.InvalidParams, 'name is required and must be a string');
237
+ }
238
+ const sceneId = resolveSceneId(args.sceneId, foundryClient);
239
+ return withToolError('create region', async () => {
240
+ const scene = foundryClient.getScenes().find((s) => s._id === sceneId);
241
+ const created = (await foundryClient.createRegion(sceneId, args));
242
+ const regionId = created?._id ?? 'unknown';
243
+ return {
244
+ content: [
245
+ {
246
+ type: 'text',
247
+ text: `🌐 **Scene Region Created**\n**ID:** ${regionId}\n**Name:** ${args.name}\n**Scene:** ${scene?.name ?? sceneId} (${sceneId})${args.color ? `\n**Color:** ${args.color}` : ''}`,
248
+ },
249
+ ],
250
+ };
251
+ });
252
+ }
253
+ export async function handleDeleteRegion(args, foundryClient) {
254
+ const { regionId } = args;
255
+ if (!regionId || typeof regionId !== 'string') {
256
+ throw new McpError(ErrorCode.InvalidParams, 'regionId is required and must be a string');
257
+ }
258
+ const sceneId = resolveSceneId(args.sceneId, foundryClient);
259
+ return withToolError('delete region', async () => {
260
+ const scene = foundryClient.getScenes().find((s) => s._id === sceneId);
261
+ await foundryClient.deleteRegion(sceneId, regionId);
262
+ return {
263
+ content: [
264
+ {
265
+ type: 'text',
266
+ text: `🧹 **Scene Region Deleted**\n**ID:** ${regionId}\n**Scene:** ${scene?.name ?? sceneId} (${sceneId})`,
267
+ },
268
+ ],
269
+ };
270
+ });
271
+ }
272
+ //# sourceMappingURL=placeable-mutations.js.map
@@ -0,0 +1,203 @@
1
+ /**
2
+ * Resource access handlers
3
+ */
4
+ import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';
5
+ import { logger } from '../../utils/logger.js';
6
+ import { getTurnOrder } from './combat-order.js';
7
+ import { withToolError } from './utils.js';
8
+ export async function handleReadResource(uri, foundryClient, diagnosticsClient) {
9
+ return withToolError('read resource', async () => {
10
+ switch (uri) {
11
+ case 'foundry://actors':
12
+ return await getActorsResource(foundryClient);
13
+ case 'foundry://items':
14
+ return await getItemsResource(foundryClient);
15
+ case 'foundry://scenes':
16
+ return await getScenesResource(foundryClient);
17
+ case 'foundry://scenes/current':
18
+ return await getCurrentSceneResource(foundryClient);
19
+ case 'foundry://world/settings':
20
+ return await getWorldSettingsResource(foundryClient);
21
+ case 'foundry://journals':
22
+ return await getJournalsResource(foundryClient);
23
+ case 'foundry://users':
24
+ return await getUsersResource(foundryClient);
25
+ case 'foundry://combat':
26
+ return await getCombatResource(foundryClient);
27
+ case 'foundry://system/diagnostics':
28
+ return await getSystemDiagnosticsResource(diagnosticsClient);
29
+ default:
30
+ throw new McpError(ErrorCode.InvalidParams, `Unknown resource URI: ${uri}`);
31
+ }
32
+ });
33
+ }
34
+ async function getActorsResource(foundryClient) {
35
+ const result = await foundryClient.searchActors({ limit: 100 });
36
+ return {
37
+ contents: [
38
+ {
39
+ uri: 'foundry://actors',
40
+ mimeType: 'application/json',
41
+ text: JSON.stringify({ actors: result.actors, total: result.total, lastUpdated: new Date().toISOString() }, null, 2),
42
+ },
43
+ ],
44
+ };
45
+ }
46
+ async function getItemsResource(foundryClient) {
47
+ const result = await foundryClient.searchItems({ limit: 100 });
48
+ return {
49
+ contents: [
50
+ {
51
+ uri: 'foundry://items',
52
+ mimeType: 'application/json',
53
+ text: JSON.stringify({ items: result.items, total: result.total, lastUpdated: new Date().toISOString() }, null, 2),
54
+ },
55
+ ],
56
+ };
57
+ }
58
+ async function getScenesResource(foundryClient) {
59
+ const scenes = foundryClient.getScenes();
60
+ return {
61
+ contents: [
62
+ {
63
+ uri: 'foundry://scenes',
64
+ mimeType: 'application/json',
65
+ text: JSON.stringify({
66
+ scenes: scenes.map((s) => ({ _id: s._id, name: s.name, active: s.active })),
67
+ total: scenes.length,
68
+ lastUpdated: new Date().toISOString(),
69
+ }, null, 2),
70
+ },
71
+ ],
72
+ };
73
+ }
74
+ async function getCurrentSceneResource(foundryClient) {
75
+ try {
76
+ const scene = await foundryClient.getCurrentScene();
77
+ return {
78
+ contents: [
79
+ {
80
+ uri: 'foundry://scenes/current',
81
+ mimeType: 'application/json',
82
+ text: JSON.stringify({ currentScene: scene, lastUpdated: new Date().toISOString() }, null, 2),
83
+ },
84
+ ],
85
+ };
86
+ }
87
+ catch (error) {
88
+ logger.debug('getCurrentSceneResource: no active scene', { error });
89
+ return {
90
+ contents: [
91
+ {
92
+ uri: 'foundry://scenes/current',
93
+ mimeType: 'application/json',
94
+ text: JSON.stringify({
95
+ currentScene: null,
96
+ message: 'No active scene',
97
+ lastUpdated: new Date().toISOString(),
98
+ }, null, 2),
99
+ },
100
+ ],
101
+ };
102
+ }
103
+ }
104
+ async function getWorldSettingsResource(foundryClient) {
105
+ const world = await foundryClient.getWorldInfo();
106
+ return {
107
+ contents: [
108
+ {
109
+ uri: 'foundry://world/settings',
110
+ mimeType: 'application/json',
111
+ text: JSON.stringify({ world, lastUpdated: new Date().toISOString() }, null, 2),
112
+ },
113
+ ],
114
+ };
115
+ }
116
+ async function getJournalsResource(foundryClient) {
117
+ const journals = foundryClient.getJournals();
118
+ return {
119
+ contents: [
120
+ {
121
+ uri: 'foundry://journals',
122
+ mimeType: 'application/json',
123
+ text: JSON.stringify({
124
+ journals: journals.map((j) => ({
125
+ _id: j._id,
126
+ name: j.name,
127
+ pages: j.pages?.length || 0,
128
+ })),
129
+ total: journals.length,
130
+ lastUpdated: new Date().toISOString(),
131
+ }, null, 2),
132
+ },
133
+ ],
134
+ };
135
+ }
136
+ async function getUsersResource(foundryClient) {
137
+ const { users, activeUsers } = foundryClient.getUsers();
138
+ return {
139
+ contents: [
140
+ {
141
+ uri: 'foundry://users',
142
+ mimeType: 'application/json',
143
+ text: JSON.stringify({ users, activeUsers, lastUpdated: new Date().toISOString() }, null, 2),
144
+ },
145
+ ],
146
+ };
147
+ }
148
+ /**
149
+ * Serves the active combat encounter (laurigates/foundryvtt-mcp#214 follow-up).
150
+ *
151
+ * `Combat#turn` indexes the **initiative-sorted** turn order, and this resource
152
+ * is the id-bearing companion to `get_combat_state`, whose ordinals are printed
153
+ * in that same order. Emitting the raw cached `combatants` array — which is in
154
+ * creation order — next to that `turn` makes both `combat.combatants[turn]` and
155
+ * "the Nth combatant I just read" resolve to the wrong document, so the emitted
156
+ * `combatants` are re-ordered here through the shared `getTurnOrder()` helper.
157
+ *
158
+ * `getTurnOrder()` sorts a copy: the cached array is a live reference into
159
+ * worldData and must not be reordered as a side effect of a read.
160
+ */
161
+ async function getCombatResource(foundryClient) {
162
+ const cached = foundryClient.getCombatState();
163
+ const combat = cached ? { ...cached, combatants: getTurnOrder(cached) } : cached;
164
+ return {
165
+ contents: [
166
+ {
167
+ uri: 'foundry://combat',
168
+ mimeType: 'application/json',
169
+ text: JSON.stringify({ combat, lastUpdated: new Date().toISOString() }, null, 2),
170
+ },
171
+ ],
172
+ };
173
+ }
174
+ async function getSystemDiagnosticsResource(diagnosticsClient) {
175
+ try {
176
+ const health = await diagnosticsClient.getSystemHealth();
177
+ return {
178
+ contents: [
179
+ {
180
+ uri: 'foundry://system/diagnostics',
181
+ mimeType: 'application/json',
182
+ text: JSON.stringify({ systemHealth: health, lastUpdated: new Date().toISOString() }, null, 2),
183
+ },
184
+ ],
185
+ };
186
+ }
187
+ catch (error) {
188
+ logger.debug('getSystemDiagnosticsResource: diagnostics unavailable', { error });
189
+ return {
190
+ contents: [
191
+ {
192
+ uri: 'foundry://system/diagnostics',
193
+ mimeType: 'application/json',
194
+ text: JSON.stringify({
195
+ message: 'Diagnostics require REST API module',
196
+ lastUpdated: new Date().toISOString(),
197
+ }, null, 2),
198
+ },
199
+ ],
200
+ };
201
+ }
202
+ }
203
+ //# sourceMappingURL=resources.js.map
@@ -0,0 +1,97 @@
1
+ /**
2
+ * @fileoverview Roll table tool handlers (read + local weighted draw)
3
+ *
4
+ * Neither tool mutates FoundryVTT — `roll_table` draws locally against the
5
+ * cached table data (see `FoundryClient#rollOnTable`) and does not require
6
+ * FOUNDRY_WRITE_ENABLED.
7
+ */
8
+ import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';
9
+ import { withToolError } from './utils.js';
10
+ /**
11
+ * Handles listing the world's roll tables.
12
+ */
13
+ export async function handleListRollTables(_args, foundryClient) {
14
+ return withToolError('list roll tables', async () => {
15
+ const tables = foundryClient.getRollTables();
16
+ if (tables.length === 0) {
17
+ return { content: [{ type: 'text', text: 'No roll tables found in this world.' }] };
18
+ }
19
+ const formatted = tables
20
+ .map((t) => {
21
+ const results = Array.isArray(t.results) ? t.results.length : 0;
22
+ return `- **${t.name}** (${results} result${results !== 1 ? 's' : ''}) — ID: ${t._id}`;
23
+ })
24
+ .join('\n');
25
+ return {
26
+ content: [{ type: 'text', text: `🎲 **Roll Tables** (${tables.length})\n\n${formatted}` }],
27
+ };
28
+ });
29
+ }
30
+ /**
31
+ * Handles drawing a result from a roll table.
32
+ */
33
+ export async function handleRollTable(args, foundryClient) {
34
+ const { tableId } = args;
35
+ if (!tableId || typeof tableId !== 'string') {
36
+ throw new McpError(ErrorCode.InvalidParams, 'tableId is required and must be a string');
37
+ }
38
+ return withToolError('roll table', async () => {
39
+ const draw = foundryClient.rollOnTable(tableId);
40
+ const text = draw.result?.text ?? draw.result?.name ?? '(unnamed result)';
41
+ return {
42
+ content: [
43
+ {
44
+ type: 'text',
45
+ text: `🎲 **Table Draw: ${draw.table}**
46
+ **Roll:** ${draw.roll}
47
+ **Result:** ${draw.result ? text : 'No result configured for this roll — the table is incompletely set up.'}`,
48
+ },
49
+ ],
50
+ };
51
+ });
52
+ }
53
+ /**
54
+ * Handles creating a new RollTable document with text results.
55
+ */
56
+ export async function handleCreateRollTable(args, foundryClient) {
57
+ const { name, results } = args;
58
+ if (!name || typeof name !== 'string') {
59
+ throw new McpError(ErrorCode.InvalidParams, 'name is required and must be a string');
60
+ }
61
+ if (!Array.isArray(results) || results.length === 0) {
62
+ throw new McpError(ErrorCode.InvalidParams, 'results is required and must be a non-empty array');
63
+ }
64
+ return withToolError('create roll table', async () => {
65
+ const created = (await foundryClient.createRollTable(name, results, args));
66
+ const tableId = created?._id ?? 'unknown';
67
+ return {
68
+ content: [
69
+ {
70
+ type: 'text',
71
+ text: `🎲 **Roll Table Created**\n**ID:** ${tableId}\n**Name:** ${name}\n**Results Count:** ${results.length}`,
72
+ },
73
+ ],
74
+ };
75
+ });
76
+ }
77
+ /**
78
+ * Handles permanently deleting a RollTable document.
79
+ */
80
+ export async function handleDeleteRollTable(args, foundryClient) {
81
+ const { tableId } = args;
82
+ if (!tableId || typeof tableId !== 'string') {
83
+ throw new McpError(ErrorCode.InvalidParams, 'tableId is required and must be a string');
84
+ }
85
+ return withToolError('delete roll table', async () => {
86
+ await foundryClient.deleteRollTable(tableId);
87
+ return {
88
+ content: [
89
+ {
90
+ type: 'text',
91
+ text: `🗑️ **Roll Table Deleted**\n**ID:** ${tableId}`,
92
+ },
93
+ ],
94
+ };
95
+ });
96
+ }
97
+ //# sourceMappingURL=roll-tables.js.map