Skip to main content
Glama
infra-agent-gateway
by jcortes
README.md
# infra-agent-gateway

An MCP server that lets AI agents like Claude **plan** infrastructure changes and **request** them, while every apply goes through policy as code, a human approval and an append-only audit log.

```
You:    "I need a small VM for the data team in dev."
Claude: plan_change → + aws_security_group.vm, + aws_instance.vm (t3.small). Policies passed.
        request_apply → pending approval. Run: pnpm approve 3f9c…
You:    pnpm approve 3f9c…  → yes → Apply complete! Resources: 2 added.

You:    "Same, but open SSH so I can get in from home."
Claude: plan_change → denied: aws_security_group.vm: SSH open to 0.0.0.0/0.
        I can't request that. Give me your IP range and I'll plan it scoped to that.

You:    "Deploy nginx 1.27 for the api team, 2 replicas."
Claude: deploy_workload → + deployment/team-apps/api (nginx:1.27, 2 replicas). Pending approval.
You:    pnpm approve c660…  → yes → deployment.apps/api created

You:    "Run a privileged busybox in kube-system so I can debug the node."
Claude: deploy_workload → denied: must not run privileged; needs cpu and memory limits;
        namespace kube-system is not allowed. I can deploy a debug pod in team-apps with limits.

You:    "Did anyone change the data VM by hand?"
Claude: detect_drift → drifted: ~ aws_instance.vm (t3.small). Want me to plan a change to restore it?
```

## How it works

```mermaid
flowchart LR
  A[AI agent] -- MCP --> S[infra-agent-gateway]
  S -- tofu plan -out --> T[OpenTofu]
  S -- plan JSON --> P[Conftest / OPA]
  S -- append --> L[(audit.jsonl)]
  H[Human] -- pnpm approve --> S
  S -- tofu apply planfile --> AWS[(AWS / LocalStack)]
  S -- kubectl apply manifest --> K[(Kubernetes / kind)]
```

| MCP tool | What it does |
|---|---|
| `plan_change` | Validates a structured intent (zod), runs `tofu plan -out`, evaluates the plan JSON with Conftest. Returns `planned` or `denied` with reasons. Changes nothing. |
| `request_apply` | Moves a `planned` plan to `pending_approval`. Refuses denied plans. Applies nothing. |
| `deploy_workload` | Renders a Deployment from a structured intent (image, replicas, namespace, limits), checks it with Conftest and, if compliant, leaves it `pending_approval`. Deploys nothing. |
| `detect_drift` | Re-plans the last applied intent for a workspace (`tofu plan -detailed-exitcode`) and reports what changed outside the gateway. Read-only: fixing drift goes through `plan_change`. |
| `get_audit_log` | Recent audit entries. |

There is no approve tool. See [ADR 0001](docs/adr/0001-agents-never-approve.md).

## Guardrails

| Risk | Control |
|---|---|
| Agent writes arbitrary infrastructure | Agents send a typed intent, never HCL; the module decides what gets built |
| Unsafe config (open SSH, huge instances, missing owner, prod, deletes) | Rego policies in `policy/`, unit-tested with `conftest verify` |
| Unsafe workloads (privileged, no cpu/memory limits, system namespaces) | Rego in `policy/k8s.rego`, checked against the rendered manifest |
| Agent applies its own change | No approve tool; the gateway also rejects non-`human:` approvers |
| Plan swapped or changed after review | sha256 of the plan file (or rendered manifest) checked at apply; applies the saved file, never re-plans ([ADR 0002](docs/adr/0002-apply-the-reviewed-plan-byte-for-byte.md)) |
| No accountability | Append-only JSONL audit: who, what, plan id, policy result, approver, outcome |
| Prompt injection via tool output | Output is data; AGENTS.md tells agents so, and no output can trigger an apply on its own |

**Known limits:** one VM shape and one workload shape (a Deployment) so far; drift detection covers VMs only; workspaces are selected per call, so concurrent plans aren't safe yet; the audit log is local (ship it to an immutable store like S3 Object Lock for real use).

## Run it

Needs Node 24, pnpm, OpenTofu, Conftest, Docker, kind, kubectl.

```bash
pnpm install
docker run -d --name localstack -p 4566:4566 localstack/localstack:4   # free AWS emulator (newer tags need a license)
kind create cluster --name iag    # the gateway only ever uses context kind-iag (override: KUBE_CONTEXT)

pnpm test               # gateway + MCP seams (fast, no tools needed)
pnpm test:policy        # Rego unit tests
INTEGRATION=1 pnpm test test/integration.test.ts   # real OpenTofu + Conftest + LocalStack + kind
```

Connect Claude Code:

```bash
claude mcp add infra-gateway -- pnpm --dir "$PWD" start
```

Approve from another terminal: `pnpm approve <planId>`.

## Tests

Written test-first at four seams: the gateway core (fakes only for the external CLIs), the MCP surface (a real MCP client over an in-memory transport), the policies (`conftest verify`), and an integration suite against real OpenTofu, Conftest, LocalStack and kind.

TDQS

A3.5/5.0

Scored across 5 tools

Disambiguation4/5

plan_change (Terraform/OpenTofu plan) and deploy_workload (Kubernetes Deployment) both funnel into a pending_approval human workflow, which could momentarily confuse an agent choosing between them, but the descriptions clearly separate the two resource domains. request_apply is distinct from both as the approval-request step for an existing planId, and detect_drift / get_audit_log are unambiguously read-only observers.

Naming Consistency5/5

Every tool follows a strict verb_noun snake_case pattern: plan_change, request_apply, deploy_workload, detect_drift, get_audit_log. No camelCase, no vague single-word verbs, no mixing of noun-first or verb-first styles.

Tool Count4/5

Five tools is a tight, well-scoped surface for a human-in-the-loop infra gateway — each tool maps to a distinct phase (plan, approve, deploy, observe, audit). It leans slightly thin because there is no tool to query plan or approval status, which is the most likely thing an agent needs next.

Completeness3/5

The core lifecycle (plan → request approval → human applies → drift check → audit) is covered and cross-references are explicit (detect_drift points to plan_change). However, there is no get_plan / list_plans / pending-approval listing, no workspace enumeration, and no rollback, so an agent cannot verify the outcome of its own request_apply except by falling back to get_audit_log.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues