Guide demonstrating how to run Terraform via the tfmcp MCP bridge using JSON-RPC over stdio to plan and apply a simple local file resource, including setup and security notes
This guide shows how to run Terraform operations through an MCP (Message Control Plane) server using the Terraform MCP bridge (tfmcp). You will learn to initialize a Terraform workspace, plan and apply a simple configuration that creates a local file — all triggered by MCP-style JSON-RPC calls (stdio). This workflow is useful when you want programmatic, message-driven control of Terraform from agents, automation pipelines, or AI assistants that already speak MCP/JSON-RPC.
Install the required system packages, Rust toolchain, and the Terraform MCP bridge. The following table summarizes the key prerequisites and where to find them.
Requirement
Purpose
Install / Reference
build tools
Compile native dependencies
See the command below
Rust (rustup)
Build the tfmcp crate
See the command below
Terraform MCP bridge
Runs the MCP server and handles Terraform calls
cargo install tf-mcp or cargo install terraform-mcp
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh# follow the prompts and add Rust to your PATH (typically by sourcing ~/.cargo/env)source "$HOME/.cargo/env"
Install the Terraform MCP bridge (the binary is commonly named tfmcp):
# Either of these may be available depending on the crate name/versioncargo install tf-mcp# orcargo install terraform-mcp
Cargo will download and compile crates during installation; when complete the tfmcp binary should be on your PATH.
Create a working directory and a minimal Terraform configuration that writes a local file.Create the directory and enter it:
mkdir terraform-democd terraform-demo
Create main.tf with the following content:
terraform { required_providers { local = { source = "hashicorp/local" version = "~> 2.0" } }}provider "local" {}resource "local_file" "example" { filename = "${path.module}/example.txt" content = "Hello from tfmcp!"}
Initialize and validate the workspace, then create a plan:
terraform initterraform validateterraform plan
Sample (cleaned) plan output:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + createTerraform will perform the following actions: # local_file.example will be created + resource "local_file" "example" { + content = "Hello from tfmcp!" + filename = "./example.txt" + directory_permission = "0777" + file_permission = "0777" + id = (known after apply) }Plan: 1 to add, 0 to change, 0 to destroy.
By default the MCP bridge may block automated apply operations for safety. If auto-approve is blocked, you will see an error such as:
{ "jsonrpc":"2.0", "id":3, "error":{ "code":-32603, "message":"Failed to apply Terraform configuration: Auto-approve for apply operation blocked by security policy. Set TFMCP_ALLOW_AUTO_APPROVE=true to enable." }}
Setting TFMCP_ALLOW_AUTO_APPROVE=true allows automated apply operations. Only enable this when you trust the Terraform configuration and understand the security implications.
If you decide to allow auto-approve, export the environment variable and re-run the JSON-RPC sequence:
Consistent control channel: If your orchestration or automation stack already uses MCP/JSON-RPC, adding Terraform as an MCP tool keeps infrastructure control within the same messaging paradigm.
Programmatic integration: Enables other agents, CI/CD pipelines, or AI assistants to request Terraform operations programmatically without shelling out or managing separate APIs.
Auditability & policy: An MCP gateway can centralize security checks, logging, and policy enforcement around Terraform operations.
Ensure tfmcp is on your PATH after cargo install (check ~/.cargo/bin).
If terraform init fails, review provider version constraints and network access to provider registries.
If JSON-RPC messages are not being processed, confirm tfmcp mcp is running in the terminal where you expect it and that your heredoc is directed at the same tfmcp instance.
If you need an example for integrating this into a CI pipeline or automating the JSON-RPC calls programmatically (Python, Node, etc.), I can provide sample clients that interact with tfmcp via stdio.