Terminal-Based AI Agents: A Controlled Workflow for AI-Assisted Development

    Build a controlled, auditable workflow for terminal-based AI agents, from research and drafting to design adaptation, testing, and staging. This guide covers MCP integration, least-privilege policies, approval gates, audit logs, rollback procedures, and a phased approach to automation.

    1. Why the Terminal Works as an Agent Workspace

    The terminal gives AI agents a straightforward working environment: they can read and write files, run commands, and invoke scripts without navigating a complex interface. Developers describe a goal in natural language, and the agent breaks it into a sequence of commands to execute step by step. Each step leaves a text record that can be reviewed when something goes wrong.

    The workflow in this guide has five stages: research collection, draft generation, design adaptation, validation, and staging with experiments. Each stage needs explicit inputs, outputs, and acceptance criteria. Without these boundaries, agents struggle to complete tasks reliably, and failures become harder to trace.

    2. Connect Tools and Data Sources with MCP

    MCP, or Model Context Protocol, is an open protocol that lets agent clients access external capabilities through a consistent interface. MCP servers can expose three types of capabilities:

    • Tools: executable operations.
    • Resources: readable data.
    • Prompts: reusable prompt templates.

    The client reads its configuration at startup to discover available servers.

    Configuration locations and field names vary by client. The following example uses the common mcpServers format. Check your client’s documentation before using it.

    1. {
    2. "mcpServers": {
    3. "web-fetch": {
    4. "command": "uvx",
    5. "args": ["mcp-server-fetch"]
    6. },
    7. "design-library": {
    8. "command": "node",
    9. "args": ["./tools/design-library/index.js"],
    10. "env": { "DESIGN_LIB_PATH": "./design/refs" }
    11. }
    12. }
    13. }

    The first server fetches web pages; the second reads a local library of design references. Both run as local processes.

    For services that require credentials, store secrets in environment variables or a secrets management service. Do not put them in configuration files or commit them to the repository.

    3. Establish a Project Directory Structure

    The pipeline relies on consistent directory conventions. Agents read and write only in designated locations, while reviewers can quickly find the artifacts produced at each stage.

    1. site/
    2. ├── content/
    3. │ ├── drafts/ # Stages 1 and 2: source excerpts and drafts
    4. │ └── posts/ # Approved articles
    5. ├── design/
    6. │ ├── tokens.json # Design tokens: colors, type sizes, radii, shadows, motion
    7. │ └── refs/ # Approved reference screenshots and source snippets
    8. ├── src/ # Page and component code
    9. ├── tests/
    10. │ ├── e2e/
    11. │ └── visual/
    12. ├── experiments/ # A/B experiment configuration
    13. └── agent/
    14. ├── policy.yaml # Permissions, approval gates, and resource limits
    15. └── audit/ # Operation logs

    4. Apply a Least-Privilege Policy

    By default, the agent should only read source material, write drafts, and run tests. Publishing, changing experiment traffic, and modifying infrastructure require approval. The following policy describes these boundaries.

    1. agent:
    2. allowed_paths:
    3. - content/
    4. - src/pages/
    5. - tests/
    6. - experiments/
    7. denied_paths:
    8. - .env
    9. - infra/
    10. - .github/workflows/
    11. network:
    12. allow_domains:
    13. - data.example-research.org
    14. commands:
    15. allow:
    16. - npm run build
    17. - npm test
    18. - npx playwright test
    19. deny:
    20. - "git push --force*"
    21. - "rm -rf*"
    22. approval_required:
    23. - action: deploy_production
    24. approver: content-lead
    25. - action: change_experiment_traffic
    26. approver: growth-owner
    27. limits:
    28. max_run_minutes: 30
    29. max_changed_files: 40

    This file is a declaration, not an enforcement mechanism. A sandbox or CI system must enforce the actual restrictions. The sandbox limits filesystem and network access; CI checks branch and approval status. Asking an agent to follow rules in a prompt does not guarantee security.

    5. Define Inputs, Outputs, and Failure Handling

    Stage Input Output Acceptance criteria Failure handling
    1. Research collection Topic and allowlisted sources Raw excerpts under content/drafts/<run-id>/ Every claim includes a source URL, and every source is allowlisted Retry the same source twice with increasing delays; if it still fails, skip it and record the failure
    2. Draft generation Raw excerpts and article template Markdown draft with sources listed in frontmatter Every numerical claim in the draft is traceable to the excerpts Preserve the draft, mark it needs-review, and wait for human review
    3. Design adaptation Draft and design/tokens.json New page under src/pages/ and screenshots Screenshot differences from the reference page stay below the team’s threshold Discard the new page if it exceeds the threshold and retain the previous version
    4. Validation Build artifacts Test reports Successful build, passing end-to-end tests, and no broken links Stop on any failure; do not proceed to staging
    5. Staging and experiments Validated build Staging URL and production version identifier Human approval If approval times out, retain only the staging version

    6. Run the Pipeline

    The following script connects the five stages. The agent run command is illustrative; replace it with the actual invocation for your agent tool.

    1. #!/usr/bin/env bash
    2. set -euo pipefail
    3. TOPIC="${1:?Usage: pipeline.sh 'topic'}"
    4. RUN_ID="r-$(date +%Y%m%d-%H%M%S)"
    5. BRANCH="agent/${RUN_ID}"
    6. DRAFT_DIR="content/drafts/${RUN_ID}"
    7. git switch -c "$BRANCH"
    8. # Stage 1: Collect sources; write only to the draft directory
    9. agent run --policy agent/policy.yaml --task collect --topic "$TOPIC" --out "$DRAFT_DIR"
    10. # Stage 2: Generate the draft
    11. agent run --policy agent/policy.yaml --task draft --in "$DRAFT_DIR" --out "$DRAFT_DIR"
    12. # Stage 3: Adapt the design; only src/pages/ is writable
    13. agent run --policy agent/policy.yaml --task design --tokens design/tokens.json --out src/pages/
    14. # Stage 4: Validate
    15. npm run build
    16. npx playwright test
    17. npx linkinator ./dist --recurse
    18. # Stage 5: Deploy to staging only. Production requires approval and is not deployed here.
    19. npm run deploy:staging
    20. git add content/ src/pages/ tests/
    21. git commit -m "agent: ${RUN_ID} ${TOPIC}"
    22. echo "Branch ${BRANCH} is ready. Open a merge request and wait for approval."

    The runtime environment needs Node.js, Python for the MCP server launched through uvx, Playwright browsers, and the required secrets configured as environment variables. Consult each tool’s documentation for version requirements.

    7. Maintain Audit Logs

    Record each tool call as one JSON line to support troubleshooting and compliance reviews.

    1. {"ts":"2026-11-23T08:30:12Z","run_id":"r-20261123-083000","actor":"pipeline-agent","action":"write_file","path":"content/drafts/r-20261123-083000/a.md","policy_rule":"allowed_paths","result":"ok","approver":null}

    Every entry must include:

    • Timestamp.
    • Run identifier.
    • Actor.
    • Action.
    • Target path or command.
    • Applicable policy rule.
    • Execution result.
    • Approver.

    Store logs separately. The agent must not have write access to the audit directory.

    8. Plan for Rollback and Circuit Breakers

    • Isolate each run. Work on a dedicated branch. The agent must not push directly to the main branch.
    • Revert faulty releases. If a problem appears after a merge, use git revert to undo the corresponding merge commit, then redeploy the previous version tag.
    • Stop when limits are exceeded. Trigger a circuit breaker if execution exceeds max_run_minutes, changed files exceed max_changed_files, or the same batch fails validation twice consecutively. Stop immediately and notify the owner.
    • Keep experiments reversible. Start A/B experiments with a small share of traffic and retain a one-click off switch. Disabling the experiment should restore the original page.

    9. Turn Design Preferences into Testable Rules

    Instructions such as “keep it subtle” cannot be reliably evaluated by a script or agent. Replace them with measurable constraints before adding them to an automated workflow.

    The following is a starting template. Adjust the values to match your team’s brand.

    1. {
    2. "typography": { "h1": "32px/40px", "h2": "24px/32px", "body": "16px/24px" },
    3. "radius": { "max": "8px" },
    4. "shadow": { "levels": 2 },
    5. "motion": { "duration": "200-250ms", "easing": "ease-out" },
    6. "review": { "screenshotDiffThreshold": "Set by the team during initial acceptance review" }
    7. }

    After each successful acceptance review, add the page screenshots and corresponding token changes to design/refs/. Keep the version history in Git so the team can review how its design patterns evolve.

    10. Decide Which Tasks to Delegate

    Task Suitable for automatic execution? Approver Notes
    Collect public source material Yes None Allowlisted sources only
    Generate drafts Yes Editor Drafts must receive human review
    Change experiment traffic Not without approval Growth owner Approval is mandatory
    Deploy to production Not without approval Content lead Approval is mandatory
    Process personal user data No, not by default Data owner Classify the data before deciding whether to grant access
    Modify infrastructure configuration No Operations lead Not permitted by the policy

    11. Start with Research and Drafting

    Begin with stages one and two only, and have editors review drafts daily. Once source accuracy and draft quality are consistent, enable design adaptation. Consider automated staging and A/B experiments only after those earlier stages are stable.

    For every additional stage, run the complete workflow in staging for at least one release cycle before moving on to the next stage.