Building an AI Agent That Generates Documentation Automatically | Deployxa

Documentation is essential but nobody writes it. Here is how to build an AI agent that generates and updates documentation automatically from your code.

← Back to Dispatch Articles
Engineering Log

Building an AI Agent That Generates Documentation Automatically

Documentation is essential but nobody writes it. Here is how to build an AI agent that generates and updates documentation automatically from your code.

Building an AI Agent That Generates Documentation Automatically

Documentation is essential for maintainability, onboarding, and API consumption, but nobody writes it. Developers write code, not docs, which means documentation is always outdated, incomplete, or missing. What you need is an AI agent that generates and updates documentation automatically from your code: API docs from route definitions, README from the project structure, and code comments from function signatures. With an LLM and the Deployxa MCP server, this is straightforward to build. Here is how.

The direct answer is that a documentation generation agent is a script that analyzes your codebase (routes, functions, components), uses an LLM to generate documentation (API docs, README, code comments), and commits the documentation to your repository. The agent runs on a schedule (e.g., weekly) or on every pull request, which ensures the documentation is always up to date. For more on documentation, see our article on building Deployxa's documentation.

Why Manual Documentation Does Not Work

Three problems make manual documentation unsustainable. First, it is a low-priority task. Developers prioritize writing code over writing docs, which means docs are always outdated. Second, it is time-consuming. Writing good documentation takes time, which means it gets postponed indefinitely. Third, it goes stale. Even if you write documentation initially, it goes stale as the code changes, because nobody updates it. An AI agent solves all three problems: it generates documentation automatically (no manual work), it runs on a schedule (always up to date), and it updates the docs when the code changes (no staleness). For more on agentic patterns, see our article on building a custom AI deployment assistant.

What the Documentation Agent Does

The documentation generation agent performs the following tasks:

  1. Analyze the codebase. The agent analyzes the project structure, route definitions, function signatures, and component props to understand what the code does.
  1. Generate API documentation. For each API route, the agent generates documentation (endpoint, method, parameters, response format, example request/response).
  1. Generate README. The agent generates a README file that describes the project, how to install it, how to run it, and how to test it.
  1. Generate code comments. For each function, the agent generates a doc comment that describes what the function does, its parameters, and its return value.
  1. Commit the documentation. The agent commits the generated documentation to the repository (or creates a pull request for review).
  1. Deploy. If the documentation is a website (e.g., Astro, Docusaurus), the agent deploys it via the Deployxa MCP server.

Step-by-Step: Building the Documentation Agent

Here is how to build the documentation generation agent in Python, using an LLM and the Deployxa MCP server.

Step 1: Install dependencies

pip install openai requests
npm install -g @deployxa/mcp-server
deployxa-mcp login

Step 2: Create the agent script

# doc_agent.py
import openai
import os
import json
import subprocess
from pathlib import Path

REPO_PATH = Path("/path/to/repo")
DEPLOYXA_MCP_URL = "http://localhost:3000/mcp"

def call_deployxa(tool, params):
    import requests
    response = requests.post(DEPLOYXA_MCP_URL, json={"tool": tool, "params": params})
    return response.json()

def analyze_codebase():
    """Analyze the codebase to understand its structure."""
    routes = []
    
    # Find all route files (Next.js app router)
    for route_file in REPO_PATH.glob("app/**/route.ts"):
        content = route_file.read_text()
        routes.append({
            "path": str(route_file.relative_to(REPO_PATH)),
            "content": content[:500],  # First 500 chars
        })
    
    # Find all components
    components = []
    for comp_file in REPO_PATH.glob("components/**/*.tsx"):
        content = comp_file.read_text()
        components.append({
            "path": str(comp_file.relative_to(REPO_PATH)),
            "content": content[:500],
        })
    
    return {"routes": routes, "components": components}

def generate_api_docs(routes):
    """Generate API documentation from route definitions."""
    client = openai.OpenAI()
    
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {
                "role": "system",
                "content": "Generate API documentation in Markdown format from the route definitions. Include endpoint, method, parameters, response format, and example.",
            },
            {
                "role": "user",
                "content": f"Routes:\n{json.dumps(routes, indent=2)}",
            },
        ],
    )
    
    return response.choices[0].message.content

def generate_readme(codebase_info):
    """Generate a README file."""
    client = openai.OpenAI()
    
    # Get package.json
    pkg_path = REPO_PATH / "package.json"
    pkg_content = pkg_path.read_text() if pkg_path.exists() else "{}"
    
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {
                "role": "system",
                "content": "Generate a README.md file in Markdown format. Include: project description, installation, usage, scripts, and environment variables.",
            },
            {
                "role": "user",
                "content": f"package.json:\n{pkg_content}\n\nCodebase info:\n{json.dumps(codebase_info, indent=2)}",
            },
        ],
    )
    
    return response.choices[0].message.content

def generate_code_comments(file_path, content):
    """Generate code comments for a file."""
    client = openai.OpenAI()
    
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {
                "role": "system",
                "content": "Generate JSDoc/TSDoc comments for the functions and components in this file. Return the complete file with comments added.",
            },
            {
                "role": "user",
                "content": f"File: {file_path}\n\n{content}",
            },
        ],
    )
    
    return response.choices[0].message.content

def commit_documentation():
    """Commit the generated documentation."""
    subprocess.run(["git", "add", "."], cwd=REPO_PATH)
    subprocess.run(["git", "commit", "-m", "docs: auto-generated documentation"], cwd=REPO_PATH)
    subprocess.run(["git", "push"], cwd=REPO_PATH)

def deploy_documentation():
    """Deploy the documentation site via Deployxa."""
    result = call_deployxa("deployxa_deploy_workflow", {"app_id": "docs-app-id"})
    return result.get("status") == "success"

def main():
    print("Generating documentation...")
    
    # Step 1: Analyze codebase
    codebase = analyze_codebase()
    print(f"  Found {len(codebase['routes'])} routes, {len(codebase['components'])} components")
    
    # Step 2: Generate API docs
    api_docs = generate_api_docs(codebase["routes"])
    (REPO_PATH / "docs" / "api.md").write_text(api_docs)
    print("  Generated API docs")
    
    # Step 3: Generate README
    readme = generate_readme(codebase)
    (REPO_PATH / "README.md").write_text(readme)
    print("  Generated README")
    
    # Step 4: Generate code comments (for first 5 files)
    for comp in codebase["components"][:5]:
        file_path = REPO_PATH / comp["path"]
        content = file_path.read_text()
        commented = generate_code_comments(comp["path"], content)
        file_path.write_text(commented)
        print(f"  Added comments to {comp['path']}")
    
    # Step 5: Commit
    commit_documentation()
    print("  Committed documentation")
    
    # Step 6: Deploy (optional)
    # deploy_documentation()
    
    print("Documentation generation complete!")

if __name__ == "__main__":
    main()

Step 3: Run the agent

python doc_agent.py

The agent analyzes the codebase, generates API docs, README, and code comments, commits the documentation, and optionally deploys it.

Common Pitfalls and Troubleshooting

The first pitfall is generating inaccurate documentation. The LLM might generate documentation that does not match the code, which is worse than no documentation. The fix is to review the generated documentation before committing (or to create a pull request for review). The second pitfall is overwriting manual documentation. If the agent overwrites manually written documentation, you lose the human touch. The fix is to only generate documentation for files that do not have existing documentation, or to merge generated documentation with manual documentation. The third pitfall is not updating documentation when code changes. If the agent runs only once, the documentation goes stale as the code changes. The fix is to run the agent on every pull request (via CI/CD) or on a schedule (e.g., weekly). The fourth pitfall is generating too much documentation. Generating comments for every function is noisy, which makes the code harder to read. The fix is to generate comments only for public APIs (exported functions, route handlers), not for internal helpers. The fifth pitfall is not testing the documentation. Generated documentation might contain incorrect examples (e.g., wrong API endpoints, wrong parameters). The fix is to test the examples in the documentation by running them.

Conclusion: Let the Agent Write Your Docs

Manual documentation is outdated, incomplete, and missing. An AI agent that generates and updates documentation automatically is the sustainable solution. With an LLM and the Deployxa MCP server, you can build a documentation agent that keeps your docs up to date. Stop writing docs manually and start automating.

Ready to build your documentation agent? Install the Deployxa MCP server with npm i -g @deployxa/mcp-server, run deployxa-mcp login, and start building. For more on agentic workflows, see our articles on the agentic security scanning pipeline and building an AI agent that manages SSL certificates. Learn about the agentic performance testing pipeline and building an AI agent that manages team access in our companion articles. Explore our free developer tools to speed up your workflow.

Ready to deploy with Deployxa?

Deploy your apps globally with automatic SSL and AI diagnostics.

Start Free Now