Skip to main content
Glama

genapi

Generate API documentation from code in Markdown, OpenAPI, or JSDoc formats to streamline development workflows and maintain project documentation.

Instructions

【生成文档】为代码生成 API 文档(支持 Markdown、OpenAPI、JSDoc 格式)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeNo需要生成文档的代码
formatNo文档格式:markdown, openapi, jsdoc(默认 markdown)

Implementation Reference

  • Main handler function for the 'genapi' tool. It constructs a detailed prompt for generating API documentation from provided code and format, returning it as MCP content.
    export async function genapi(args: any) {
      try {
        const code = args?.code || "";
        const format = args?.format || "markdown"; // markdown, openapi, jsdoc
    
        const message = `请为以下代码生成 API 文档:
    
    📝 **代码**:
    ${code || "请提供需要生成文档的代码(函数、类、API 端点等)"}
    
    📖 **文档格式**:${format}
    
    ---
    
    🎯 **API 文档生成指南**:
    
    **基础信息**:
    - API 名称和描述
    - 版本信息
    - 基础 URL
    
    **详细文档**(每个端点/函数):
    
    1. **功能描述**
       - 简短说明(一句话)
       - 详细描述(用途、场景)
    
    2. **请求参数**
       | 参数名 | 类型 | 必填 | 描述 | 示例 |
       |--------|------|------|------|------|
       | id | string | 是 | 用户 ID | "12345" |
       | name | string | 否 | 用户名 | "张三" |
    
    3. **返回值**
       - 成功响应(状态码、数据结构、示例)
       - 错误响应(错误码、错误信息)
    
    4. **示例代码**
       \`\`\`typescript
       // 请求示例
       const response = await fetch('/api/users/123');
       const data = await response.json();
       
       // 响应示例
       {
         "code": 200,
         "data": {
           "id": "123",
           "name": "张三"
         }
       }
       \`\`\`
    
    5. **注意事项**
       - 权限要求
       - 速率限制
       - 废弃信息
       - 相关链接
    
    ---
    
    **Markdown 格式模板**:
    \`\`\`markdown
    # API 文档
    
    ## 用户管理
    
    ### 获取用户信息
    
    **接口地址**:\`GET /api/users/:id\`
    
    **功能描述**:根据用户 ID 获取用户详细信息
    
    **请求参数**:
    | 参数 | 类型 | 必填 | 描述 |
    |------|------|------|------|
    | id | string | 是 | 用户 ID |
    
    **返回示例**:
    \`\`\`json
    {
      "code": 200,
      "data": {
        "id": "123",
        "name": "张三",
        "email": "zhangsan@example.com"
      }
    }
    \`\`\`
    
    **错误码**:
    - 404: 用户不存在
    - 403: 无权限访问
    \`\`\`
    
    ---
    
    **OpenAPI 3.0 格式模板**:
    \`\`\`yaml
    openapi: 3.0.0
    info:
      title: User API
      version: 1.0.0
    paths:
      /api/users/{id}:
        get:
          summary: 获取用户信息
          parameters:
            - name: id
              in: path
              required: true
              schema:
                type: string
          responses:
            '200':
              description: 成功
              content:
                application/json:
                  schema:
                    type: object
                    properties:
                      code:
                        type: integer
                      data:
                        type: object
    \`\`\`
    
    ---
    
    现在请根据上述代码生成完整的 API 文档,并将文档保存到项目的 \`docs/api/\` 目录。`;
    
        return {
          content: [
            {
              type: "text",
              text: message,
            },
          ],
        };
      } catch (error) {
        const errorMessage =
          error instanceof Error ? error.message : String(error);
        return {
          content: [
            {
              type: "text",
              text: `❌ 生成 API 文档失败: ${errorMessage}`,
            },
          ],
          isError: true,
        };
      }
    }
  • Schema definition for the genapi tool in the ListToolsRequest handler, specifying input parameters: code (string) and format (string).
    {
      name: "genapi",
      description: "【生成文档】为代码生成 API 文档(支持 Markdown、OpenAPI、JSDoc 格式)",
      inputSchema: {
        type: "object",
        properties: {
          code: {
            type: "string",
            description: "需要生成文档的代码",
          },
          format: {
            type: "string",
            description: "文档格式:markdown, openapi, jsdoc(默认 markdown)",
          },
        },
        required: [],
      },
    },
  • src/index.ts:474-475 (registration)
    Registration of the genapi handler in the CallToolRequest switch statement, dispatching calls to the genapi function.
    case "genapi":
      return await genapi(args);
  • src/tools/index.ts:6-6 (registration)
    Re-export of the genapi function from its implementation file for use in the main index.
    export { genapi } from "./genapi.js";
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool generates API documentation but doesn't describe how it works (e.g., parsing code, generating structured output), potential limitations (e.g., code language support, accuracy), or output characteristics (e.g., format details, error handling). For a tool with no annotations, this is a significant gap in transparency.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is concise and front-loaded, stating the core purpose in a single sentence. It efficiently lists supported formats without unnecessary elaboration. However, it could be slightly improved by structuring key details (e.g., separating purpose from format list) for better readability.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (generating API documentation from code), lack of annotations, and no output schema, the description is incomplete. It doesn't explain the output format details, error cases, or behavioral traits needed for effective use. The description should provide more context to compensate for the missing structured data.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% description coverage, clearly documenting both parameters ('code' and 'format') with their types and purposes. The description adds no additional parameter semantics beyond what the schema provides, such as examples or constraints. With high schema coverage, the baseline score of 3 is appropriate, as the description doesn't compensate but also doesn't detract.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: '为代码生成 API 文档' (generate API documentation for code). It specifies the action (generate) and resource (API documentation), and mentions supported formats (Markdown, OpenAPI, JSDoc). However, it doesn't explicitly differentiate from sibling tools like 'gendoc' or 'genreadme', which may also generate documentation, so it misses full sibling differentiation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites, context (e.g., for API code vs. general code), or compare to siblings like 'gendoc' or 'genreadme'. The lack of usage context leaves the agent without clear direction on tool selection.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mybolide/mcp-probe-kit'

If you have feedback or need assistance with the MCP directory API, please join our Discord server