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:
tfmcp):
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: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:main.tf with the following content:
Interact with the Terraform MCP server via JSON-RPC (stdio)
Whiletfmcp mcp is running in one terminal, send JSON-RPC messages from another terminal using a heredoc. The example sequence below performs:
- An
initializerequest to the MCP server. - A
tools/callrequest forget_terraform_plan. - A
tools/callrequest forapply_terraformwithauto_approve=true.
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.Successful apply and verification
When the apply completes, the MCP server will return the standard Terraform apply summary, for example: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
tfmcpis on your PATH aftercargo install(check~/.cargo/bin). - If
terraform initfails, review provider version constraints and network access to provider registries. - If JSON-RPC messages are not being processed, confirm
tfmcp mcpis running in the terminal where you expect it and that your heredoc is directed at the sametfmcpinstance.
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_APPROVEenvironment variable. - Verified the resulting resource and discussed reasons to use Terraform via MCP in automated systems.
Links and references
- Terraform
- JSON-RPC specification
- Rustup (install Rust)
- Terraform local provider: https://registry.terraform.io/providers/hashicorp/local
tfmcp via stdio.