scrivener-mcp 0.1.4
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 +109 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +796 -0
- package/dist/index.js.map +1 -0
- package/dist/rtf-handler.d.ts +121 -0
- package/dist/rtf-handler.d.ts.map +1 -0
- package/dist/rtf-handler.js +583 -0
- package/dist/rtf-handler.js.map +1 -0
- package/dist/scrivener-project.d.ts +199 -0
- package/dist/scrivener-project.d.ts.map +1 -0
- package/dist/scrivener-project.js +855 -0
- package/dist/scrivener-project.js.map +1 -0
- package/dist/types/internal.d.ts +86 -0
- package/dist/types/internal.d.ts.map +1 -0
- package/dist/types/internal.js +7 -0
- package/dist/types/internal.js.map +1 -0
- package/package.json +82 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,796 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
3
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
4
|
+
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
5
|
+
import { ScrivenerProject } from './scrivener-project.js';
|
|
6
|
+
import * as fs from 'fs/promises';
|
|
7
|
+
import * as path from 'path';
|
|
8
|
+
let currentProject = null;
|
|
9
|
+
const server = new Server({
|
|
10
|
+
name: 'scrivener-mcp',
|
|
11
|
+
version: '0.1.0',
|
|
12
|
+
}, {
|
|
13
|
+
capabilities: {
|
|
14
|
+
tools: {},
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
18
|
+
return {
|
|
19
|
+
tools: [
|
|
20
|
+
{
|
|
21
|
+
name: 'open_project',
|
|
22
|
+
description: 'Open a Scrivener project file',
|
|
23
|
+
inputSchema: {
|
|
24
|
+
type: 'object',
|
|
25
|
+
properties: {
|
|
26
|
+
path: {
|
|
27
|
+
type: 'string',
|
|
28
|
+
description: 'Path to the .scriv project folder',
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
required: ['path'],
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: 'get_structure',
|
|
36
|
+
description: 'Get the hierarchical structure of the project',
|
|
37
|
+
inputSchema: {
|
|
38
|
+
type: 'object',
|
|
39
|
+
properties: {},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'read_document',
|
|
44
|
+
description: 'Read the content of a specific document',
|
|
45
|
+
inputSchema: {
|
|
46
|
+
type: 'object',
|
|
47
|
+
properties: {
|
|
48
|
+
documentId: {
|
|
49
|
+
type: 'string',
|
|
50
|
+
description: 'UUID of the document to read',
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
required: ['documentId'],
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: 'write_document',
|
|
58
|
+
description: 'Write content to a specific document',
|
|
59
|
+
inputSchema: {
|
|
60
|
+
type: 'object',
|
|
61
|
+
properties: {
|
|
62
|
+
documentId: {
|
|
63
|
+
type: 'string',
|
|
64
|
+
description: 'UUID of the document to write',
|
|
65
|
+
},
|
|
66
|
+
content: {
|
|
67
|
+
type: 'string',
|
|
68
|
+
description: 'New content for the document',
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
required: ['documentId', 'content'],
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: 'create_document',
|
|
76
|
+
description: 'Create a new document or folder',
|
|
77
|
+
inputSchema: {
|
|
78
|
+
type: 'object',
|
|
79
|
+
properties: {
|
|
80
|
+
parentId: {
|
|
81
|
+
type: 'string',
|
|
82
|
+
description: 'UUID of parent folder (null for root)',
|
|
83
|
+
nullable: true,
|
|
84
|
+
},
|
|
85
|
+
title: {
|
|
86
|
+
type: 'string',
|
|
87
|
+
description: 'Title of the new document',
|
|
88
|
+
},
|
|
89
|
+
type: {
|
|
90
|
+
type: 'string',
|
|
91
|
+
enum: ['Text', 'Folder'],
|
|
92
|
+
description: 'Type of document to create',
|
|
93
|
+
default: 'Text',
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
required: ['title'],
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: 'delete_document',
|
|
101
|
+
description: 'Delete a document or folder',
|
|
102
|
+
inputSchema: {
|
|
103
|
+
type: 'object',
|
|
104
|
+
properties: {
|
|
105
|
+
documentId: {
|
|
106
|
+
type: 'string',
|
|
107
|
+
description: 'UUID of the document to delete',
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
required: ['documentId'],
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
name: 'rename_document',
|
|
115
|
+
description: 'Rename a document or folder',
|
|
116
|
+
inputSchema: {
|
|
117
|
+
type: 'object',
|
|
118
|
+
properties: {
|
|
119
|
+
documentId: {
|
|
120
|
+
type: 'string',
|
|
121
|
+
description: 'UUID of the document to rename',
|
|
122
|
+
},
|
|
123
|
+
newTitle: {
|
|
124
|
+
type: 'string',
|
|
125
|
+
description: 'New title for the document',
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
required: ['documentId', 'newTitle'],
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
name: 'refresh_project',
|
|
133
|
+
description: 'Reload project from disk (discards unsaved changes)',
|
|
134
|
+
inputSchema: {
|
|
135
|
+
type: 'object',
|
|
136
|
+
properties: {},
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
name: 'move_document',
|
|
141
|
+
description: 'Move a document to a different location',
|
|
142
|
+
inputSchema: {
|
|
143
|
+
type: 'object',
|
|
144
|
+
properties: {
|
|
145
|
+
documentId: {
|
|
146
|
+
type: 'string',
|
|
147
|
+
description: 'UUID of the document to move',
|
|
148
|
+
},
|
|
149
|
+
newParentId: {
|
|
150
|
+
type: 'string',
|
|
151
|
+
description: 'UUID of new parent (null for root)',
|
|
152
|
+
nullable: true,
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
required: ['documentId'],
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
name: 'update_metadata',
|
|
160
|
+
description: 'Update document metadata',
|
|
161
|
+
inputSchema: {
|
|
162
|
+
type: 'object',
|
|
163
|
+
properties: {
|
|
164
|
+
documentId: {
|
|
165
|
+
type: 'string',
|
|
166
|
+
description: 'UUID of the document',
|
|
167
|
+
},
|
|
168
|
+
metadata: {
|
|
169
|
+
type: 'object',
|
|
170
|
+
properties: {
|
|
171
|
+
title: { type: 'string' },
|
|
172
|
+
keywords: {
|
|
173
|
+
type: 'array',
|
|
174
|
+
items: { type: 'string' },
|
|
175
|
+
},
|
|
176
|
+
customFields: {
|
|
177
|
+
type: 'object',
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
required: ['documentId', 'metadata'],
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
name: 'search_content',
|
|
187
|
+
description: 'Search for content across all documents',
|
|
188
|
+
inputSchema: {
|
|
189
|
+
type: 'object',
|
|
190
|
+
properties: {
|
|
191
|
+
query: {
|
|
192
|
+
type: 'string',
|
|
193
|
+
description: 'Search query',
|
|
194
|
+
},
|
|
195
|
+
caseSensitive: {
|
|
196
|
+
type: 'boolean',
|
|
197
|
+
default: false,
|
|
198
|
+
},
|
|
199
|
+
regex: {
|
|
200
|
+
type: 'boolean',
|
|
201
|
+
default: false,
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
required: ['query'],
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
name: 'compile_documents',
|
|
209
|
+
description: 'Compile multiple documents into a single text',
|
|
210
|
+
inputSchema: {
|
|
211
|
+
type: 'object',
|
|
212
|
+
properties: {
|
|
213
|
+
documentIds: {
|
|
214
|
+
type: 'array',
|
|
215
|
+
items: { type: 'string' },
|
|
216
|
+
description: 'Array of document UUIDs to compile',
|
|
217
|
+
},
|
|
218
|
+
separator: {
|
|
219
|
+
type: 'string',
|
|
220
|
+
default: '\n\n',
|
|
221
|
+
description: 'Separator between documents',
|
|
222
|
+
},
|
|
223
|
+
outputFormat: {
|
|
224
|
+
type: 'string',
|
|
225
|
+
enum: ['text', 'markdown', 'html', 'latex', 'json'],
|
|
226
|
+
default: 'text',
|
|
227
|
+
description: 'Output format for compilation',
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
required: ['documentIds'],
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
name: 'read_document_formatted',
|
|
235
|
+
description: 'Read document with preserved RTF formatting',
|
|
236
|
+
inputSchema: {
|
|
237
|
+
type: 'object',
|
|
238
|
+
properties: {
|
|
239
|
+
documentId: {
|
|
240
|
+
type: 'string',
|
|
241
|
+
description: 'UUID of the document to read',
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
required: ['documentId'],
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
name: 'get_document_annotations',
|
|
249
|
+
description: 'Get Scrivener annotations and comments from a document',
|
|
250
|
+
inputSchema: {
|
|
251
|
+
type: 'object',
|
|
252
|
+
properties: {
|
|
253
|
+
documentId: {
|
|
254
|
+
type: 'string',
|
|
255
|
+
description: 'UUID of the document',
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
required: ['documentId'],
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
name: 'get_word_count',
|
|
263
|
+
description: 'Get word and character count',
|
|
264
|
+
inputSchema: {
|
|
265
|
+
type: 'object',
|
|
266
|
+
properties: {
|
|
267
|
+
documentId: {
|
|
268
|
+
type: 'string',
|
|
269
|
+
description: 'UUID of specific document (omit for entire project)',
|
|
270
|
+
},
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
name: 'analyze_document',
|
|
276
|
+
description: 'Analyze document for writing quality metrics',
|
|
277
|
+
inputSchema: {
|
|
278
|
+
type: 'object',
|
|
279
|
+
properties: {
|
|
280
|
+
documentId: {
|
|
281
|
+
type: 'string',
|
|
282
|
+
description: 'UUID of the document to analyze',
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
required: ['documentId'],
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
name: 'critique_document',
|
|
290
|
+
description: 'Provide constructive critique of document',
|
|
291
|
+
inputSchema: {
|
|
292
|
+
type: 'object',
|
|
293
|
+
properties: {
|
|
294
|
+
documentId: {
|
|
295
|
+
type: 'string',
|
|
296
|
+
description: 'UUID of the document to critique',
|
|
297
|
+
},
|
|
298
|
+
focusAreas: {
|
|
299
|
+
type: 'array',
|
|
300
|
+
items: {
|
|
301
|
+
type: 'string',
|
|
302
|
+
enum: [
|
|
303
|
+
'structure',
|
|
304
|
+
'flow',
|
|
305
|
+
'clarity',
|
|
306
|
+
'character',
|
|
307
|
+
'dialogue',
|
|
308
|
+
'pacing',
|
|
309
|
+
'style',
|
|
310
|
+
],
|
|
311
|
+
},
|
|
312
|
+
description: 'Areas to focus critique on',
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
required: ['documentId'],
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
name: 'get_project_metadata',
|
|
320
|
+
description: 'Get project-level metadata',
|
|
321
|
+
inputSchema: {
|
|
322
|
+
type: 'object',
|
|
323
|
+
properties: {},
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
],
|
|
327
|
+
};
|
|
328
|
+
});
|
|
329
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
330
|
+
const { name, arguments: args } = request.params;
|
|
331
|
+
try {
|
|
332
|
+
switch (name) {
|
|
333
|
+
case 'open_project': {
|
|
334
|
+
const { path: projPath } = args;
|
|
335
|
+
// Verify the path exists and is a .scriv folder
|
|
336
|
+
const stats = await fs.stat(projPath);
|
|
337
|
+
if (!stats.isDirectory() || !projPath.endsWith('.scriv')) {
|
|
338
|
+
throw new Error('Invalid Scrivener project path');
|
|
339
|
+
}
|
|
340
|
+
currentProject = new ScrivenerProject(projPath);
|
|
341
|
+
await currentProject.loadProject();
|
|
342
|
+
return {
|
|
343
|
+
content: [
|
|
344
|
+
{
|
|
345
|
+
type: 'text',
|
|
346
|
+
text: `Successfully opened project: ${path.basename(projPath)}`,
|
|
347
|
+
},
|
|
348
|
+
],
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
case 'get_structure': {
|
|
352
|
+
if (!currentProject) {
|
|
353
|
+
throw new Error('No project is currently open');
|
|
354
|
+
}
|
|
355
|
+
const structure = await currentProject.getProjectStructure();
|
|
356
|
+
return {
|
|
357
|
+
content: [
|
|
358
|
+
{
|
|
359
|
+
type: 'text',
|
|
360
|
+
text: JSON.stringify(structure, null, 2),
|
|
361
|
+
},
|
|
362
|
+
],
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
case 'read_document': {
|
|
366
|
+
if (!currentProject) {
|
|
367
|
+
throw new Error('No project is currently open');
|
|
368
|
+
}
|
|
369
|
+
const { documentId } = args;
|
|
370
|
+
const content = await currentProject.readDocument(documentId);
|
|
371
|
+
return {
|
|
372
|
+
content: [
|
|
373
|
+
{
|
|
374
|
+
type: 'text',
|
|
375
|
+
text: content,
|
|
376
|
+
},
|
|
377
|
+
],
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
case 'write_document': {
|
|
381
|
+
if (!currentProject) {
|
|
382
|
+
throw new Error('No project is currently open');
|
|
383
|
+
}
|
|
384
|
+
const { documentId, content } = args;
|
|
385
|
+
await currentProject.writeDocument(documentId, content);
|
|
386
|
+
return {
|
|
387
|
+
content: [
|
|
388
|
+
{
|
|
389
|
+
type: 'text',
|
|
390
|
+
text: `Document ${documentId} updated successfully`,
|
|
391
|
+
},
|
|
392
|
+
],
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
case 'create_document': {
|
|
396
|
+
if (!currentProject) {
|
|
397
|
+
throw new Error('No project is currently open');
|
|
398
|
+
}
|
|
399
|
+
const { parentId, title, type = 'Text', } = args;
|
|
400
|
+
const uuid = await currentProject.createDocument(parentId || null, title, type);
|
|
401
|
+
return {
|
|
402
|
+
content: [
|
|
403
|
+
{
|
|
404
|
+
type: 'text',
|
|
405
|
+
text: `Created ${type.toLowerCase()} "${title}" with ID: ${uuid}`,
|
|
406
|
+
},
|
|
407
|
+
],
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
case 'delete_document': {
|
|
411
|
+
if (!currentProject) {
|
|
412
|
+
throw new Error('No project is currently open');
|
|
413
|
+
}
|
|
414
|
+
const { documentId } = args;
|
|
415
|
+
await currentProject.deleteDocument(documentId);
|
|
416
|
+
return {
|
|
417
|
+
content: [
|
|
418
|
+
{
|
|
419
|
+
type: 'text',
|
|
420
|
+
text: `Document ${documentId} deleted successfully`,
|
|
421
|
+
},
|
|
422
|
+
],
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
case 'rename_document': {
|
|
426
|
+
if (!currentProject) {
|
|
427
|
+
throw new Error('No project is currently open');
|
|
428
|
+
}
|
|
429
|
+
const { documentId, newTitle } = args;
|
|
430
|
+
await currentProject.renameDocument(documentId, newTitle);
|
|
431
|
+
return {
|
|
432
|
+
content: [
|
|
433
|
+
{
|
|
434
|
+
type: 'text',
|
|
435
|
+
text: `Document ${documentId} renamed to "${newTitle}"`,
|
|
436
|
+
},
|
|
437
|
+
],
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
case 'refresh_project': {
|
|
441
|
+
if (!currentProject) {
|
|
442
|
+
throw new Error('No project is currently open');
|
|
443
|
+
}
|
|
444
|
+
await currentProject.refreshProject();
|
|
445
|
+
return {
|
|
446
|
+
content: [
|
|
447
|
+
{
|
|
448
|
+
type: 'text',
|
|
449
|
+
text: 'Project refreshed from disk',
|
|
450
|
+
},
|
|
451
|
+
],
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
case 'move_document': {
|
|
455
|
+
if (!currentProject) {
|
|
456
|
+
throw new Error('No project is currently open');
|
|
457
|
+
}
|
|
458
|
+
const { documentId, newParentId } = args;
|
|
459
|
+
await currentProject.moveDocument(documentId, newParentId || null);
|
|
460
|
+
return {
|
|
461
|
+
content: [
|
|
462
|
+
{
|
|
463
|
+
type: 'text',
|
|
464
|
+
text: `Document ${documentId} moved successfully`,
|
|
465
|
+
},
|
|
466
|
+
],
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
case 'update_metadata': {
|
|
470
|
+
if (!currentProject) {
|
|
471
|
+
throw new Error('No project is currently open');
|
|
472
|
+
}
|
|
473
|
+
const { documentId, metadata } = args;
|
|
474
|
+
await currentProject.updateMetadata(documentId, metadata);
|
|
475
|
+
return {
|
|
476
|
+
content: [
|
|
477
|
+
{
|
|
478
|
+
type: 'text',
|
|
479
|
+
text: `Metadata updated for document ${documentId}`,
|
|
480
|
+
},
|
|
481
|
+
],
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
case 'search_content': {
|
|
485
|
+
if (!currentProject) {
|
|
486
|
+
throw new Error('No project is currently open');
|
|
487
|
+
}
|
|
488
|
+
const { query, caseSensitive = false, regex = false, } = args;
|
|
489
|
+
const results = await currentProject.searchContent(query, { caseSensitive, regex });
|
|
490
|
+
return {
|
|
491
|
+
content: [
|
|
492
|
+
{
|
|
493
|
+
type: 'text',
|
|
494
|
+
text: JSON.stringify(results, null, 2),
|
|
495
|
+
},
|
|
496
|
+
],
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
case 'compile_documents': {
|
|
500
|
+
if (!currentProject) {
|
|
501
|
+
throw new Error('No project is currently open');
|
|
502
|
+
}
|
|
503
|
+
const { documentIds, separator = '\n\n', outputFormat = 'text', } = args;
|
|
504
|
+
const compiled = await currentProject.compileDocuments(documentIds, separator, outputFormat);
|
|
505
|
+
return {
|
|
506
|
+
content: [
|
|
507
|
+
{
|
|
508
|
+
type: 'text',
|
|
509
|
+
text: typeof compiled === 'string'
|
|
510
|
+
? compiled
|
|
511
|
+
: JSON.stringify(compiled, null, 2),
|
|
512
|
+
},
|
|
513
|
+
],
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
case 'read_document_formatted': {
|
|
517
|
+
if (!currentProject) {
|
|
518
|
+
throw new Error('No project is currently open');
|
|
519
|
+
}
|
|
520
|
+
const { documentId } = args;
|
|
521
|
+
const rtfContent = await currentProject.readDocumentFormatted(documentId);
|
|
522
|
+
return {
|
|
523
|
+
content: [
|
|
524
|
+
{
|
|
525
|
+
type: 'text',
|
|
526
|
+
text: JSON.stringify(rtfContent, null, 2),
|
|
527
|
+
},
|
|
528
|
+
],
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
case 'get_document_annotations': {
|
|
532
|
+
if (!currentProject) {
|
|
533
|
+
throw new Error('No project is currently open');
|
|
534
|
+
}
|
|
535
|
+
const { documentId } = args;
|
|
536
|
+
const annotations = await currentProject.getDocumentAnnotations(documentId);
|
|
537
|
+
const annotationObject = {};
|
|
538
|
+
annotations.forEach((value, key) => {
|
|
539
|
+
annotationObject[key] = value;
|
|
540
|
+
});
|
|
541
|
+
return {
|
|
542
|
+
content: [
|
|
543
|
+
{
|
|
544
|
+
type: 'text',
|
|
545
|
+
text: JSON.stringify(annotationObject, null, 2),
|
|
546
|
+
},
|
|
547
|
+
],
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
case 'get_word_count': {
|
|
551
|
+
if (!currentProject) {
|
|
552
|
+
throw new Error('No project is currently open');
|
|
553
|
+
}
|
|
554
|
+
const { documentId } = args;
|
|
555
|
+
const count = await currentProject.getWordCount(documentId);
|
|
556
|
+
return {
|
|
557
|
+
content: [
|
|
558
|
+
{
|
|
559
|
+
type: 'text',
|
|
560
|
+
text: `Words: ${count.words}\nCharacters: ${count.characters}`,
|
|
561
|
+
},
|
|
562
|
+
],
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
case 'analyze_document': {
|
|
566
|
+
if (!currentProject) {
|
|
567
|
+
throw new Error('No project is currently open');
|
|
568
|
+
}
|
|
569
|
+
const { documentId } = args;
|
|
570
|
+
const content = await currentProject.readDocument(documentId);
|
|
571
|
+
// Basic analysis
|
|
572
|
+
const analysis = analyzeWriting(content);
|
|
573
|
+
return {
|
|
574
|
+
content: [
|
|
575
|
+
{
|
|
576
|
+
type: 'text',
|
|
577
|
+
text: JSON.stringify(analysis, null, 2),
|
|
578
|
+
},
|
|
579
|
+
],
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
case 'critique_document': {
|
|
583
|
+
if (!currentProject) {
|
|
584
|
+
throw new Error('No project is currently open');
|
|
585
|
+
}
|
|
586
|
+
const { documentId, focusAreas = [] } = args;
|
|
587
|
+
const content = await currentProject.readDocument(documentId);
|
|
588
|
+
const critique = generateCritique(content, focusAreas);
|
|
589
|
+
return {
|
|
590
|
+
content: [
|
|
591
|
+
{
|
|
592
|
+
type: 'text',
|
|
593
|
+
text: critique,
|
|
594
|
+
},
|
|
595
|
+
],
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
case 'get_project_metadata': {
|
|
599
|
+
if (!currentProject) {
|
|
600
|
+
throw new Error('No project is currently open');
|
|
601
|
+
}
|
|
602
|
+
const metadata = await currentProject.getProjectMetadata();
|
|
603
|
+
return {
|
|
604
|
+
content: [
|
|
605
|
+
{
|
|
606
|
+
type: 'text',
|
|
607
|
+
text: JSON.stringify(metadata, null, 2),
|
|
608
|
+
},
|
|
609
|
+
],
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
default:
|
|
613
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
catch (error) {
|
|
617
|
+
throw new Error(`Tool execution failed: ${error}`);
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
function analyzeWriting(content) {
|
|
621
|
+
const sentences = content.split(/[.!?]+/).filter((s) => s.trim().length > 0);
|
|
622
|
+
const words = content.split(/\s+/).filter((w) => w.length > 0);
|
|
623
|
+
// Calculate readability metrics
|
|
624
|
+
const avgWordsPerSentence = words.length / Math.max(sentences.length, 1);
|
|
625
|
+
// Find long sentences
|
|
626
|
+
const longSentences = sentences.filter((s) => s.split(/\s+/).length > 30);
|
|
627
|
+
// Check for passive voice indicators
|
|
628
|
+
const passiveIndicators = content.match(/\b(was|were|been|being|is|are|am)\s+\w+ed\b/gi) || [];
|
|
629
|
+
// Count adjectives and adverbs
|
|
630
|
+
const adjectives = (content.match(/\b\w+(ful|less|ous|ive|able|ible|al|ic)\b/gi) || []).length;
|
|
631
|
+
const adverbs = (content.match(/\b\w+ly\b/gi) || []).length;
|
|
632
|
+
// Calculate reading level (simple approximation)
|
|
633
|
+
const fleschScore = calculateFleschReadingEase(content);
|
|
634
|
+
let readingLevel;
|
|
635
|
+
if (fleschScore >= 90)
|
|
636
|
+
readingLevel = 'Very Easy';
|
|
637
|
+
else if (fleschScore >= 70)
|
|
638
|
+
readingLevel = 'Easy';
|
|
639
|
+
else if (fleschScore >= 50)
|
|
640
|
+
readingLevel = 'Average';
|
|
641
|
+
else if (fleschScore >= 30)
|
|
642
|
+
readingLevel = 'Difficult';
|
|
643
|
+
else
|
|
644
|
+
readingLevel = 'Very Difficult';
|
|
645
|
+
// Calculate complexity based on sentence length
|
|
646
|
+
const complexity = avgWordsPerSentence > 25 ? 3 : avgWordsPerSentence > 15 ? 2 : 1;
|
|
647
|
+
return {
|
|
648
|
+
wordCount: words.length,
|
|
649
|
+
sentenceCount: sentences.length,
|
|
650
|
+
averageWordsPerSentence: Math.round(avgWordsPerSentence * 10) / 10,
|
|
651
|
+
longSentences: longSentences.map((s) => s.substring(0, 100) + (s.length > 100 ? '...' : '')),
|
|
652
|
+
readingLevel,
|
|
653
|
+
complexity,
|
|
654
|
+
statistics: {
|
|
655
|
+
adjectives,
|
|
656
|
+
adverbs,
|
|
657
|
+
passiveVoice: passiveIndicators.length,
|
|
658
|
+
},
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
function calculateFleschReadingEase(text) {
|
|
662
|
+
const sentences = text.split(/[.!?]+/).filter((s) => s.trim().length > 0);
|
|
663
|
+
const words = text.split(/\s+/).filter((w) => w.length > 0);
|
|
664
|
+
const syllables = words.reduce((sum, word) => sum + countSyllables(word), 0);
|
|
665
|
+
if (sentences.length === 0 || words.length === 0)
|
|
666
|
+
return 0;
|
|
667
|
+
const score = 206.835 - 1.015 * (words.length / sentences.length) - 84.6 * (syllables / words.length);
|
|
668
|
+
return Math.max(0, Math.min(100, Math.round(score)));
|
|
669
|
+
}
|
|
670
|
+
function countSyllables(word) {
|
|
671
|
+
word = word.toLowerCase().replace(/[^a-z]/g, '');
|
|
672
|
+
if (word.length <= 3)
|
|
673
|
+
return 1;
|
|
674
|
+
const vowels = 'aeiouy';
|
|
675
|
+
let count = 0;
|
|
676
|
+
let previousWasVowel = false;
|
|
677
|
+
for (let i = 0; i < word.length; i++) {
|
|
678
|
+
const isVowel = vowels.includes(word[i]);
|
|
679
|
+
if (isVowel && !previousWasVowel) {
|
|
680
|
+
count++;
|
|
681
|
+
}
|
|
682
|
+
previousWasVowel = isVowel;
|
|
683
|
+
}
|
|
684
|
+
if (word.endsWith('e'))
|
|
685
|
+
count--;
|
|
686
|
+
if (word.endsWith('le') && word.length > 2 && !vowels.includes(word[word.length - 3]))
|
|
687
|
+
count++;
|
|
688
|
+
return Math.max(1, count);
|
|
689
|
+
}
|
|
690
|
+
function generateCritique(content, focusAreas) {
|
|
691
|
+
const areas = focusAreas.length > 0 ? focusAreas : ['structure', 'flow', 'clarity', 'style'];
|
|
692
|
+
const critiques = [];
|
|
693
|
+
const sentences = content.split(/[.!?]+/).filter((s) => s.trim().length > 0);
|
|
694
|
+
const paragraphs = content.split(/\n\n+/).filter((p) => p.trim().length > 0);
|
|
695
|
+
if (areas.includes('structure')) {
|
|
696
|
+
critiques.push('**Structure:**');
|
|
697
|
+
if (paragraphs.length < 3) {
|
|
698
|
+
critiques.push('- Consider breaking the text into more paragraphs for better readability');
|
|
699
|
+
}
|
|
700
|
+
if (paragraphs.some((p) => p.split(/[.!?]+/).length > 7)) {
|
|
701
|
+
critiques.push('- Some paragraphs are quite long. Consider splitting them for better pacing');
|
|
702
|
+
}
|
|
703
|
+
critiques.push('');
|
|
704
|
+
}
|
|
705
|
+
if (areas.includes('flow')) {
|
|
706
|
+
critiques.push('**Flow:**');
|
|
707
|
+
const transitionWords = [
|
|
708
|
+
'however',
|
|
709
|
+
'therefore',
|
|
710
|
+
'moreover',
|
|
711
|
+
'furthermore',
|
|
712
|
+
'consequently',
|
|
713
|
+
'nevertheless',
|
|
714
|
+
];
|
|
715
|
+
const hasTransitions = transitionWords.some((word) => content.toLowerCase().includes(word));
|
|
716
|
+
if (!hasTransitions) {
|
|
717
|
+
critiques.push('- Consider adding transition words to improve flow between ideas');
|
|
718
|
+
}
|
|
719
|
+
if (sentences.filter((s) => s.startsWith('I ') || s.startsWith('The ')).length >
|
|
720
|
+
sentences.length * 0.3) {
|
|
721
|
+
critiques.push('- Vary sentence beginnings to improve flow and maintain reader interest');
|
|
722
|
+
}
|
|
723
|
+
critiques.push('');
|
|
724
|
+
}
|
|
725
|
+
if (areas.includes('clarity')) {
|
|
726
|
+
critiques.push('**Clarity:**');
|
|
727
|
+
const avgWordsPerSentence = content.split(/\s+/).length / Math.max(sentences.length, 1);
|
|
728
|
+
if (avgWordsPerSentence > 25) {
|
|
729
|
+
critiques.push('- Sentences are quite long on average. Consider breaking them up for clarity');
|
|
730
|
+
}
|
|
731
|
+
const passiveVoice = (content.match(/\b(was|were|been|being)\s+\w+ed\b/gi) || []).length;
|
|
732
|
+
if (passiveVoice > sentences.length * 0.2) {
|
|
733
|
+
critiques.push('- High use of passive voice detected. Consider using active voice for more direct communication');
|
|
734
|
+
}
|
|
735
|
+
critiques.push('');
|
|
736
|
+
}
|
|
737
|
+
if (areas.includes('style')) {
|
|
738
|
+
critiques.push('**Style:**');
|
|
739
|
+
const adverbs = (content.match(/\w+ly\b/gi) || []).length;
|
|
740
|
+
if (adverbs > sentences.length * 0.3) {
|
|
741
|
+
critiques.push('- Consider reducing adverb usage. Strong verbs often work better than verb + adverb combinations');
|
|
742
|
+
}
|
|
743
|
+
const saidBookisms = content.match(/\b(exclaimed|declared|announced|stated|remarked|uttered)\b/gi) || [];
|
|
744
|
+
if (saidBookisms.length > 2) {
|
|
745
|
+
critiques.push('- Avoid overuse of "said bookisms". "Said" and "asked" are usually sufficient');
|
|
746
|
+
}
|
|
747
|
+
critiques.push('');
|
|
748
|
+
}
|
|
749
|
+
if (areas.includes('dialogue') && content.includes('"')) {
|
|
750
|
+
critiques.push('**Dialogue:**');
|
|
751
|
+
const dialogueLines = content.match(/"[^"]+"/g) || [];
|
|
752
|
+
if (dialogueLines.some((d) => d.length > 150)) {
|
|
753
|
+
critiques.push('- Some dialogue passages are quite long. Consider breaking them up with action or description');
|
|
754
|
+
}
|
|
755
|
+
if (!content.includes('said') && dialogueLines.length > 3) {
|
|
756
|
+
critiques.push('- Consider adding dialogue tags for clarity about who is speaking');
|
|
757
|
+
}
|
|
758
|
+
critiques.push('');
|
|
759
|
+
}
|
|
760
|
+
if (areas.includes('pacing')) {
|
|
761
|
+
critiques.push('**Pacing:**');
|
|
762
|
+
const shortSentences = sentences.filter((s) => s.split(/\s+/).length < 8).length;
|
|
763
|
+
const longSentences = sentences.filter((s) => s.split(/\s+/).length > 25).length;
|
|
764
|
+
if (shortSentences > sentences.length * 0.7) {
|
|
765
|
+
critiques.push('- Many short sentences detected. Consider combining some for better flow');
|
|
766
|
+
}
|
|
767
|
+
else if (longSentences > sentences.length * 0.5) {
|
|
768
|
+
critiques.push('- Many long sentences detected. Mix in shorter sentences for better pacing');
|
|
769
|
+
}
|
|
770
|
+
critiques.push('');
|
|
771
|
+
}
|
|
772
|
+
if (areas.includes('character') &&
|
|
773
|
+
(content.includes('he ') || content.includes('she ') || content.includes('they '))) {
|
|
774
|
+
critiques.push('**Character:**');
|
|
775
|
+
if (!content.match(/\b(felt|thought|wondered|realized|understood)\b/i)) {
|
|
776
|
+
critiques.push('- Consider adding more internal character thoughts and feelings');
|
|
777
|
+
}
|
|
778
|
+
const dialogueRatio = (content.match(/"[^"]+"/g) || []).join('').length / content.length;
|
|
779
|
+
if (dialogueRatio < 0.1) {
|
|
780
|
+
critiques.push('- Limited dialogue detected. Consider adding more character interaction');
|
|
781
|
+
}
|
|
782
|
+
critiques.push('');
|
|
783
|
+
}
|
|
784
|
+
critiques.push('**Overall:** Remember that these are suggestions. The best writing often breaks rules purposefully. Consider your intended style and audience when applying these critiques.');
|
|
785
|
+
return critiques.join('\n');
|
|
786
|
+
}
|
|
787
|
+
async function main() {
|
|
788
|
+
const transport = new StdioServerTransport();
|
|
789
|
+
await server.connect(transport);
|
|
790
|
+
console.error('Scrivener MCP server running on stdio');
|
|
791
|
+
}
|
|
792
|
+
main().catch((error) => {
|
|
793
|
+
console.error('Server error:', error);
|
|
794
|
+
process.exit(1);
|
|
795
|
+
});
|
|
796
|
+
//# sourceMappingURL=index.js.map
|