|
6 | 6 |
|
7 | 7 | import fs from 'node:fs'; |
8 | 8 |
|
9 | | -import {Client} from '@modelcontextprotocol/sdk/client/index.js'; |
10 | | -import {StdioClientTransport} from '@modelcontextprotocol/sdk/client/stdio.js'; |
11 | 9 | import type {Tool} from '@modelcontextprotocol/sdk/types.js'; |
12 | 10 |
|
13 | 11 | import {cliOptions} from '../build/src/cli.js'; |
14 | 12 | import {ToolCategory, labels} from '../build/src/tools/categories.js'; |
| 13 | +import {tools} from '../build/src/tools/tools.js'; |
15 | 14 |
|
16 | | -const MCP_SERVER_PATH = 'build/src/index.js'; |
17 | 15 | const OUTPUT_PATH = './docs/tool-reference.md'; |
18 | 16 | const README_PATH = './README.md'; |
19 | 17 |
|
@@ -162,34 +160,106 @@ function updateReadmeWithOptionsMarkdown(optionsMarkdown: string): void { |
162 | 160 | console.log('Updated README.md with options markdown'); |
163 | 161 | } |
164 | 162 |
|
165 | | -async function generateToolDocumentation(): Promise<void> { |
166 | | - console.log('Starting MCP server to query tool definitions...'); |
167 | | - |
168 | | - // Create MCP client with stdio transport pointing to the built server |
169 | | - const transport = new StdioClientTransport({ |
170 | | - command: 'node', |
171 | | - args: [MCP_SERVER_PATH, '--channel', 'canary'], |
172 | | - }); |
173 | | - |
174 | | - const client = new Client( |
175 | | - { |
176 | | - name: 'docs-generator', |
177 | | - version: '1.0.0', |
178 | | - }, |
179 | | - { |
180 | | - capabilities: {}, |
181 | | - }, |
182 | | - ); |
| 163 | +// Helper to convert Zod schema to JSON schema-like object for docs |
| 164 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 165 | +function getZodTypeInfo(schema: any): { |
| 166 | + type: string; |
| 167 | + enum?: string[]; |
| 168 | + items?: any; |
| 169 | + description?: string; |
| 170 | + default?: any; |
| 171 | +} { |
| 172 | + let description = schema.description; |
| 173 | + let def = schema._def; |
| 174 | + let defaultValue = undefined; |
| 175 | + |
| 176 | + // Unwrap optional/default/effects |
| 177 | + while ( |
| 178 | + def.typeName === 'ZodOptional' || |
| 179 | + def.typeName === 'ZodDefault' || |
| 180 | + def.typeName === 'ZodEffects' |
| 181 | + ) { |
| 182 | + if (def.typeName === 'ZodDefault') { |
| 183 | + defaultValue = def.defaultValue(); |
| 184 | + } |
| 185 | + schema = def.innerType || def.schema; |
| 186 | + def = schema._def; |
| 187 | + if (!description && schema.description) description = schema.description; |
| 188 | + } |
| 189 | + |
| 190 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 191 | + const result: any = {}; |
| 192 | + if (description) result.description = description; |
| 193 | + if (defaultValue !== undefined) result.default = defaultValue; |
| 194 | + |
| 195 | + switch (def.typeName) { |
| 196 | + case 'ZodString': |
| 197 | + result.type = 'string'; |
| 198 | + break; |
| 199 | + case 'ZodNumber': |
| 200 | + result.type = def.checks?.some((c: any) => c.kind === 'int') |
| 201 | + ? 'integer' |
| 202 | + : 'number'; |
| 203 | + break; |
| 204 | + case 'ZodBoolean': |
| 205 | + result.type = 'boolean'; |
| 206 | + break; |
| 207 | + case 'ZodEnum': |
| 208 | + result.type = 'string'; |
| 209 | + result.enum = def.values; |
| 210 | + break; |
| 211 | + case 'ZodArray': |
| 212 | + result.type = 'array'; |
| 213 | + result.items = getZodTypeInfo(def.type); |
| 214 | + break; |
| 215 | + default: |
| 216 | + result.type = 'unknown'; |
| 217 | + } |
| 218 | + return result; |
| 219 | +} |
183 | 220 |
|
| 221 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 222 | +function isRequired(schema: any): boolean { |
| 223 | + let def = schema._def; |
| 224 | + while (def.typeName === 'ZodEffects') { |
| 225 | + schema = def.schema; |
| 226 | + def = schema._def; |
| 227 | + } |
| 228 | + return def.typeName !== 'ZodOptional' && def.typeName !== 'ZodDefault'; |
| 229 | +} |
| 230 | + |
| 231 | +async function generateToolDocumentation(): Promise<void> { |
184 | 232 | try { |
185 | | - // Connect to the server |
186 | | - await client.connect(transport); |
187 | | - console.log('Connected to MCP server'); |
| 233 | + console.log('Generating tool documentation from definitions...'); |
| 234 | + |
| 235 | + // Convert ToolDefinitions to ToolWithAnnotations |
| 236 | + const toolsWithAnnotations: ToolWithAnnotations[] = tools.map(tool => { |
| 237 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 238 | + const properties: any = {}; |
| 239 | + const required: string[] = []; |
| 240 | + |
| 241 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 242 | + for (const [key, schema] of Object.entries(tool.schema as any)) { |
| 243 | + const info = getZodTypeInfo(schema); |
| 244 | + properties[key] = info; |
| 245 | + if (isRequired(schema)) { |
| 246 | + required.push(key); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + return { |
| 251 | + name: tool.name, |
| 252 | + description: tool.description, |
| 253 | + inputSchema: { |
| 254 | + type: 'object', |
| 255 | + properties, |
| 256 | + required, |
| 257 | + }, |
| 258 | + annotations: tool.annotations, |
| 259 | + }; |
| 260 | + }); |
188 | 261 |
|
189 | | - // List all available tools |
190 | | - const {tools} = await client.listTools(); |
191 | | - const toolsWithAnnotations = tools as ToolWithAnnotations[]; |
192 | | - console.log(`Found ${tools.length} tools`); |
| 262 | + console.log(`Found ${toolsWithAnnotations.length} tools`); |
193 | 263 |
|
194 | 264 | // Generate markdown documentation |
195 | 265 | let markdown = `<!-- AUTO GENERATED DO NOT EDIT - run 'npm run docs' to update--> |
@@ -322,8 +392,6 @@ async function generateToolDocumentation(): Promise<void> { |
322 | 392 | // Generate and update configuration options |
323 | 393 | const optionsMarkdown = generateConfigOptionsMarkdown(); |
324 | 394 | updateReadmeWithOptionsMarkdown(optionsMarkdown); |
325 | | - // Clean up |
326 | | - await client.close(); |
327 | 395 | process.exit(0); |
328 | 396 | } catch (error) { |
329 | 397 | console.error('Error generating documentation:', error); |
|
0 commit comments