
Mcp Server Terraform
io.github.RajeevSirohi由 GitHub 帳號 RajeevSirohi 發布v0.2.1更新於 Oct 24, 2026
Safety-first Terraform: confirmation-gated apply/destroy, risk & cost analysis, drift detection
概覽
一個本機 MCP 伺服器,讓助理執行 Terraform CLI 指令(init、plan、apply、destroy、state 與漂移檢查),並具備確認流程和成本風險摘要。
- 可以做什麼
- 初始化並驗證 Terraform 工作目錄與設定,在執行前檢查雲端供應商 CLI 的登入狀態。執行 plan 並回傳差異,附上風險與成本摘要,標出將被銷毀的資源與高價的常駐資源。透過兩步確認流程執行 apply 或 destroy,並讀取 state 中的輸出值。檢視與修改 state 項目,列出、選擇與建立工作區,以及匯入、標記、取消標記或重新整理資源。以僅重新整理方式執行 plan 偵測漂移,回報在 Terraform 之外被改動的資源。
- 適用情境
- 適合使用 Terraform 的人在對話中檢視 plan、執行 apply 或 destroy、檢查 state 與輸出,或診斷 plan 與 apply 失敗,而不必切換到終端機。也適合需要停用破壞性工具並記錄每次呼叫的謹慎或共用環境。
- 執行需求
- 以 stdio 在本機執行,通常透過 npm 套件的 npx 或從原始碼建置後啟動。需要 Node.js 18 或更新版本、PATH 中的 Terraform CLI,以及相容 MCP 的用戶端(如 Claude Desktop)。目標雲端供應商的憑證需事先設定好。選用環境變數:ALLOW_ONLY_NON_DESTRUCTIVE_TOOLS、ALLOW_ONLY_READONLY_TOOLS、ALLOWED_TOOLS 和 AUDIT_LOG_PATH。
安裝
在 SourceWeft 中
- 開啟 儀表板中的 Mcp Server Terraform,將其新增到工作區。
- 為需要使用其工具的對話啟用該服務。
Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。
其他 MCP 客戶端
參照 儲存庫 中的啟動說明。
README
mcp-server-terraform
A Model Context Protocol (MCP) server that lets Claude manage Terraform infrastructure through natural language.
Run plans, apply changes, inspect state, and diagnose failures — all from a Claude conversation.
What it does
Instead of switching to a terminal to run terraform plan, you can ask Claude:
"Plan the changes in
/infra/stagingand explain what will change"
"Apply it — but only if no resources will be destroyed"
"Show me all the outputs from the prod workspace"
"Something broke after the last apply — diagnose it"
The server translates these into real terraform CLI commands on your machine, with a built-in safety confirmation flow before any destructive operation runs.
Tools
Plan risk & cost summary
Every plan (and every apply preview) is analyzed via terraform show -json and
annotated with a structured summary — destroyed resources are called out, and
always-on resources that commonly cause bill shock are flagged with rough
monthly costs:
Cost-flagged resource types include NAT gateways, load balancers, EKS/AKS/GKE control planes, RDS/Cloud SQL instances, ElastiCache, Redshift, MSK, and Azure Firewall (~$900/month!).
Drift detection
tf_drift runs a refresh-only plan and reports resources that were changed
outside Terraform (e.g. manually in the cloud console), with the changed
attribute names and remediation options.
Audit logging
Set AUDIT_LOG_PATH to a file path and every tool call is appended as a JSON
line with timestamp, tool name, outcome, and duration. Variable values are
always redacted (db_password=<redacted>) — only names are logged.
Confirmation flow
tf_apply and tf_destroy use a two-step safety flow:
- First call (no
confirmed) → runsterraform plan, shows the diff, does nothing else - Second call (
confirmed: true) → actually applies or destroys
Claude is instructed to never pass confirmed: true without first presenting the plan to you.
Prompt
The server exposes a /tf-diagnose prompt that guides Claude through a systematic 5-step diagnosis of plan or apply failures.
Prerequisites
- Node.js 18 or later
- Terraform CLI on your PATH
- Claude Desktop (or any MCP-compatible client)
Installation
No install needed — run it straight from npm:
Or, for development, from source:
Claude Desktop setup
Add to your Claude Desktop config file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
(If running from source instead, use "command": "node" with "args": ["/absolute/path/to/dist/index.js"].)
Restart Claude Desktop. You should see a hammer icon indicating tools are available.
Usage
Once connected, just talk to Claude about your Terraform workspaces in plain language. Some examples:
First time in a new workspace
"Initialize the terraform config in
C:\infra\stagingand check if I'm logged into the right cloud accounts"
Claude runs tf_init, then tf_preflight — if you're not authenticated it tells you exactly which command to run (az login, aws configure, ...).
The everyday plan → review → apply loop
"Plan the changes in
C:\infra\stagingand explain what will change"
You get the plan diff plus a summary: how many resources created/updated/destroyed, anything destructive called out explicitly, and cost warnings for expensive always-on resources.
"Looks good, apply it"
Claude shows the plan preview one more time and asks for your confirmation — nothing is applied until you say yes. This two-step gate is built into the server itself, not just the prompt, so Claude cannot skip it.
Checking on your infrastructure
"Did anyone change anything outside terraform in the prod workspace?"
tf_drift compares state against reality and reports what was modified in the console, with options to accept or revert.
"Show me all the outputs" · "List everything in state" · "What workspaces exist?"
Learning / experimenting (e.g. cert prep)
"Apply the VPC lab in
C:\labs\vpc, and when I say 'done' destroy everything"
The cost flags are your friend here — if a lab creates a NAT gateway or EKS cluster, the plan summary warns you what it costs per month if forgotten:
Fixing things
"terraform plan is failing in
C:\infra\staging— diagnose it"
The /tf-diagnose prompt walks Claude through validate → providers → plan → state → outputs systematically. There's also /tf-login for step-by-step authentication setup per provider.
"Import the S3 bucket
my-legacy-bucketinto state asaws_s3_bucket.legacy"
"Taint the web server so it gets recreated on the next apply"
Recommended setup for shared or cautious environments
Run with ALLOW_ONLY_NON_DESTRUCTIVE_TOOLS=true (see Safety modes below) so apply/destroy are unavailable entirely, and set AUDIT_LOG_PATH so every operation is logged.
Safety modes
Control which tools are available via environment variables:
Example — read-only mode:
Development
Project structure
Adding a new tool
- Create
src/tools/tf-yourcommand.ts— export a*Schemaconst and an async handler function - Import both in
src/index.ts - Add the schema to
readonlyToolsordestructiveToolsarray - Add a
casein theCallToolRequestSchemahandler switch
Roadmap
- Terraform Cloud / Enterprise API support (Phase 2)
- OpenTelemetry tracing
- SSE / streamable HTTP transport for remote deployments
- Docker image on GitHub Container Registry
- Plan risk & cost analysis
- Drift detection
- Import / taint / untaint / refresh
- Audit logging
- CI (build, test matrix, e2e against real terraform)
Contributing
See CONTRIBUTING.md.
License
MIT — see LICENSE.
來源:README.md,提交 07982db
工具
0版本歷史
1- v0.2.1最新Sep 16, 2026
評分
已滿足適用檢查項的 50%
該評分衡量此收錄條目的完整程度,依據註冊中心條目、端點與公開來源計算得出。它不是對伺服器本身的評價,也不表示伺服器安全或目前可用:可連通性是帶日期的檢查結果,評分也不包含安裝量或使用者評價。
- 近 30 天內可連通25 分
請保持端點能夠回應 MCP 握手;SourceWeft 每 30 天探測一次。
- 已宣告認證方式10 分
請在註冊中心條目中宣告憑證請求標頭,或保持端點能夠回應探測,以便判定其認證方式。
- 已列出工具15 分
請讓伺服器在無憑證時回應 tools/list,或發布可在沙箱中無憑證啟動的軟體包。
- 描述必需15 分
- 圖示10 分
- 文件15 分
- 授權條款10 分