@hanna84/mcp-writing 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +193 -0
- package/db.js +127 -0
- package/index.js +892 -0
- package/metadata-lint.js +332 -0
- package/package.json +42 -0
- package/scripts/import.js +360 -0
- package/scripts/lint-metadata.mjs +28 -0
- package/scripts/merge-scrivx.js +319 -0
- package/scripts/split-versions.js +26 -0
- package/sync.js +431 -0
package/metadata-lint.js
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { parseFile, sidecarPath, walkFiles, walkSidecars } from "./sync.js";
|
|
5
|
+
import yaml from "js-yaml";
|
|
6
|
+
|
|
7
|
+
const { load: parseYaml } = yaml;
|
|
8
|
+
|
|
9
|
+
const metadataKindSchema = z.enum(["scene", "character", "place"]);
|
|
10
|
+
|
|
11
|
+
const threadLinkSchema = z.object({
|
|
12
|
+
thread_id: z.string().min(1),
|
|
13
|
+
beat: z.string().min(1).optional(),
|
|
14
|
+
thread_name: z.string().min(1).optional(),
|
|
15
|
+
status: z.string().min(1).optional(),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const sceneSchema = z.object({
|
|
19
|
+
scene_id: z.string().min(1),
|
|
20
|
+
title: z.string().min(1).optional(),
|
|
21
|
+
part: z.number().int().positive().optional(),
|
|
22
|
+
chapter: z.number().int().positive().optional(),
|
|
23
|
+
pov: z.string().min(1).optional(),
|
|
24
|
+
logline: z.string().min(1).optional(),
|
|
25
|
+
save_the_cat_beat: z.string().min(1).optional(),
|
|
26
|
+
timeline_position: z.number().int().optional(),
|
|
27
|
+
story_time: z.string().min(1).optional(),
|
|
28
|
+
word_count: z.number().int().nonnegative().optional(),
|
|
29
|
+
scene_change: z.string().min(1).optional(),
|
|
30
|
+
causality: z.string().min(1).optional(),
|
|
31
|
+
stakes: z.string().min(1).optional(),
|
|
32
|
+
scene_functions: z.array(z.string().min(1)).optional(),
|
|
33
|
+
characters: z.array(z.string().min(1)).optional(),
|
|
34
|
+
places: z.array(z.string().min(1)).optional(),
|
|
35
|
+
tags: z.array(z.string().min(1)).optional(),
|
|
36
|
+
versions: z.array(z.string().min(1)).optional(),
|
|
37
|
+
threads: z.array(threadLinkSchema).optional(),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const characterSchema = z.object({
|
|
41
|
+
character_id: z.string().min(1),
|
|
42
|
+
name: z.string().min(1).optional(),
|
|
43
|
+
role: z.string().min(1).optional(),
|
|
44
|
+
group: z.string().min(1).optional(),
|
|
45
|
+
arc_summary: z.string().min(1).optional(),
|
|
46
|
+
first_appearance: z.string().min(1).optional(),
|
|
47
|
+
traits: z.array(z.string().min(1)).optional(),
|
|
48
|
+
tags: z.array(z.string().min(1)).optional(),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const placeSchema = z.object({
|
|
52
|
+
place_id: z.string().min(1),
|
|
53
|
+
name: z.string().min(1).optional(),
|
|
54
|
+
associated_characters: z.array(z.string().min(1)).optional(),
|
|
55
|
+
tags: z.array(z.string().min(1)).optional(),
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const sceneAllowedKeys = new Set(Object.keys(sceneSchema.shape));
|
|
59
|
+
const characterAllowedKeys = new Set(Object.keys(characterSchema.shape));
|
|
60
|
+
const placeAllowedKeys = new Set(Object.keys(placeSchema.shape));
|
|
61
|
+
const sceneLegacyKeys = new Set(["synopsis", "save_the_cat", "change"]);
|
|
62
|
+
|
|
63
|
+
function uniqueItems(items = []) {
|
|
64
|
+
return new Set(items).size === items.length;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function detectMetadataKind(meta) {
|
|
68
|
+
if (meta && typeof meta === "object") {
|
|
69
|
+
if (typeof meta.character_id === "string") return "character";
|
|
70
|
+
if (typeof meta.place_id === "string") return "place";
|
|
71
|
+
return "scene";
|
|
72
|
+
}
|
|
73
|
+
return "scene";
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function allowedKeysFor(kind) {
|
|
77
|
+
if (kind === "character") return characterAllowedKeys;
|
|
78
|
+
if (kind === "place") return placeAllowedKeys;
|
|
79
|
+
return sceneAllowedKeys;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function schemaFor(kind) {
|
|
83
|
+
if (kind === "character") return characterSchema;
|
|
84
|
+
if (kind === "place") return placeSchema;
|
|
85
|
+
return sceneSchema;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function validateUniqueArrays(meta, kind, issues) {
|
|
89
|
+
const fields = kind === "scene"
|
|
90
|
+
? ["characters", "places", "tags", "scene_functions", "versions"]
|
|
91
|
+
: kind === "character"
|
|
92
|
+
? ["traits", "tags"]
|
|
93
|
+
: ["associated_characters", "tags"];
|
|
94
|
+
|
|
95
|
+
for (const key of fields) {
|
|
96
|
+
if (Array.isArray(meta[key]) && !uniqueItems(meta[key])) {
|
|
97
|
+
issues.push({
|
|
98
|
+
level: "warning",
|
|
99
|
+
code: "DUPLICATE_ARRAY_ITEMS",
|
|
100
|
+
message: `Array '${key}' contains duplicate values.`,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function validateMetadataObject(meta, { sourcePath, kindHint } = {}) {
|
|
107
|
+
const issues = [];
|
|
108
|
+
const kind = metadataKindSchema.parse(kindHint ?? detectMetadataKind(meta));
|
|
109
|
+
const schema = schemaFor(kind);
|
|
110
|
+
const allowed = allowedKeysFor(kind);
|
|
111
|
+
|
|
112
|
+
if (!meta || typeof meta !== "object" || Array.isArray(meta)) {
|
|
113
|
+
return {
|
|
114
|
+
ok: false,
|
|
115
|
+
kind,
|
|
116
|
+
issues: [{
|
|
117
|
+
level: "error",
|
|
118
|
+
code: "INVALID_METADATA_OBJECT",
|
|
119
|
+
message: "Metadata must be a YAML mapping/object.",
|
|
120
|
+
}],
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const parsed = schema.safeParse(meta);
|
|
125
|
+
if (!parsed.success) {
|
|
126
|
+
for (const issue of parsed.error.issues) {
|
|
127
|
+
issues.push({
|
|
128
|
+
level: "error",
|
|
129
|
+
code: "SCHEMA_VALIDATION_ERROR",
|
|
130
|
+
message: `${issue.path.join(".") || "metadata"}: ${issue.message}`,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
for (const key of Object.keys(meta)) {
|
|
136
|
+
if (kind === "scene" && sceneLegacyKeys.has(key)) {
|
|
137
|
+
issues.push({
|
|
138
|
+
level: "warning",
|
|
139
|
+
code: "LEGACY_SCENE_KEY",
|
|
140
|
+
message: `Legacy scene key '${key}' found. Prefer canonical sidecar keys.`,
|
|
141
|
+
});
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (!allowed.has(key)) {
|
|
145
|
+
issues.push({
|
|
146
|
+
level: "warning",
|
|
147
|
+
code: "UNKNOWN_KEY",
|
|
148
|
+
message: `Unknown key '${key}' for ${kind} metadata.`,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
validateUniqueArrays(meta, kind, issues);
|
|
154
|
+
|
|
155
|
+
if (kind === "scene" && sourcePath) {
|
|
156
|
+
const sidecar = sourcePath.endsWith(".meta.yaml");
|
|
157
|
+
if (sidecar && !meta.scene_id) {
|
|
158
|
+
issues.push({
|
|
159
|
+
level: "error",
|
|
160
|
+
code: "MISSING_SCENE_ID",
|
|
161
|
+
message: "Scene sidecar is missing required 'scene_id'.",
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const hasErrors = issues.some(i => i.level === "error");
|
|
167
|
+
return { ok: !hasErrors, kind, issues };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function loadYamlFile(filePath) {
|
|
171
|
+
try {
|
|
172
|
+
const raw = fs.readFileSync(filePath, "utf8");
|
|
173
|
+
const parsed = parseYaml(raw);
|
|
174
|
+
return { ok: true, value: parsed ?? {} };
|
|
175
|
+
} catch (err) {
|
|
176
|
+
return {
|
|
177
|
+
ok: false,
|
|
178
|
+
error: {
|
|
179
|
+
level: "error",
|
|
180
|
+
code: "YAML_PARSE_ERROR",
|
|
181
|
+
message: `Failed to parse YAML: ${err.message}`,
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function lintSidecar(filePath) {
|
|
188
|
+
const loaded = loadYamlFile(filePath);
|
|
189
|
+
if (!loaded.ok) {
|
|
190
|
+
return {
|
|
191
|
+
file: filePath,
|
|
192
|
+
kind: "scene",
|
|
193
|
+
issues: [loaded.error],
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
const result = validateMetadataObject(loaded.value, { sourcePath: filePath });
|
|
197
|
+
return { file: filePath, kind: result.kind, issues: result.issues };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function lintFrontmatter(filePath) {
|
|
201
|
+
try {
|
|
202
|
+
const { data } = parseFile(filePath);
|
|
203
|
+
if (!data || !Object.keys(data).length) return null;
|
|
204
|
+
const result = validateMetadataObject(data, { sourcePath: filePath });
|
|
205
|
+
return { file: filePath, kind: result.kind, issues: result.issues };
|
|
206
|
+
} catch (err) {
|
|
207
|
+
return {
|
|
208
|
+
file: filePath,
|
|
209
|
+
kind: "scene",
|
|
210
|
+
issues: [{
|
|
211
|
+
level: "error",
|
|
212
|
+
code: "FRONTMATTER_PARSE_ERROR",
|
|
213
|
+
message: `Failed to parse frontmatter: ${err.message}`,
|
|
214
|
+
}],
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function compareSidecarAndFrontmatter(filePath, reports) {
|
|
220
|
+
const sidecar = sidecarPath(filePath);
|
|
221
|
+
if (!fs.existsSync(sidecar)) return;
|
|
222
|
+
const sc = reports.find(r => r.file === sidecar);
|
|
223
|
+
if (!sc) return;
|
|
224
|
+
|
|
225
|
+
try {
|
|
226
|
+
const { data } = parseFile(filePath);
|
|
227
|
+
if (!data || !Object.keys(data).length) return;
|
|
228
|
+
const sidecarData = parseYaml(fs.readFileSync(sidecar, "utf8")) ?? {};
|
|
229
|
+
|
|
230
|
+
if (typeof data.scene_id === "string" && typeof sidecarData.scene_id === "string" && data.scene_id !== sidecarData.scene_id) {
|
|
231
|
+
sc.issues.push({
|
|
232
|
+
level: "warning",
|
|
233
|
+
code: "SCENE_ID_MISMATCH",
|
|
234
|
+
message: `scene_id mismatch between frontmatter ('${data.scene_id}') and sidecar ('${sidecarData.scene_id}').`,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
} catch {
|
|
238
|
+
// Parsing failures are already surfaced by individual report entries.
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function lintMetadataInSyncDir(syncDir) {
|
|
243
|
+
const reports = [];
|
|
244
|
+
const files = walkFiles(syncDir);
|
|
245
|
+
|
|
246
|
+
for (const sidecar of walkSidecars(syncDir)) {
|
|
247
|
+
reports.push(lintSidecar(sidecar));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
for (const file of files) {
|
|
251
|
+
if (fs.existsSync(sidecarPath(file))) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const frontmatterReport = lintFrontmatter(file);
|
|
255
|
+
if (frontmatterReport) {
|
|
256
|
+
reports.push(frontmatterReport);
|
|
257
|
+
} else {
|
|
258
|
+
// No sidecar and no frontmatter — file will be silently skipped during sync
|
|
259
|
+
reports.push({
|
|
260
|
+
file,
|
|
261
|
+
kind: "scene",
|
|
262
|
+
issues: [{
|
|
263
|
+
level: "warning",
|
|
264
|
+
code: "NO_METADATA",
|
|
265
|
+
message: "File has no sidecar and no frontmatter — will be skipped during sync (no scene_id).",
|
|
266
|
+
}],
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
for (const file of files) {
|
|
272
|
+
compareSidecarAndFrontmatter(file, reports);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// --- Duplicate scene_id detection (cross-file, errors) ---
|
|
276
|
+
const sceneIdToFiles = new Map(); // scene_id → [filePath, ...]
|
|
277
|
+
|
|
278
|
+
for (const sidecar of walkSidecars(syncDir)) {
|
|
279
|
+
try {
|
|
280
|
+
const raw = fs.readFileSync(sidecar, "utf8");
|
|
281
|
+
const meta = parseYaml(raw) ?? {};
|
|
282
|
+
if (typeof meta.scene_id === "string" && meta.scene_id) {
|
|
283
|
+
const arr = sceneIdToFiles.get(meta.scene_id) ?? [];
|
|
284
|
+
arr.push(sidecar);
|
|
285
|
+
sceneIdToFiles.set(meta.scene_id, arr);
|
|
286
|
+
}
|
|
287
|
+
} catch {}
|
|
288
|
+
}
|
|
289
|
+
for (const file of files) {
|
|
290
|
+
if (fs.existsSync(sidecarPath(file))) continue; // already counted via sidecar
|
|
291
|
+
try {
|
|
292
|
+
const { data } = parseFile(file);
|
|
293
|
+
if (typeof data.scene_id === "string" && data.scene_id) {
|
|
294
|
+
const arr = sceneIdToFiles.get(data.scene_id) ?? [];
|
|
295
|
+
arr.push(file);
|
|
296
|
+
sceneIdToFiles.set(data.scene_id, arr);
|
|
297
|
+
}
|
|
298
|
+
} catch {}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
for (const [sceneId, dupeFiles] of sceneIdToFiles) {
|
|
302
|
+
if (dupeFiles.length < 2) continue;
|
|
303
|
+
const relPaths = dupeFiles.map(f => path.relative(syncDir, f)).join(", ");
|
|
304
|
+
for (const f of dupeFiles) {
|
|
305
|
+
const report = reports.find(r => r.file === f);
|
|
306
|
+
const issue = {
|
|
307
|
+
level: "error",
|
|
308
|
+
code: "DUPLICATE_SCENE_ID",
|
|
309
|
+
message: `scene_id "${sceneId}" is used by ${dupeFiles.length} files: ${relPaths}`,
|
|
310
|
+
};
|
|
311
|
+
if (report) {
|
|
312
|
+
report.issues.push(issue);
|
|
313
|
+
} else {
|
|
314
|
+
reports.push({ file: f, kind: "scene", issues: [issue] });
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const errors = reports.flatMap(r => r.issues.map(i => ({ ...i, file: r.file, kind: r.kind }))).filter(i => i.level === "error");
|
|
320
|
+
const warnings = reports.flatMap(r => r.issues.map(i => ({ ...i, file: r.file, kind: r.kind }))).filter(i => i.level === "warning");
|
|
321
|
+
|
|
322
|
+
return {
|
|
323
|
+
ok: errors.length === 0,
|
|
324
|
+
syncDir: path.resolve(syncDir),
|
|
325
|
+
files_checked: reports.length,
|
|
326
|
+
error_count: errors.length,
|
|
327
|
+
warning_count: warnings.length,
|
|
328
|
+
errors,
|
|
329
|
+
warnings,
|
|
330
|
+
reports,
|
|
331
|
+
};
|
|
332
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hanna84/mcp-writing",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "MCP service for AI-assisted reasoning and editing on long-form fiction projects",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"db.js",
|
|
10
|
+
"sync.js",
|
|
11
|
+
"metadata-lint.js",
|
|
12
|
+
"scripts/",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"start": "node --experimental-sqlite index.js",
|
|
20
|
+
"lint:metadata": "node scripts/lint-metadata.mjs",
|
|
21
|
+
"lint:metadata:test": "node scripts/lint-metadata.mjs --sync-dir ./test-sync",
|
|
22
|
+
"test:unit": "node --experimental-sqlite --test test/unit.test.mjs",
|
|
23
|
+
"test:integration": "node --experimental-sqlite --test test/integration.test.mjs",
|
|
24
|
+
"test": "node --experimental-sqlite --test test/unit.test.mjs test/integration.test.mjs"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"mcp",
|
|
28
|
+
"writing",
|
|
29
|
+
"fiction",
|
|
30
|
+
"ai",
|
|
31
|
+
"scrivener"
|
|
32
|
+
],
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
36
|
+
"@xmldom/xmldom": "^0.9.9",
|
|
37
|
+
"fast-xml-parser": "^5.6.0",
|
|
38
|
+
"gray-matter": "^4.0.3",
|
|
39
|
+
"js-yaml": "^4.1.1",
|
|
40
|
+
"zod": "^4.3.6"
|
|
41
|
+
}
|
|
42
|
+
}
|