Skip to main content
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.

Prerequisites (Ubuntu)

Install the required system packages, Rust toolchain, and the Terraform MCP bridge. The following table summarizes the key prerequisites and where to find them. Install the build tools:
Install Rust via rustup:
Install the Terraform MCP bridge (the binary is commonly named tfmcp):
Cargo will download and compile crates during installation; when complete the tfmcp binary should be on your PATH.

Start the MCP bridge / server

Start the MCP server so it can accept MCP-style requests over stdio in this demo:
The server will run in the foreground and listen for JSON-RPC messages on stdin/stdout.

Create a sample Terraform project

Create a working directory and a minimal Terraform configuration that writes a local file. Create the directory and enter it:
Create main.tf with the following content:
Initialize and validate the workspace, then create a plan:
Sample (cleaned) plan output:

Interact with the Terraform MCP server via JSON-RPC (stdio)

While tfmcp mcp is running in one terminal, send JSON-RPC messages from another terminal using a heredoc. The example sequence below performs:
  1. An initialize request to the MCP server.
  2. A tools/call request for get_terraform_plan.
  3. A tools/call request for apply_terraform with auto_approve=true.
Send these requests via stdio:
For quick reference, common MCP tool calls used in this demo: Wrap any JSON examples containing curly braces in code blocks to avoid parsing issues (as done above).

Security policy and auto-approve

By default the MCP bridge may block automated apply operations for safety. If auto-approve is blocked, you will see an error such as:
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:

Successful apply and verification

When the apply completes, the MCP server will return the standard Terraform apply summary, for example:
Verify the created file:

Why use Terraform with MCP?

  • 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.

Troubleshooting tips

  • 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.

Summary

  • Installed the Terraform MCP bridge (tfmcp) and required toolchain.
  • Created a simple Terraform configuration that writes a local file.
  • Executed Terraform init/plan/apply through MCP JSON-RPC calls (stdio).
  • Addressed auto-approve security via the TFMCP_ALLOW_AUTO_APPROVE environment variable.
  • Verified the resulting resource and discussed reasons to use Terraform via MCP in automated systems.
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.

Watch Video