Skip to main content
Glama
README.md
# opentofu-mcp

**A Model Context Protocol server that lets Cursor drive OpenTofu — without letting it run away with your infrastructure.**

Large language models are good at writing infrastructure code and bad at knowing when *not* to execute it. This server exposes OpenTofu as a small set of narrow, auditable tools, built around one rule:

> `apply` only ever runs a plan file that already exists on disk, and only when the caller literally passes `confirm: "APPLY"`.

Everything else is read-only or produces an artefact you can inspect first.

---

## Why this exists

The usual loop with an AI assistant and Terraform/OpenTofu looks like this: the model edits `.tf` files, then someone copy-pastes commands into a terminal and hopes the plan is what the model said it was.

This server closes that gap:

- the model can **generate a plan** and read it back as JSON;
- **you** review the plan;
- the model applies **that exact plan file**, nothing else;
- every invocation is written to an **audit log** with arguments, exit code and duration.

It is deliberately boring. That is the point.

---

## Safety model

| Control | Behaviour |
|---|---|
| **No shell** | Commands are spawned with `shell: false`; arguments are passed as an array |
| **Working directory confinement** | Absolute paths are rejected; relative paths are resolved and refused if they escape `TOFU_WORKDIR` |
| **No `-auto-approve`** | The flag is rejected before the process is spawned |
| **No `-destroy` on apply** | Destructive applies must be planned explicitly, reviewed, and applied as a plan |
| **Explicit confirmation** | `tofu_apply` requires the literal string `APPLY` |
| **Plan-first** | `tofu_apply` refuses to run without an existing plan file |
| **Audit trail** | One JSON line per call: timestamp, tool, args, cwd, exit code, duration |
| **Timeouts** | Every command is killed after `TOFU_MCP_TIMEOUT_MS` (default 5 minutes) |

The server never stores credentials. It inherits the environment you launch it with, exactly like running `tofu` yourself.

---

## Requirements

- **Node.js 18+**
- **OpenTofu** installed and reachable (`tofu version`)
- A directory containing your `.tf` configuration

---

## Install

```bash
git clone https://github.com/aleraze/opentofu-mcp.git
cd opentofu-mcp
npm install
npm run build
```

The compiled entry point is `dist/index.js`.

---

## Configure Cursor

Add the server to Cursor's MCP configuration — either globally (`~/.cursor/mcp.json`) or per project (`.cursor/mcp.json`):

```json
{
  "mcpServers": {
    "opentofu": {
      "command": "node",
      "args": ["/absolute/path/to/opentofu-mcp/dist/index.js"],
      "env": {
        "TOFU_WORKDIR": "/absolute/path/to/your/infrastructure",
        "TOFU_BIN": "tofu",
        "TOFU_MCP_TIMEOUT_MS": "300000"
      }
    }
  }
}
```

Then open **Cursor → Settings → MCP** and enable the `opentofu` server. The tools appear in the chat's tool picker.

> `TOFU_WORKDIR` is the only directory the server will operate on. Point it at your infrastructure repository, not at your home directory.

---

## Tools

| Tool | Arguments | Changes infrastructure? |
|---|---|---|
| `tofu_init` | `upgrade?`, `migrateState?` | No — downloads providers, prepares the backend |
| `tofu_validate` | — | No |
| `tofu_fmt` | `check?` | No — rewrites formatting only when `check` is false |
| `tofu_plan` | `out?`, `varFile?`, `destroy?`, `target?` | No — writes a plan file |
| `tofu_show` | `planFile?`, `json?` | No — reads a plan or the current state |
| `tofu_output` | `json?` | No |
| `tofu_state_list` | — | No |
| `tofu_apply` | `planFile`, `confirm: "APPLY"` | **Yes** |

Output is truncated at 40,000 characters; use `tofu_show` with `json: true` to inspect a specific part of a large plan.

---

## A real workflow: single node → three nodes

This is the loop the server was built for — growing a single-node cluster into a three-node one without blind applies.

```text
1. tofu_init            { upgrade: false }
2. tofu_validate        {}
3. tofu_fmt             { check: true }          → the diff must be empty
4. tofu_plan            { out: "grow-3nodes.tfplan", varFile: "prod.tfvars" }
5. tofu_show            { planFile: "grow-3nodes.tfplan" }      → you read what will change
6. — human review, out loud, on the plan —
7. tofu_apply           { planFile: "grow-3nodes.tfplan", confirm: "APPLY" }
8. tofu_output          {}
```

If step 5 shows anything you did not expect, you delete the plan file and go back to step 4. Nothing has been touched.

---

## Environment variables

| Variable | Default | Purpose |
|---|---|---|
| `TOFU_WORKDIR` | `process.cwd()` | The only directory the server operates on |
| `TOFU_BIN` | `tofu` | Path to the OpenTofu binary |
| `TOFU_MCP_TIMEOUT_MS` | `300000` | Per-command timeout |
| `TOFU_MCP_AUDIT_LOG` | `<workdir>/.opentofu-mcp-audit.log` | Where the audit trail is written |

---

## Audit log

Each call appends a single JSON line:

```json
{"ts":"2026-12-03T09:14:02.118Z","tool":"tofu_apply","args":["apply","-input=false","-no-color","/srv/infra/grow-3nodes.tfplan"],"cwd":"/srv/infra","exitCode":0,"durationMs":48213,"outcome":"ok","note":"plan applied"}
```

It is append-only and never rotated automatically: treat it as evidence, archive it with your logs.

---

## Limitations

- **One working directory per process.** Multiple environments means multiple server entries in `mcp.json`.
- **No remote state helpers.** Backend authentication is whatever your environment already provides.
- **No `tofu import` yet** — it mutates state without a plan, so it is intentionally out of scope.
- **No `destroy` shortcut.** Plan with `destroy: true`, review, then apply the plan.

Roadmap, in order of usefulness: `tofu_state_show`, `tofu_import` behind an explicit plan, a `--dry-run` global switch, and Windows path tests.

---

## License

MIT — see [LICENSE](LICENSE).