Terraform MCP 服务器
关于此服务器
HashiCorp 官方出品的 Terraform MCP 服务器。让 AI 智能体与 Terraform 生态无缝集成:管理状态文件、规划执行、编写基础设施即代码(IaC)。
安装方式
npx -y @hashicorp/terraform-mcp-server官方 README 安装指南(英文原文)
Usage with Visual Studio Code
Add the following JSON block to your User Settings (JSON) file in VS Code. You can do this by pressing Ctrl + Shift + P and typing Preferences: Open User Settings (JSON).
More about using MCP server tools in VS Code's agent mode documentation.
<table>
<tr><th>Version 0.3.0+ or greater</th><th>Version 0.2.3 or lower</th></tr>
<tr valign=top>
<td>
{
"mcp": {
"servers": {
"terraform": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e", "TFE_TOKEN=${input:tfe_token}",
"-e", "TFE_ADDRESS=${input:tfe_address}",
"hashicorp/terraform-mcp-server:1.1.0"
]
}
},
"inputs": [
{
"type": "promptString",
"id": "tfe_token",
"description": "Terraform API Token",
"password": true
},
{
"type": "promptString",
"id": "tfe_address",
"description": "Terraform Address",
"password": false
}
]
}
}</td>
<td>
{
"mcp": {
"servers": {
"terraform": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"hashicorp/terraform-mcp-server:0.2.3"
]
}
}
}
}</td>
</tr>
</table>
Optionally, you can add a similar example (i.e. without the mcp key) to a file called .vscode/mcp.json in your workspace. This will allow you to share the configuration with others.
<table>
<tr><th>Version 0.3.0+ or greater</th><th>Version 0.2.3 or lower</th></tr>
<tr valign=top>
<td>
{
"servers": {
"terraform": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e", "TFE_TOKEN=${input:tfe_token}",
"-e", "TFE_ADDRESS=${input:tfe_address}",
"hashicorp/terraform-mcp-server:1.1.0"
]
}
},
"inputs": [
{
"type": "promptString",
"id": "tfe_token",
"description": "Terraform API Token",
"password": true
},
{
"type": "promptString",
"id": "tfe_address",
"description": "Terraform Address",
"password": false
}
]
}</td>
<td>
{
"servers": {
"terraform": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"hashicorp/terraform-mcp-server:0.2.3"
]
}
}
}</td>
</tr>
</table>
Usage with Cursor
Add this to your Cursor config (~/.cursor/mcp.json) or via Settings → Cursor Settings → MCP:
<table>
<tr><th>Version 0.3.0+ or greater</th><th>Version 0.2.3 or lower</th></tr>
<tr valign=top>
<td>
{
"mcpServers": {
"terraform": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e", "TFE_ADDRESS=<<PASTE_TFE_ADDRESS_HERE>>",
"-e", "TFE_TOKEN=<<PASTE_TFE_TOKEN_HERE>>",
"hashicorp/terraform-mcp-server:1.1.0"
]
}
}
}</td>
<td>
{
"servers": {
"terraform": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"hashicorp/terraform-mcp-server:0.2.3"
]
}
}
}</td>
</tr>
</table>
<a href="cursor://anysphere.cursor-deeplink/mcp/install?name=terraform&config=eyJjb21tYW5kIjoiZG9ja2VyIiwiYXJncyI6WyJydW4iLCItaSIsIi0tcm0iLCJoYXNoaWNvcnAvdGVycmFmb3JtLW1jcC1zZXJ2ZXIiXX0%3D">
<img alt="Add terraform MCP server to Cursor" src="https://cursor.com/deeplink/mcp-install-dark.png" height="32" />
</a>
Usage with Claude Desktop / Amazon Q Developer / Kiro CLI
More about using MCP server tools in Claude Desktop user documentation. Read more about using MCP server in Amazon Q Developer and Kiro CLI.
<table>
<tr><th>Version 0.3.0+ or greater</th><th>Version 0.2.3 or lower</th></tr>
<tr valign=top>
<td>
{
"mcpServers": {
"terraform": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e", "TFE_ADDRESS=<<PASTE_TFE_ADDRESS_HERE>>",
"-e", "TFE_TOKEN=<<PASTE_TFE_TOKEN_HERE>>",
"hashicorp/terraform-mcp-server:1.1.0"
]
}
}
}</td>
<td>
{
"mcpServers": {
"terraform": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"hashicorp/terraform-mcp-server:0.2.3"
]
}
}
}</td>
</tr>
</table>
Usage with Claude Code
More about using and adding MCP server tools in Claude Code user documentation
- Local (
stdio) Transport
claude mcp add terraform -s user -t stdio -- docker run -i --rm hashicorp/terraform-mcp-server- Remote (
streamable-http) Transport
# Run server (example)
docker run -p 8080:8080 --rm -e TRANSPORT_MODE=streamable-http -e TRANSPORT_HOST=0.0.0.0 hashicorp/terraform-mcp-server
# Add to Claude Code
claude mcp add --transport http terraform http://localhost:8080/mcpUsage with Gemini extensions
For security, avoid hardcoding your credentials, create or update ~/.gemini/.env (where ~ is your home or project directory) for storing HCP Terraform or Terraform Enterprise credentials
# ~/.gemini/.env
TFE_ADDRESS=your_tfe_address_here
TFE_TOKEN=your_tfe_token_hereInstall the extension & run Gemini
gemini extensions install https://github.com/hashicorp/terraform-mcp-server
geminiUsage with Bob IDE / Shell
More about using and adding MCP servers tools in Bob IDE or Shell Using MCP in Bob.
<table>
<tr><th>Version 0.3.0+ or greater</th><th>Version 0.2.3 or lower</th></tr>
<tr valign=top>
<td>
{
"mcpServers": {
"terraform": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e", "TFE_ADDRESS=<<PASTE_TFE_ADDRESS_HERE>>",
"-e", "TFE_TOKEN=<<PASTE_TFE_TOKEN_HERE>>",
"hashicorp/terraform-mcp-server:1.1.0"
],
"disabled": false
}
}
}</td>
<td>
{
"mcpServers": {
"terraform": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"hashicorp/terraform-mcp-server:0.2.3"
],
"disabled": false
}
}
}</td>
</tr>
</table>
常见问题
Terraform MCP 能做什么?
让 AI 智能体直接编写和执行 Terraform 配置:管理状态文件、查看和创建执行计划、预览资源变更、读取 Terraform 配置和输出。让基础设施管理变得更加智能和高效。
Terraform MCP 需要安装 Terraform 吗?
是的,需要本地安装 Terraform CLI。MCP 服务器通过调用本地 terraform 命令来执行操作,因此需要先安装 Terraform 并配置好云提供商凭据。
Terraform MCP 免费吗?
完全免费开源(BUSL 协议),由 HashiCorp 官方维护。MCP 服务器本身免费,但使用 Terraform 管理云资源时,云服务商本身会按资源用量收费。