@twojestarerpg/campaignmap-mcp 0.0.0-stage → 0.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 +21 -0
- package/README.md +34 -2
- package/THIRD-PARTY-NOTICES.txt +477 -0
- package/dist/apiClient.js +45 -0
- package/dist/config.js +13 -0
- package/dist/confirm.js +21 -0
- package/dist/index.js +49 -0
- package/dist/log.js +5 -0
- package/dist/server.js +26 -0
- package/dist/session.js +173 -0
- package/dist/tools/assets.js +30 -0
- package/dist/tools/campaigns.js +54 -0
- package/dist/tools/index.js +15 -0
- package/dist/tools/maps.js +96 -0
- package/dist/tools/pois.js +73 -0
- package/dist/tools/run.js +36 -0
- package/dist/tools/schemas.js +29 -0
- package/dist/tools/sessions.js +50 -0
- package/dist/tools/types.js +1 -0
- package/dist/tools/zones.js +63 -0
- package/dist/tty.js +98 -0
- package/package.json +58 -4
package/dist/log.js
ADDED
package/dist/server.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// MCP-001 / LLD-58 §6: adapt the tool registry to an MCP server. No logic here:
|
|
2
|
+
// validation, the destructive gate and the HTTP call all live in tools/run.ts.
|
|
3
|
+
import { McpServer } from '@modelcontextprotocol/server';
|
|
4
|
+
import { tools as allTools } from './tools/index.js';
|
|
5
|
+
import { runTool } from './tools/run.js';
|
|
6
|
+
export const SERVER_INFO = { name: 'campaignmap', version: '0.1.0' };
|
|
7
|
+
export function createMcpServer({ api, confirm, tools = allTools }) {
|
|
8
|
+
const server = new McpServer(SERVER_INFO);
|
|
9
|
+
for (const tool of tools) {
|
|
10
|
+
server.registerTool(tool.name, {
|
|
11
|
+
title: tool.title,
|
|
12
|
+
description: tool.description,
|
|
13
|
+
inputSchema: tool.input,
|
|
14
|
+
annotations: {
|
|
15
|
+
readOnlyHint: tool.route.method === 'GET',
|
|
16
|
+
destructiveHint: tool.destructive === true,
|
|
17
|
+
idempotentHint: tool.route.method === 'GET' || tool.route.method === 'DELETE',
|
|
18
|
+
openWorldHint: false,
|
|
19
|
+
},
|
|
20
|
+
}, async (args) => {
|
|
21
|
+
const res = await runTool(tool, args, api, confirm);
|
|
22
|
+
return { content: [{ type: 'text', text: res.text }], isError: !res.ok };
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return server;
|
|
26
|
+
}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// MCP-003 / ADR-012 / ADR-031 / LLD-58 §4: authentication.
|
|
2
|
+
//
|
|
3
|
+
// The API hands the refresh token out only as the `refreshToken` httpOnly
|
|
4
|
+
// cookie, so we read it from `Set-Cookie` and replay it as `Cookie`. Only the
|
|
5
|
+
// refresh token is persisted (mode 0600); the access token stays in memory and
|
|
6
|
+
// the password is never stored (MCP-05).
|
|
7
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
8
|
+
import { dirname } from 'node:path';
|
|
9
|
+
export const REFRESH_COOKIE = 'refreshToken';
|
|
10
|
+
export const NOT_LOGGED_IN_MESSAGE = 'Not logged in. Run "npx -y @twojestarerpg/campaignmap-mcp login" in a terminal.';
|
|
11
|
+
export class NotLoggedInError extends Error {
|
|
12
|
+
constructor(message = NOT_LOGGED_IN_MESSAGE) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = 'NotLoggedInError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** Pull the refresh cookie's value out of a response's `Set-Cookie` headers. */
|
|
18
|
+
export function extractRefreshToken(headers) {
|
|
19
|
+
const lines = headers.getSetCookie?.() ?? [];
|
|
20
|
+
for (const line of lines) {
|
|
21
|
+
const pair = line.split(';', 1)[0] ?? '';
|
|
22
|
+
const eq = pair.indexOf('=');
|
|
23
|
+
if (eq > 0 && pair.slice(0, eq).trim() === REFRESH_COOKIE) {
|
|
24
|
+
const value = pair.slice(eq + 1).trim();
|
|
25
|
+
return value === '' ? null : value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
export class Session {
|
|
31
|
+
opts;
|
|
32
|
+
accessToken = null;
|
|
33
|
+
pending = null;
|
|
34
|
+
doFetch;
|
|
35
|
+
constructor(opts) {
|
|
36
|
+
this.opts = opts;
|
|
37
|
+
this.doFetch = opts.fetch ?? fetch;
|
|
38
|
+
}
|
|
39
|
+
get authUrl() {
|
|
40
|
+
return `${this.opts.config.apiUrl}/api/v1/auth`;
|
|
41
|
+
}
|
|
42
|
+
// ── session file ──────────────────────────────────────────────────────────
|
|
43
|
+
/** MCP-06: missing, corrupt or foreign-URL file = "not logged in". */
|
|
44
|
+
readFile() {
|
|
45
|
+
try {
|
|
46
|
+
const raw = JSON.parse(readFileSync(this.opts.config.sessionFile, 'utf8'));
|
|
47
|
+
if (typeof raw.refreshToken !== 'string' || raw.refreshToken === '' ||
|
|
48
|
+
typeof raw.email !== 'string' || raw.apiUrl !== this.opts.config.apiUrl)
|
|
49
|
+
return null;
|
|
50
|
+
return { apiUrl: raw.apiUrl, email: raw.email, refreshToken: raw.refreshToken };
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
writeFile(data) {
|
|
57
|
+
const file = this.opts.config.sessionFile;
|
|
58
|
+
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
|
59
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
60
|
+
writeFileSync(tmp, JSON.stringify(data), { mode: 0o600 });
|
|
61
|
+
try {
|
|
62
|
+
chmodSync(tmp, 0o600);
|
|
63
|
+
}
|
|
64
|
+
catch { /* not meaningful on Windows */ }
|
|
65
|
+
renameSync(tmp, file);
|
|
66
|
+
}
|
|
67
|
+
clearFile() {
|
|
68
|
+
try {
|
|
69
|
+
rmSync(this.opts.config.sessionFile, { force: true });
|
|
70
|
+
}
|
|
71
|
+
catch { /* ignore */ }
|
|
72
|
+
}
|
|
73
|
+
isLoggedIn() {
|
|
74
|
+
return this.accessToken !== null || this.readFile() !== null;
|
|
75
|
+
}
|
|
76
|
+
// ── tokens ────────────────────────────────────────────────────────────────
|
|
77
|
+
/** The access token for the next request: memory, else refresh, else prompt. */
|
|
78
|
+
getAccessToken() {
|
|
79
|
+
if (this.accessToken)
|
|
80
|
+
return Promise.resolve(this.accessToken);
|
|
81
|
+
return this.acquire();
|
|
82
|
+
}
|
|
83
|
+
/** After a 401: forget the access token and mint a new one. */
|
|
84
|
+
renew() {
|
|
85
|
+
this.accessToken = null;
|
|
86
|
+
return this.acquire();
|
|
87
|
+
}
|
|
88
|
+
acquire() {
|
|
89
|
+
// Single flight: concurrent tool calls share one refresh / login.
|
|
90
|
+
this.pending ??= this.acquireOnce().finally(() => { this.pending = null; });
|
|
91
|
+
return this.pending;
|
|
92
|
+
}
|
|
93
|
+
async acquireOnce() {
|
|
94
|
+
const stored = this.readFile();
|
|
95
|
+
if (stored) {
|
|
96
|
+
const token = await this.refresh(stored);
|
|
97
|
+
if (token)
|
|
98
|
+
return token;
|
|
99
|
+
}
|
|
100
|
+
// No usable session: prompt if (and only if) a terminal exists.
|
|
101
|
+
const prompter = this.opts.openPrompter?.() ?? null;
|
|
102
|
+
if (!prompter)
|
|
103
|
+
throw new NotLoggedInError();
|
|
104
|
+
try {
|
|
105
|
+
return await this.login(prompter);
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
prompter.close();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async refresh(stored) {
|
|
112
|
+
const res = await this.doFetch(`${this.authUrl}/refresh`, {
|
|
113
|
+
method: 'POST',
|
|
114
|
+
headers: { cookie: `${REFRESH_COOKIE}=${stored.refreshToken}` },
|
|
115
|
+
});
|
|
116
|
+
if (res.status === 401) {
|
|
117
|
+
this.clearFile(); // revoked / expired: the file is useless now
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
if (!res.ok)
|
|
121
|
+
throw new Error(`Token refresh failed: HTTP ${res.status}`);
|
|
122
|
+
const body = (await res.json());
|
|
123
|
+
const rotated = extractRefreshToken(res.headers);
|
|
124
|
+
if (rotated)
|
|
125
|
+
this.writeFile({ ...stored, refreshToken: rotated }); // rotation: the old one is dead
|
|
126
|
+
this.accessToken = body.accessToken;
|
|
127
|
+
return body.accessToken;
|
|
128
|
+
}
|
|
129
|
+
/** Interactive login: email visible, password hidden, never persisted. */
|
|
130
|
+
async login(prompter) {
|
|
131
|
+
const email = (await prompter.ask(`CampaignMap e-mail (${this.opts.config.apiUrl}): `)).trim();
|
|
132
|
+
const password = await prompter.ask('Password: ', { hidden: true });
|
|
133
|
+
return this.loginWith(email, password);
|
|
134
|
+
}
|
|
135
|
+
async loginWith(email, password) {
|
|
136
|
+
const res = await this.doFetch(`${this.authUrl}/login`, {
|
|
137
|
+
method: 'POST',
|
|
138
|
+
headers: { 'content-type': 'application/json' },
|
|
139
|
+
body: JSON.stringify({ email, password }),
|
|
140
|
+
});
|
|
141
|
+
if (!res.ok) {
|
|
142
|
+
let message = `HTTP ${res.status}`;
|
|
143
|
+
try {
|
|
144
|
+
message = (await res.json()).message ?? message;
|
|
145
|
+
}
|
|
146
|
+
catch { /* keep status */ }
|
|
147
|
+
throw new Error(`Login failed: ${message}`);
|
|
148
|
+
}
|
|
149
|
+
const body = (await res.json());
|
|
150
|
+
const refreshToken = extractRefreshToken(res.headers);
|
|
151
|
+
if (!refreshToken)
|
|
152
|
+
throw new Error('Login failed: the API did not set a refresh cookie');
|
|
153
|
+
this.writeFile({ apiUrl: this.opts.config.apiUrl, email, refreshToken });
|
|
154
|
+
this.accessToken = body.accessToken;
|
|
155
|
+
return body.accessToken;
|
|
156
|
+
}
|
|
157
|
+
/** Revoke server-side (best effort) and delete the file. */
|
|
158
|
+
async logout() {
|
|
159
|
+
const stored = this.readFile();
|
|
160
|
+
this.accessToken = null;
|
|
161
|
+
if (stored) {
|
|
162
|
+
try {
|
|
163
|
+
await this.doFetch(`${this.authUrl}/logout`, {
|
|
164
|
+
method: 'POST',
|
|
165
|
+
headers: { cookie: `${REFRESH_COOKIE}=${stored.refreshToken}` },
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
catch { /* offline: the file is still removed */ }
|
|
169
|
+
}
|
|
170
|
+
if (existsSync(this.opts.config.sessionFile))
|
|
171
|
+
this.clearFile();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// MCP-006: metadata only. Image bytes are never returned to the model.
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { envelope } from '../apiClient.js';
|
|
4
|
+
import { id, mapId } from './schemas.js';
|
|
5
|
+
import { defineTool } from './types.js';
|
|
6
|
+
export const assetTools = [
|
|
7
|
+
defineTool({
|
|
8
|
+
name: 'campaignmap_list_map_assets',
|
|
9
|
+
title: 'List map assets',
|
|
10
|
+
description: 'List a map\'s background images as metadata (id, mimeType, widthPx, heightPx, placement). The image bytes are not returned.',
|
|
11
|
+
input: z.strictObject({ mapId }),
|
|
12
|
+
route: { method: 'GET', path: '/maps/{mapId}/assets' },
|
|
13
|
+
}),
|
|
14
|
+
defineTool({
|
|
15
|
+
name: 'campaignmap_get_map_asset_metadata',
|
|
16
|
+
title: 'Get map asset metadata',
|
|
17
|
+
description: 'Metadata of one background image (id, mimeType, widthPx, heightPx, placement). The image bytes are not returned.',
|
|
18
|
+
input: z.strictObject({ mapId, assetId: id('Asset id') }),
|
|
19
|
+
// The by-id endpoint streams the image, so this reads the list and picks the item.
|
|
20
|
+
route: { method: 'GET', path: '/maps/{mapId}/assets' },
|
|
21
|
+
run: async (args, api) => {
|
|
22
|
+
const res = await api.request({ method: 'GET', path: `/maps/${encodeURIComponent(String(args.mapId))}/assets` });
|
|
23
|
+
if (!res.ok)
|
|
24
|
+
return res;
|
|
25
|
+
const items = JSON.parse(res.text);
|
|
26
|
+
const hit = items.find((a) => a.id === args.assetId);
|
|
27
|
+
return hit ? { status: 200, ok: true, text: JSON.stringify(hit) } : envelope(404, 'Not Found', 'Asset not found');
|
|
28
|
+
},
|
|
29
|
+
}),
|
|
30
|
+
];
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { atLeastOneOf, description, id, title } from './schemas.js';
|
|
3
|
+
import { defineTool } from './types.js';
|
|
4
|
+
const campaignId = id('Campaign id');
|
|
5
|
+
export const campaignTools = [
|
|
6
|
+
defineTool({
|
|
7
|
+
name: 'campaignmap_list_campaigns',
|
|
8
|
+
title: 'List campaigns',
|
|
9
|
+
description: 'List the user\'s campaigns (newest activity first) with mapCount and lastActivityAt. Use this first to find a campaign id.',
|
|
10
|
+
input: z.strictObject({
|
|
11
|
+
page: z.number().int().min(1).optional().describe('Page, default 1'),
|
|
12
|
+
pageSize: z.number().int().min(1).max(100).optional().describe('Items per page, default 20'),
|
|
13
|
+
}),
|
|
14
|
+
route: { method: 'GET', path: '/campaigns' },
|
|
15
|
+
query: ['page', 'pageSize'],
|
|
16
|
+
}),
|
|
17
|
+
defineTool({
|
|
18
|
+
name: 'campaignmap_get_campaign',
|
|
19
|
+
title: 'Get a campaign',
|
|
20
|
+
description: 'Get one campaign by id.',
|
|
21
|
+
input: z.strictObject({ campaignId }),
|
|
22
|
+
route: { method: 'GET', path: '/campaigns/{campaignId}' },
|
|
23
|
+
}),
|
|
24
|
+
defineTool({
|
|
25
|
+
name: 'campaignmap_create_campaign',
|
|
26
|
+
title: 'Create a campaign',
|
|
27
|
+
description: 'Create a campaign (a container for maps). Returns the new campaign including its id.',
|
|
28
|
+
input: z.strictObject({ title, description: description.optional() }),
|
|
29
|
+
route: { method: 'POST', path: '/campaigns' },
|
|
30
|
+
body: ['title', 'description'],
|
|
31
|
+
}),
|
|
32
|
+
defineTool({
|
|
33
|
+
name: 'campaignmap_update_campaign',
|
|
34
|
+
title: 'Update a campaign',
|
|
35
|
+
description: 'Change a campaign\'s title and/or description. Send only the fields to change.',
|
|
36
|
+
input: atLeastOneOf(z.strictObject({ campaignId, title: title.optional(), description: description.optional() }), ['campaignId']),
|
|
37
|
+
route: { method: 'PATCH', path: '/campaigns/{campaignId}' },
|
|
38
|
+
body: ['title', 'description'],
|
|
39
|
+
}),
|
|
40
|
+
defineTool({
|
|
41
|
+
name: 'campaignmap_delete_campaign',
|
|
42
|
+
title: 'Delete a campaign',
|
|
43
|
+
description: 'DESTRUCTIVE. Delete a campaign. A human must confirm on the terminal where the MCP server runs; if nobody confirms nothing is deleted. ' +
|
|
44
|
+
'A campaign that still has maps is refused with 409 unless `maps` is set: "cascade" deletes its maps too, "orphan" keeps them in the Library.',
|
|
45
|
+
input: z.strictObject({
|
|
46
|
+
campaignId,
|
|
47
|
+
maps: z.enum(['cascade', 'orphan']).optional().describe('What to do with the campaign\'s maps'),
|
|
48
|
+
}),
|
|
49
|
+
route: { method: 'DELETE', path: '/campaigns/{campaignId}' },
|
|
50
|
+
query: ['maps'],
|
|
51
|
+
destructive: true,
|
|
52
|
+
confirmText: (a) => `Delete campaign ${a.campaignId}${a.maps === 'cascade' ? ' AND ALL ITS MAPS' : a.maps === 'orphan' ? ' (its maps move to the Library)' : ''}?`,
|
|
53
|
+
}),
|
|
54
|
+
];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// The tool registry (MCP-004 / 005 / 006).
|
|
2
|
+
import { assetTools } from './assets.js';
|
|
3
|
+
import { campaignTools } from './campaigns.js';
|
|
4
|
+
import { mapTools } from './maps.js';
|
|
5
|
+
import { poiTools } from './pois.js';
|
|
6
|
+
import { sessionTools } from './sessions.js';
|
|
7
|
+
import { zoneTools } from './zones.js';
|
|
8
|
+
export const tools = [
|
|
9
|
+
...campaignTools,
|
|
10
|
+
...mapTools,
|
|
11
|
+
...sessionTools,
|
|
12
|
+
...poiTools,
|
|
13
|
+
...zoneTools,
|
|
14
|
+
...assetTools,
|
|
15
|
+
];
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { atLeastOneOf, colorHex, description, mapId, title } from './schemas.js';
|
|
3
|
+
import { defineTool } from './types.js';
|
|
4
|
+
const campaignIdOrNull = z.uuid().nullable();
|
|
5
|
+
export const mapTools = [
|
|
6
|
+
defineTool({
|
|
7
|
+
name: 'campaignmap_list_maps',
|
|
8
|
+
title: 'List maps',
|
|
9
|
+
description: 'List the user\'s maps (with sessionCount and lastUsedAt). Filter by campaign with `campaignId`, or pass "none" for maps outside any campaign (the Library).',
|
|
10
|
+
input: z.strictObject({
|
|
11
|
+
page: z.number().int().min(1).optional().describe('Page, default 1'),
|
|
12
|
+
pageSize: z.number().int().min(1).max(100).optional().describe('Items per page, default 20'),
|
|
13
|
+
campaignId: z
|
|
14
|
+
.union([z.uuid(), z.literal('none')])
|
|
15
|
+
.optional()
|
|
16
|
+
.describe('Campaign id, or "none" for the Library'),
|
|
17
|
+
}),
|
|
18
|
+
route: { method: 'GET', path: '/maps' },
|
|
19
|
+
query: ['page', 'pageSize', 'campaignId'],
|
|
20
|
+
}),
|
|
21
|
+
defineTool({
|
|
22
|
+
name: 'campaignmap_get_map',
|
|
23
|
+
title: 'Get a map',
|
|
24
|
+
description: 'Get one map\'s details (title, mapType image | hex, campaign, colours).',
|
|
25
|
+
input: z.strictObject({ mapId }),
|
|
26
|
+
route: { method: 'GET', path: '/maps/{mapId}' },
|
|
27
|
+
}),
|
|
28
|
+
defineTool({
|
|
29
|
+
name: 'campaignmap_export_map',
|
|
30
|
+
title: 'Export a map',
|
|
31
|
+
description: 'Return the map as the v1-compatible JSON document (sessions, POIs, zones, ...). Prefer this over the individual list tools when you need the whole map; ' +
|
|
32
|
+
'it can be large. Image bytes are not included.',
|
|
33
|
+
input: z.strictObject({ mapId }),
|
|
34
|
+
route: { method: 'GET', path: '/maps/{mapId}/export' },
|
|
35
|
+
}),
|
|
36
|
+
defineTool({
|
|
37
|
+
name: 'campaignmap_create_map',
|
|
38
|
+
title: 'Create a map',
|
|
39
|
+
description: 'Create an empty map. mapType "image" (default) is a background-image map; "hex" needs hexConfig { orientation, hexSize }. ' +
|
|
40
|
+
'Optionally place it in a campaign with campaignId. The new map comes with its first session.',
|
|
41
|
+
input: z
|
|
42
|
+
.strictObject({
|
|
43
|
+
title,
|
|
44
|
+
description: description.optional(),
|
|
45
|
+
campaignId: campaignIdOrNull.optional().describe('Campaign to put the map in; omit or null for the Library'),
|
|
46
|
+
mapType: z.enum(['image', 'hex']).optional().describe('Default "image"'),
|
|
47
|
+
hexConfig: z
|
|
48
|
+
.strictObject({
|
|
49
|
+
orientation: z.enum(['pointy', 'flat']),
|
|
50
|
+
hexSize: z.number().int().min(8).max(256).describe('Hex circumradius in pixels (8..256)'),
|
|
51
|
+
})
|
|
52
|
+
.optional()
|
|
53
|
+
.describe('Required for mapType "hex", not allowed for "image"'),
|
|
54
|
+
})
|
|
55
|
+
.refine((o) => (o.mapType === 'hex') === (o.hexConfig !== undefined), {
|
|
56
|
+
message: 'hexConfig is required for a hex map and not allowed for an image map',
|
|
57
|
+
}),
|
|
58
|
+
route: { method: 'POST', path: '/maps' },
|
|
59
|
+
body: ['title', 'description', 'campaignId', 'mapType', 'hexConfig'],
|
|
60
|
+
}),
|
|
61
|
+
defineTool({
|
|
62
|
+
name: 'campaignmap_update_map',
|
|
63
|
+
title: 'Update a map',
|
|
64
|
+
description: 'Change a map\'s title, description, campaign (null = move to the Library), backgroundColor or gridColor (#rrggbb, null = default). Send only what changes.',
|
|
65
|
+
input: atLeastOneOf(z.strictObject({
|
|
66
|
+
mapId,
|
|
67
|
+
title: title.optional(),
|
|
68
|
+
description: description.optional(),
|
|
69
|
+
campaignId: campaignIdOrNull.optional(),
|
|
70
|
+
backgroundColor: colorHex.nullable().optional(),
|
|
71
|
+
gridColor: colorHex.nullable().optional(),
|
|
72
|
+
}), ['mapId']),
|
|
73
|
+
route: { method: 'PATCH', path: '/maps/{mapId}' },
|
|
74
|
+
body: ['title', 'description', 'campaignId', 'backgroundColor', 'gridColor'],
|
|
75
|
+
}),
|
|
76
|
+
defineTool({
|
|
77
|
+
name: 'campaignmap_duplicate_map',
|
|
78
|
+
title: 'Duplicate a map',
|
|
79
|
+
description: 'Copy a map with all its sessions, POIs and zones. By default the copy stays in the same campaign; pass campaignId (or null for the Library) to put it elsewhere.',
|
|
80
|
+
input: z.strictObject({
|
|
81
|
+
mapId,
|
|
82
|
+
campaignId: z.uuid().nullable().optional().describe('Target campaign; null = Library; omit = same as source'),
|
|
83
|
+
}),
|
|
84
|
+
route: { method: 'POST', path: '/maps/{mapId}/duplicate' },
|
|
85
|
+
body: ['campaignId'],
|
|
86
|
+
}),
|
|
87
|
+
defineTool({
|
|
88
|
+
name: 'campaignmap_delete_map',
|
|
89
|
+
title: 'Delete a map',
|
|
90
|
+
description: 'DESTRUCTIVE. Permanently delete a map and everything on it. A human must confirm on the terminal where the MCP server runs; if nobody confirms nothing is deleted.',
|
|
91
|
+
input: z.strictObject({ mapId }),
|
|
92
|
+
route: { method: 'DELETE', path: '/maps/{mapId}' },
|
|
93
|
+
destructive: true,
|
|
94
|
+
confirmText: (a) => `Permanently delete map ${a.mapId} and everything on it?`,
|
|
95
|
+
}),
|
|
96
|
+
];
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { atLeastOneOf, colorHex, coords, description, id, mapId, sessionIdField, slug, state, title, visibility, } from './schemas.js';
|
|
3
|
+
import { defineTool } from './types.js';
|
|
4
|
+
const poiId = id('POI id');
|
|
5
|
+
const category = slug.describe('POI category slug from list_poi_categories, e.g. "location"; an unknown slug is rejected by the API');
|
|
6
|
+
const iconId = slug.describe('POI icon slug');
|
|
7
|
+
export const poiTools = [
|
|
8
|
+
defineTool({
|
|
9
|
+
name: 'campaignmap_list_poi_categories',
|
|
10
|
+
title: 'List POI categories',
|
|
11
|
+
description: 'List the valid POI category slugs (with name and colour). Call this before create_poi to choose a `category`.',
|
|
12
|
+
input: z.strictObject({}),
|
|
13
|
+
route: { method: 'GET', path: '/poi-categories' },
|
|
14
|
+
}),
|
|
15
|
+
defineTool({
|
|
16
|
+
name: 'campaignmap_list_pois',
|
|
17
|
+
title: 'List POIs',
|
|
18
|
+
description: 'List the points of interest on a map, optionally only those of one session.',
|
|
19
|
+
input: z.strictObject({ mapId, sessionId: sessionIdField.optional() }),
|
|
20
|
+
route: { method: 'GET', path: '/maps/{mapId}/pois' },
|
|
21
|
+
query: ['sessionId'],
|
|
22
|
+
}),
|
|
23
|
+
defineTool({
|
|
24
|
+
name: 'campaignmap_create_poi',
|
|
25
|
+
title: 'Create a POI',
|
|
26
|
+
description: 'Place a point of interest. `sessionId` (see list_sessions) and `coords` are required: { x, y } on image maps, { q, r, s } cube coordinates on hex maps. ' +
|
|
27
|
+
'visibility "gm" (default) keeps it out of the public share view; "players" publishes it.',
|
|
28
|
+
input: z.strictObject({
|
|
29
|
+
mapId,
|
|
30
|
+
sessionId: sessionIdField,
|
|
31
|
+
name: title,
|
|
32
|
+
category,
|
|
33
|
+
coords,
|
|
34
|
+
iconId: iconId.nullable().optional(),
|
|
35
|
+
color: colorHex.nullable().optional(),
|
|
36
|
+
description: description.optional(),
|
|
37
|
+
state: state.optional(),
|
|
38
|
+
visibility: visibility.optional(),
|
|
39
|
+
}),
|
|
40
|
+
route: { method: 'POST', path: '/maps/{mapId}/pois' },
|
|
41
|
+
body: ['sessionId', 'name', 'category', 'coords', 'iconId', 'color', 'description', 'state', 'visibility'],
|
|
42
|
+
}),
|
|
43
|
+
defineTool({
|
|
44
|
+
name: 'campaignmap_update_poi',
|
|
45
|
+
title: 'Update a POI',
|
|
46
|
+
description: 'Change a POI (rename, move via coords, recolour, change state / visibility, move to another session). Send only what changes.',
|
|
47
|
+
input: atLeastOneOf(z.strictObject({
|
|
48
|
+
mapId,
|
|
49
|
+
poiId,
|
|
50
|
+
sessionId: sessionIdField.optional(),
|
|
51
|
+
name: title.optional(),
|
|
52
|
+
category: category.optional(),
|
|
53
|
+
coords: coords.optional(),
|
|
54
|
+
iconId: iconId.nullable().optional(),
|
|
55
|
+
color: colorHex.nullable().optional(),
|
|
56
|
+
description: description.optional(),
|
|
57
|
+
state: state.optional(),
|
|
58
|
+
visibility: visibility.optional(),
|
|
59
|
+
}), ['mapId', 'poiId']),
|
|
60
|
+
route: { method: 'PATCH', path: '/maps/{mapId}/pois/{poiId}' },
|
|
61
|
+
body: ['sessionId', 'name', 'category', 'coords', 'iconId', 'color', 'description', 'state', 'visibility'],
|
|
62
|
+
}),
|
|
63
|
+
defineTool({
|
|
64
|
+
name: 'campaignmap_delete_poi',
|
|
65
|
+
title: 'Delete a POI',
|
|
66
|
+
description: 'DESTRUCTIVE. Delete a POI. A human must confirm on the terminal where the MCP server runs; if nobody confirms nothing is deleted. ' +
|
|
67
|
+
'To hide it reversibly use update_poi with state "removed" or "disabled".',
|
|
68
|
+
input: z.strictObject({ mapId, poiId }),
|
|
69
|
+
route: { method: 'DELETE', path: '/maps/{mapId}/pois/{poiId}' },
|
|
70
|
+
destructive: true,
|
|
71
|
+
confirmText: (a) => `Delete POI ${a.poiId} from map ${a.mapId}?`,
|
|
72
|
+
}),
|
|
73
|
+
];
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { envelope } from '../apiClient.js';
|
|
2
|
+
import { refusal } from '../confirm.js';
|
|
3
|
+
function pick(args, keys) {
|
|
4
|
+
if (!keys)
|
|
5
|
+
return undefined;
|
|
6
|
+
const out = {};
|
|
7
|
+
for (const k of keys)
|
|
8
|
+
if (args[k] !== undefined)
|
|
9
|
+
out[k] = args[k];
|
|
10
|
+
return out;
|
|
11
|
+
}
|
|
12
|
+
export async function runTool(tool, rawArgs, api, confirm) {
|
|
13
|
+
const parsed = tool.input.safeParse(rawArgs ?? {});
|
|
14
|
+
if (!parsed.success) {
|
|
15
|
+
const message = parsed.error.issues.map((i) => `${i.path.join('.') || '(input)'}: ${i.message}`).join('; ');
|
|
16
|
+
return envelope(400, 'Bad Request', `Invalid arguments for ${tool.name}: ${message}`);
|
|
17
|
+
}
|
|
18
|
+
const args = parsed.data;
|
|
19
|
+
if (tool.destructive) {
|
|
20
|
+
const question = tool.confirmText?.(args) ?? `Run ${tool.name}?`;
|
|
21
|
+
if (!(await confirm(question)))
|
|
22
|
+
return refusal(tool.title);
|
|
23
|
+
}
|
|
24
|
+
if (tool.run)
|
|
25
|
+
return tool.run(args, api);
|
|
26
|
+
const path = tool.route.path.replace(/\{(\w+)\}/g, (_, key) => encodeURIComponent(String(args[key])));
|
|
27
|
+
const query = pick(args, tool.query);
|
|
28
|
+
const body = pick(args, tool.body);
|
|
29
|
+
return api.request({
|
|
30
|
+
method: tool.route.method,
|
|
31
|
+
path,
|
|
32
|
+
query,
|
|
33
|
+
// POST / PATCH always carry a JSON object (the API's strict schemas reject a missing body).
|
|
34
|
+
body: tool.route.method === 'POST' || tool.route.method === 'PATCH' ? (body ?? {}) : undefined,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Field schemas shared by several tools. They mirror api/src/types/* so a bad
|
|
2
|
+
// call fails before the request; the API stays the authority and re-validates.
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
export const id = (what) => z.uuid().describe(`${what} (UUID)`);
|
|
5
|
+
export const mapId = id('Map id');
|
|
6
|
+
export const sessionIdField = id('Session id');
|
|
7
|
+
export const title = z.string().trim().min(1).max(255);
|
|
8
|
+
export const description = z.string().max(10000).nullable();
|
|
9
|
+
export const colorHex = z.string().regex(/^#[0-9a-fA-F]{6}$/, 'Use #rrggbb');
|
|
10
|
+
export const state = z.enum(['active', 'disabled', 'removed']);
|
|
11
|
+
export const visibility = z.enum(['gm', 'players']);
|
|
12
|
+
export const coords = z
|
|
13
|
+
.union([
|
|
14
|
+
z.strictObject({ x: z.number().finite(), y: z.number().finite() }),
|
|
15
|
+
z.strictObject({
|
|
16
|
+
q: z.number().finite(),
|
|
17
|
+
r: z.number().finite(),
|
|
18
|
+
s: z.number().finite(),
|
|
19
|
+
slot: z.number().int().min(0).max(6).optional(),
|
|
20
|
+
}),
|
|
21
|
+
])
|
|
22
|
+
.describe('Image map: { x, y } in image pixels. Hex map: cube coordinates { q, r, s } (q + r + s = 0) with optional slot 0..6 (0 = centre).');
|
|
23
|
+
export const slug = z.string().regex(/^[a-z0-9-]{1,40}$/, 'Lowercase letters, digits and dashes (max 40)');
|
|
24
|
+
/** Update tools need at least one field besides the identifying ones (same rule as the API). */
|
|
25
|
+
export function atLeastOneOf(schema, idKeys) {
|
|
26
|
+
return schema.refine((o) => Object.entries(o).some(([k, v]) => !idKeys.includes(k) && v !== undefined), {
|
|
27
|
+
message: 'At least one field to change is required',
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { atLeastOneOf, description, id, mapId, title } from './schemas.js';
|
|
3
|
+
import { defineTool } from './types.js';
|
|
4
|
+
const sessionId = id('Session id');
|
|
5
|
+
export const sessionTools = [
|
|
6
|
+
defineTool({
|
|
7
|
+
name: 'campaignmap_list_sessions',
|
|
8
|
+
title: 'List sessions of a map',
|
|
9
|
+
description: 'List a map\'s sessions (play-session snapshots; POIs and zones belong to one). Needed to get a sessionId for creating POIs and zones.',
|
|
10
|
+
input: z.strictObject({ mapId }),
|
|
11
|
+
route: { method: 'GET', path: '/maps/{mapId}/sessions' },
|
|
12
|
+
}),
|
|
13
|
+
defineTool({
|
|
14
|
+
name: 'campaignmap_get_session',
|
|
15
|
+
title: 'Get a session',
|
|
16
|
+
description: 'Get one session of a map.',
|
|
17
|
+
input: z.strictObject({ mapId, sessionId }),
|
|
18
|
+
route: { method: 'GET', path: '/maps/{mapId}/sessions/{sessionId}' },
|
|
19
|
+
}),
|
|
20
|
+
defineTool({
|
|
21
|
+
name: 'campaignmap_create_session',
|
|
22
|
+
title: 'Create a session',
|
|
23
|
+
description: 'Add a session to a map. title defaults to "Session N". On hex maps copyFromSessionId copies that session\'s painted grid into the new one.',
|
|
24
|
+
input: z.strictObject({
|
|
25
|
+
mapId,
|
|
26
|
+
title: title.optional(),
|
|
27
|
+
description: description.optional(),
|
|
28
|
+
copyFromSessionId: z.uuid().optional().describe('Hex maps only: session whose grid to copy'),
|
|
29
|
+
}),
|
|
30
|
+
route: { method: 'POST', path: '/maps/{mapId}/sessions' },
|
|
31
|
+
body: ['title', 'description', 'copyFromSessionId'],
|
|
32
|
+
}),
|
|
33
|
+
defineTool({
|
|
34
|
+
name: 'campaignmap_update_session',
|
|
35
|
+
title: 'Update a session',
|
|
36
|
+
description: 'Rename a session or change its description.',
|
|
37
|
+
input: atLeastOneOf(z.strictObject({ mapId, sessionId, title: title.optional(), description: description.optional() }), ['mapId', 'sessionId']),
|
|
38
|
+
route: { method: 'PATCH', path: '/maps/{mapId}/sessions/{sessionId}' },
|
|
39
|
+
body: ['title', 'description'],
|
|
40
|
+
}),
|
|
41
|
+
defineTool({
|
|
42
|
+
name: 'campaignmap_delete_session',
|
|
43
|
+
title: 'Delete a session',
|
|
44
|
+
description: 'DESTRUCTIVE. Delete a session and its POIs / zones. A human must confirm on the terminal where the MCP server runs; if nobody confirms nothing is deleted.',
|
|
45
|
+
input: z.strictObject({ mapId, sessionId }),
|
|
46
|
+
route: { method: 'DELETE', path: '/maps/{mapId}/sessions/{sessionId}' },
|
|
47
|
+
destructive: true,
|
|
48
|
+
confirmText: (a) => `Delete session ${a.sessionId} of map ${a.mapId}?`,
|
|
49
|
+
}),
|
|
50
|
+
];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const defineTool = (t) => t;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { atLeastOneOf, coords, description, id, mapId, sessionIdField, state, title, visibility } from './schemas.js';
|
|
3
|
+
import { defineTool } from './types.js';
|
|
4
|
+
const zoneId = id('Zone id');
|
|
5
|
+
const color = z.string().min(1).max(20).describe('Fill colour, e.g. "#d946ef"');
|
|
6
|
+
const polygon = z
|
|
7
|
+
.array(coords)
|
|
8
|
+
.min(1)
|
|
9
|
+
.describe('Vertices in order, same coordinate system as POIs (image { x, y } or hex { q, r, s }).');
|
|
10
|
+
export const zoneTools = [
|
|
11
|
+
defineTool({
|
|
12
|
+
name: 'campaignmap_list_zones',
|
|
13
|
+
title: 'List zones',
|
|
14
|
+
description: 'List the zones (polygons / hex areas) on a map, optionally only those of one session.',
|
|
15
|
+
input: z.strictObject({ mapId, sessionId: sessionIdField.optional() }),
|
|
16
|
+
route: { method: 'GET', path: '/maps/{mapId}/zones' },
|
|
17
|
+
query: ['sessionId'],
|
|
18
|
+
}),
|
|
19
|
+
defineTool({
|
|
20
|
+
name: 'campaignmap_create_zone',
|
|
21
|
+
title: 'Create a zone',
|
|
22
|
+
description: 'Create a zone from a polygon. `sessionId` (see list_sessions) and `polygon` are required.',
|
|
23
|
+
input: z.strictObject({
|
|
24
|
+
mapId,
|
|
25
|
+
sessionId: sessionIdField,
|
|
26
|
+
name: title,
|
|
27
|
+
polygon,
|
|
28
|
+
color: color.optional(),
|
|
29
|
+
description: description.optional(),
|
|
30
|
+
state: state.optional(),
|
|
31
|
+
visibility: visibility.optional(),
|
|
32
|
+
}),
|
|
33
|
+
route: { method: 'POST', path: '/maps/{mapId}/zones' },
|
|
34
|
+
body: ['sessionId', 'name', 'polygon', 'color', 'description', 'state', 'visibility'],
|
|
35
|
+
}),
|
|
36
|
+
defineTool({
|
|
37
|
+
name: 'campaignmap_update_zone',
|
|
38
|
+
title: 'Update a zone',
|
|
39
|
+
description: 'Change a zone (rename, reshape via polygon, recolour, state / visibility, move to another session). Send only what changes.',
|
|
40
|
+
input: atLeastOneOf(z.strictObject({
|
|
41
|
+
mapId,
|
|
42
|
+
zoneId,
|
|
43
|
+
sessionId: sessionIdField.optional(),
|
|
44
|
+
name: title.optional(),
|
|
45
|
+
polygon: polygon.optional(),
|
|
46
|
+
color: color.optional(),
|
|
47
|
+
description: description.optional(),
|
|
48
|
+
state: state.optional(),
|
|
49
|
+
visibility: visibility.optional(),
|
|
50
|
+
}), ['mapId', 'zoneId']),
|
|
51
|
+
route: { method: 'PATCH', path: '/maps/{mapId}/zones/{zoneId}' },
|
|
52
|
+
body: ['sessionId', 'name', 'polygon', 'color', 'description', 'state', 'visibility'],
|
|
53
|
+
}),
|
|
54
|
+
defineTool({
|
|
55
|
+
name: 'campaignmap_delete_zone',
|
|
56
|
+
title: 'Delete a zone',
|
|
57
|
+
description: 'DESTRUCTIVE. Delete a zone. A human must confirm on the terminal where the MCP server runs; if nobody confirms nothing is deleted.',
|
|
58
|
+
input: z.strictObject({ mapId, zoneId }),
|
|
59
|
+
route: { method: 'DELETE', path: '/maps/{mapId}/zones/{zoneId}' },
|
|
60
|
+
destructive: true,
|
|
61
|
+
confirmText: (a) => `Delete zone ${a.zoneId} from map ${a.mapId}?`,
|
|
62
|
+
}),
|
|
63
|
+
];
|