generate_documentation
Automatically generate detailed code documentation and comments in multiple formats (inline, markdown, JSDoc) for various programming languages to enhance code readability and maintainability.
Instructions
为代码生成详细的文档和注释
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | 要生成文档的代码 | |
| docType | Yes | 文档类型 | |
| language | Yes | 编程语言 |
Implementation Reference
- src/tools/docGenerator.ts:6-20 (handler)Core implementation of the generate_documentation tool. This async function takes code, language, and docType parameters and dispatches to specific documentation generators (inline comments, JSDoc, or Markdown).export async function generateDocumentation( code: string, language: string, docType: 'inline' | 'markdown' | 'jsdoc' = 'markdown' ): Promise<string> { switch (docType) { case 'inline': return generateInlineComments(code, language); case 'jsdoc': return generateJSDoc(code, language); case 'markdown': default: return generateMarkdownDoc(code, language); } }
- src/index.ts:67-88 (registration)Registration of the 'generate_documentation' tool in the MCP server's ListTools response, including name, description, and JSON input schema.name: 'generate_documentation', description: '为代码生成详细的文档和注释', inputSchema: { type: 'object', properties: { code: { type: 'string', description: '要生成文档的代码' }, language: { type: 'string', description: '编程语言' }, docType: { type: 'string', enum: ['inline', 'markdown', 'jsdoc'], description: '文档类型' } }, required: ['code', 'language', 'docType'] } },
- src/index.ts:205-223 (handler)MCP server-side handler for the generate_documentation tool call. Validates inputs with Zod, invokes the core generateDocumentation function, and formats the response as MCP content.private async handleGenerateDocumentation(args: any) { const schema = z.object({ code: z.string(), language: z.string(), docType: z.enum(['inline', 'markdown', 'jsdoc']).default('markdown') }); const { code, language, docType } = schema.parse(args); const result = await generateDocumentation(code, language, docType); return { content: [ { type: 'text', text: result } ] }; }
- src/index.ts:206-210 (schema)Zod runtime validation schema used in the tool handler to parse and validate input arguments.const schema = z.object({ code: z.string(), language: z.string(), docType: z.enum(['inline', 'markdown', 'jsdoc']).default('markdown') });
- src/tools/docGenerator.ts:91-174 (helper)Helper function that generates comprehensive Markdown documentation from code analysis, including overview, classes, functions, constants, and usage examples.function generateMarkdownDoc(code: string, language: string): string { const analysis = analyzeCodeStructure(code, language); let markdown = `# 代码文档\n\n`; if (analysis.overview) { markdown += `## 概述\n\n${analysis.overview}\n\n`; } if (analysis.classes.length > 0) { markdown += `## 类\n\n`; analysis.classes.forEach(cls => { markdown += `### ${cls.name}\n\n`; markdown += `${cls.description}\n\n`; if (cls.properties.length > 0) { markdown += `#### 属性\n\n`; cls.properties.forEach((prop: any) => { markdown += `- **${prop.name}** (${prop.type}): ${prop.description}\n`; }); markdown += '\n'; } if (cls.methods.length > 0) { markdown += `#### 方法\n\n`; cls.methods.forEach((method: any) => { markdown += `##### ${method.name}\n\n`; markdown += `${method.description}\n\n`; if (method.parameters.length > 0) { markdown += `**参数:**\n\n`; method.parameters.forEach((param: any) => { markdown += `- \`${param.name}\` (${param.type}): ${param.description}\n`; }); markdown += '\n'; } if (method.returns) { markdown += `**返回值:** ${method.returns.type} - ${method.returns.description}\n\n`; } markdown += `\`\`\`${language}\n${method.code}\n\`\`\`\n\n`; }); } }); } if (analysis.functions.length > 0) { markdown += `## 函数\n\n`; analysis.functions.forEach(func => { markdown += `### ${func.name}\n\n`; markdown += `${func.description}\n\n`; if (func.parameters.length > 0) { markdown += `**参数:**\n\n`; func.parameters.forEach((param: any) => { markdown += `- \`${param.name}\` (${param.type}): ${param.description}\n`; }); markdown += '\n'; } if (func.returns) { markdown += `**返回值:** ${func.returns.type} - ${func.returns.description}\n\n`; } markdown += `\`\`\`${language}\n${func.code}\n\`\`\`\n\n`; }); } if (analysis.constants.length > 0) { markdown += `## 常量\n\n`; analysis.constants.forEach(constant => { markdown += `- **${constant.name}**: ${constant.description}\n`; }); markdown += '\n'; } if (analysis.usage) { markdown += `## 使用示例\n\n`; markdown += `\`\`\`${language}\n${analysis.usage}\n\`\`\`\n\n`; } return markdown; }