-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathgenerate-docs.ts
More file actions
546 lines (459 loc) · 15.9 KB
/
generate-docs.ts
File metadata and controls
546 lines (459 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import fs from 'node:fs';
import {Client} from '@modelcontextprotocol/sdk/client/index.js';
import {StdioClientTransport} from '@modelcontextprotocol/sdk/client/stdio.js';
import type {Tool} from '@modelcontextprotocol/sdk/types.js';
import {get_encoding} from 'tiktoken';
import {cliOptions} from '../build/src/bin/chrome-devtools-mcp-cli-options.js';
import type {ParsedArguments} from '../build/src/bin/chrome-devtools-mcp-cli-options.js';
import {ToolCategory, labels} from '../build/src/tools/categories.js';
import {createTools} from '../build/src/tools/tools.js';
const OUTPUT_PATH = './docs/tool-reference.md';
const SLIM_OUTPUT_PATH = './docs/slim-tool-reference.md';
const README_PATH = './README.md';
async function measureServer(args: string[]) {
// 1. Connect to your actual MCP server
const transport = new StdioClientTransport({
command: 'node',
args: ['./build/src/bin/chrome-devtools-mcp.js', ...args], // Point to your built MCP server
});
const client = new Client(
{name: 'measurer', version: '1.0.0'},
{capabilities: {}},
);
await client.connect(transport);
// 2. Fetch all tools
const toolsList = await client.listTools();
// 3. Serialize exactly how an LLM would see it (JSON)
const jsonString = JSON.stringify(toolsList.tools, null, 2);
// 4. Count tokens (using cl100k_base which is standard for GPT-4/Claude-3.5 approximation)
const enc = get_encoding('cl100k_base');
const tokenCount = enc.encode(jsonString).length;
console.log(`--- Measurement Results ---`);
console.log(`Total Tools: ${toolsList.tools.length}`);
console.log(`JSON Character Count: ${jsonString.length}`);
console.log(`Estimated Token Count: ~${tokenCount}`);
// Clean up
enc.free();
await client.close();
return {
tokenCount,
};
}
// Extend the MCP Tool type to include our annotations
interface ToolWithAnnotations extends Tool {
annotations?: {
title?: string;
category?: typeof ToolCategory;
conditions?: string[];
};
}
interface ZodCheck {
kind: string;
}
interface ZodDef {
typeName: string;
checks?: ZodCheck[];
values?: string[];
type?: ZodSchema;
innerType?: ZodSchema;
schema?: ZodSchema;
defaultValue?: () => unknown;
}
interface ZodSchema {
_def: ZodDef;
description?: string;
}
interface TypeInfo {
type: string;
enum?: string[];
items?: TypeInfo;
description?: string;
default?: unknown;
}
function escapeHtmlTags(text: string): string {
return text
.replace(/&(?![a-zA-Z]+;)/g, '&')
.replace(/<([a-zA-Z][^>]*)>/g, '<$1>');
}
function addCrossLinks(text: string, tools: ToolWithAnnotations[]): string {
let result = text;
// Create a set of all tool names for efficient lookup
const toolNames = new Set(tools.map(tool => tool.name));
// Sort tool names by length (descending) to match longer names first
const sortedToolNames = Array.from(toolNames).sort(
(a, b) => b.length - a.length,
);
for (const toolName of sortedToolNames) {
// Create regex to match tool name (case insensitive, word boundaries)
const regex = new RegExp(`\\b${toolName}\\b`, 'gi');
result = result.replace(regex, match => {
// Only create link if the match isn't already inside a link
if (result.indexOf(`[${match}]`) !== -1) {
return match; // Already linked
}
const anchorLink = toolName.toLowerCase();
return `[\`${match}\`](#${anchorLink})`;
});
}
return result;
}
function generateToolsTOC(
categories: Record<string, ToolWithAnnotations[]>,
sortedCategories: string[],
): string {
let toc = '';
for (const category of sortedCategories) {
const categoryTools = categories[category];
const categoryName = labels[category];
toc += `- **${categoryName}** (${categoryTools.length} tools)\n`;
// Sort tools within category for TOC
categoryTools.sort((a: Tool, b: Tool) => a.name.localeCompare(b.name));
for (const tool of categoryTools) {
const anchorLink = tool.name.toLowerCase();
toc += ` - [\`${tool.name}\`](docs/tool-reference.md#${anchorLink})\n`;
}
}
return toc;
}
function updateReadmeWithToolsTOC(toolsTOC: string): void {
const readmeContent = fs.readFileSync(README_PATH, 'utf8');
const beginMarker = '<!-- BEGIN AUTO GENERATED TOOLS -->';
const endMarker = '<!-- END AUTO GENERATED TOOLS -->';
const beginIndex = readmeContent.indexOf(beginMarker);
const endIndex = readmeContent.indexOf(endMarker);
if (beginIndex === -1 || endIndex === -1) {
console.warn('Could not find auto-generated tools markers in README.md');
return;
}
const before = readmeContent.substring(0, beginIndex + beginMarker.length);
const after = readmeContent.substring(endIndex);
const updatedContent = before + '\n\n' + toolsTOC + '\n' + after;
fs.writeFileSync(README_PATH, updatedContent);
console.log('Updated README.md with tools table of contents');
}
function generateConfigOptionsMarkdown(): string {
let markdown = '';
for (const [optionName, optionConfig] of Object.entries(cliOptions)) {
// Skip hidden options
if (optionConfig.hidden) {
continue;
}
const aliasText = optionConfig.alias ? `, \`-${optionConfig.alias}\`` : '';
const description = optionConfig.description || optionConfig.describe || '';
// Convert camelCase to dash-case
const dashCaseName = optionName
.replace(/([a-z])([A-Z])/g, '$1-$2')
.toLowerCase();
const nameDisplay =
dashCaseName !== optionName
? `\`--${optionName}\`/ \`--${dashCaseName}\``
: `\`--${optionName}\``;
// Start with option name and description
markdown += `- **${nameDisplay}${aliasText}**\n`;
markdown += ` ${description}\n`;
// Add type information
markdown += ` - **Type:** ${optionConfig.type}\n`;
// Add choices if available
if (optionConfig.choices) {
markdown += ` - **Choices:** ${optionConfig.choices.map(c => `\`${c}\``).join(', ')}\n`;
}
// Add default if available
if (optionConfig.default !== undefined) {
markdown += ` - **Default:** \`${optionConfig.default}\`\n`;
}
markdown += '\n';
}
return markdown.trim();
}
function updateReadmeWithOptionsMarkdown(optionsMarkdown: string): void {
const readmeContent = fs.readFileSync(README_PATH, 'utf8');
const beginMarker = '<!-- BEGIN AUTO GENERATED OPTIONS -->';
const endMarker = '<!-- END AUTO GENERATED OPTIONS -->';
const beginIndex = readmeContent.indexOf(beginMarker);
const endIndex = readmeContent.indexOf(endMarker);
if (beginIndex === -1 || endIndex === -1) {
console.warn('Could not find auto-generated options markers in README.md');
return;
}
const before = readmeContent.substring(0, beginIndex + beginMarker.length);
const after = readmeContent.substring(endIndex);
const updatedContent = before + '\n\n' + optionsMarkdown + '\n\n' + after;
fs.writeFileSync(README_PATH, updatedContent);
console.log('Updated README.md with options markdown');
}
// Helper to convert Zod schema to JSON schema-like object for docs
function getZodTypeInfo(schema: ZodSchema): TypeInfo {
let description = schema.description;
let def = schema._def;
let defaultValue: unknown;
// Unwrap optional/default/effects
while (
def.typeName === 'ZodOptional' ||
def.typeName === 'ZodDefault' ||
def.typeName === 'ZodEffects'
) {
if (def.typeName === 'ZodDefault' && def.defaultValue) {
defaultValue = def.defaultValue();
}
const next = def.innerType || def.schema;
if (!next) {
break;
}
schema = next;
def = schema._def;
if (!description && schema.description) {
description = schema.description;
}
}
const result: TypeInfo = {type: 'unknown'};
if (description) {
result.description = description;
}
if (defaultValue !== undefined) {
result.default = defaultValue;
}
switch (def.typeName) {
case 'ZodString':
result.type = 'string';
break;
case 'ZodNumber':
result.type = def.checks?.some((c: ZodCheck) => c.kind === 'int')
? 'integer'
: 'number';
break;
case 'ZodBoolean':
result.type = 'boolean';
break;
case 'ZodEnum':
result.type = 'string';
result.enum = def.values;
break;
case 'ZodArray':
result.type = 'array';
if (def.type) {
result.items = getZodTypeInfo(def.type);
}
break;
default:
result.type = 'unknown';
}
return result;
}
function isRequired(schema: ZodSchema): boolean {
let def = schema._def;
while (def.typeName === 'ZodEffects') {
if (!def.schema) {
break;
}
schema = def.schema;
def = schema._def;
}
return def.typeName !== 'ZodOptional' && def.typeName !== 'ZodDefault';
}
async function generateReference(
title: string,
outputPath: string,
toolsWithAnnotations: ToolWithAnnotations[],
categories: Record<string, ToolWithAnnotations[]>,
sortedCategories: string[],
serverArgs: string[],
) {
console.log(`Found ${toolsWithAnnotations.length} tools`);
// Generate markdown documentation
let markdown = `<!-- AUTO GENERATED DO NOT EDIT - run 'npm run gen' to update-->
# ${title} (~${(await measureServer(serverArgs)).tokenCount} cl100k_base tokens)
`;
// Generate table of contents
for (const category of sortedCategories) {
const categoryTools = categories[category];
const categoryName = labels[category];
const anchorName = categoryName.toLowerCase().replace(/\s+/g, '-');
markdown += `- **[${categoryName}](#${anchorName})** (${categoryTools.length} tools)\n`;
// Sort tools within category for TOC
categoryTools.sort((a: Tool, b: Tool) => a.name.localeCompare(b.name));
for (const tool of categoryTools) {
// Generate proper markdown anchor link: backticks are removed, keep underscores, lowercase
const anchorLink = tool.name.toLowerCase();
markdown += ` - [\`${tool.name}\`](#${anchorLink})\n`;
}
}
markdown += '\n';
for (const category of sortedCategories) {
const categoryTools = categories[category];
const categoryName = labels[category];
markdown += `## ${categoryName}\n\n`;
// Sort tools within category
categoryTools.sort((a: Tool, b: Tool) => a.name.localeCompare(b.name));
for (const tool of categoryTools) {
markdown += `### \`${tool.name}\`\n\n`;
if (tool.description) {
// Escape HTML tags but preserve JS function syntax
let escapedDescription = escapeHtmlTags(tool.description);
// Add cross-links to mentioned tools
escapedDescription = addCrossLinks(
escapedDescription,
toolsWithAnnotations,
);
markdown += `**Description:** ${escapedDescription}\n\n`;
}
// Handle input schema
if (
tool.inputSchema &&
tool.inputSchema.properties &&
Object.keys(tool.inputSchema.properties).length > 0
) {
const properties = tool.inputSchema.properties;
const required = tool.inputSchema.required || [];
markdown += '**Parameters:**\n\n';
const propertyNames = Object.keys(properties).sort((a, b) => {
const aRequired = required.includes(a);
const bRequired = required.includes(b);
if (aRequired && !bRequired) {
return -1;
}
if (!aRequired && bRequired) {
return 1;
}
return a.localeCompare(b);
});
for (const propName of propertyNames) {
const prop = properties[propName] as TypeInfo;
const isRequired = required.includes(propName);
const requiredText = isRequired ? ' **(required)**' : ' _(optional)_';
let typeInfo = prop.type || 'unknown';
if (prop.enum) {
typeInfo = `enum: ${prop.enum.map((v: string) => `"${v}"`).join(', ')}`;
}
markdown += `- **${propName}** (${typeInfo})${requiredText}`;
if (prop.description) {
let escapedParamDesc = escapeHtmlTags(prop.description);
// Add cross-links to mentioned tools
escapedParamDesc = addCrossLinks(
escapedParamDesc,
toolsWithAnnotations,
);
markdown += `: ${escapedParamDesc}`;
}
markdown += '\n';
}
markdown += '\n';
} else {
markdown += '**Parameters:** None\n\n';
}
markdown += '---\n\n';
}
}
// Write the documentation to file
fs.writeFileSync(outputPath, markdown.trim() + '\n');
console.log(
`Generated documentation for ${toolsWithAnnotations.length} tools in ${outputPath}`,
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getToolsAndCategories(tools: any) {
// Convert ToolDefinitions to ToolWithAnnotations
const toolsWithAnnotations: ToolWithAnnotations[] = tools
.filter(tool => {
if (!tool.annotations.conditions) {
return true;
}
// Only include unconditional tools.
return tool.annotations.conditions.length === 0;
})
.map(tool => {
const properties: Record<string, TypeInfo> = {};
const required: string[] = [];
for (const [key, schema] of Object.entries(
tool.schema as unknown as Record<string, ZodSchema>,
)) {
const info = getZodTypeInfo(schema);
properties[key] = info;
if (isRequired(schema)) {
required.push(key);
}
}
return {
name: tool.name,
description: tool.description,
inputSchema: {
type: 'object',
properties,
required,
},
annotations: tool.annotations,
};
});
// Group tools by category (based on annotations)
const categories: Record<string, ToolWithAnnotations[]> = {};
toolsWithAnnotations.forEach((tool: ToolWithAnnotations) => {
const category = tool.annotations?.category || 'Uncategorized';
if (!categories[category]) {
categories[category] = [];
}
categories[category].push(tool);
});
// Sort categories using the enum order
const categoryOrder = Object.values(ToolCategory);
const sortedCategories = Object.keys(categories).sort((a, b) => {
const aIndex = categoryOrder.indexOf(a);
const bIndex = categoryOrder.indexOf(b);
// Put known categories first, unknown categories last
if (aIndex === -1 && bIndex === -1) {
return a.localeCompare(b);
}
if (aIndex === -1) {
return 1;
}
if (bIndex === -1) {
return -1;
}
return aIndex - bIndex;
});
return {toolsWithAnnotations, categories, sortedCategories};
}
async function generateToolDocumentation(): Promise<void> {
try {
console.log('Generating tool documentation from definitions...');
{
const {toolsWithAnnotations, categories, sortedCategories} =
getToolsAndCategories(createTools({slim: false} as ParsedArguments));
await generateReference(
'Chrome DevTools MCP Tool Reference',
OUTPUT_PATH,
toolsWithAnnotations,
categories,
sortedCategories,
[],
);
// Generate tools TOC and update README
const toolsTOC = generateToolsTOC(categories, sortedCategories);
updateReadmeWithToolsTOC(toolsTOC);
}
{
const {toolsWithAnnotations, categories, sortedCategories} =
getToolsAndCategories(createTools({slim: true} as ParsedArguments));
await generateReference(
'Chrome DevTools MCP Slim Tool Reference',
SLIM_OUTPUT_PATH,
toolsWithAnnotations,
categories,
sortedCategories,
['--slim'],
);
}
// Generate and update configuration options
const optionsMarkdown = generateConfigOptionsMarkdown();
updateReadmeWithOptionsMarkdown(optionsMarkdown);
process.exit(0);
} catch (error) {
console.error('Error generating documentation:', error);
process.exit(1);
}
}
// Run the documentation generator
generateToolDocumentation().catch(console.error);