Terraform 管理的 MCP 服务器
Terraform MCP Server

原始链接: https://github.com/hashicorp/terraform-mcp-server

Terraform MCP服务器通过与Terraform Registry API无缝集成,促进了高级基础设施即代码(IaC)自动化。它使用户能够自动化提供程序和模块的发现,提取和分析注册表数据,检索提供程序资源和模块的详细信息。 建议使用Docker运行该服务器。您可以配置VS Code或Claude Desktop在Docker容器内运行服务器。如果无法使用Docker,也可以直接从源代码构建二进制文件。 MCP服务器提供`resolveProviderDocID`、`getProviderDocs`、`searchModules`和`moduleDetails`等工具。这些工具分别使⽤户能够查找提供程序文档,获取特定的文档内容,搜索模块以及检索详细的模块信息。 请务必仔细审查MCP服务器生成的所有输出和建议,以确保在实施之前符合安全最佳实践、成本效益目标和合规性要求。

Hacker News 上的一篇帖子讨论了 HashiCorp 新推出的 Terraform MCP 服务器,这是一个旨在增强 AI 辅助 HCL(Terraform 代码)生成的工具。用户正在探索其改进工作流程的潜力,特别是利用 lockfile 中的 provider 文档来提高准确性。 一位用户询问其与 Terragrunt(Terraform 的一个包装器)的兼容性,另一位用户建议,鉴于 Terragrunt 直接与 Terraform 交互,它应该可以通过适当的提示来工作。 另一种观点则表达了对 RUM(实时用户监控)和 HashiCorp 账单增加的担忧。有人回应说,该工具目前采用 MPL-2 许可(开源),似乎并没有直接与 HashiCorp 的集中式服务绑定。他们还指出,该服务器实际上是为大型语言模型提供 Terraform provider 文档,而这部分功能已经可以通过 `tofu provider schema -json` 命令实现。他们建议改进 provider 二进制文件本身的描述字段作为替代方案。

原文

The Terraform MCP Server is a Model Context Protocol (MCP) server that provides seamless integration with Terraform Registry APIs, enabling advanced automation and interaction capabilities for Infrastructure as Code (IaC) development.

  • Automating Terraform provider and module discovery
  • Extracting and analyzing data from Terraform Registry
  • Getting detailed information about provider resources and data sources
  • Exploring and understanding Terraform modules

Caution: The outputs and recommendations provided by the MCP server are generated dynamically and may vary based on the query, model, and the connected MCP server. Users should thoroughly review all outputs/recommendations to ensure they align with their organization's security best practices, cost-efficiency goals, and compliance requirements before implementation.

  1. To run the server in a container, you will need to have Docker installed.
  2. Once Docker is installed, you will need to ensure Docker is running.

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.

{
  "mcp": {
    "servers": {
      "terraform": {
        "command": "docker",
        "args": [
          "run",
          "-i",
          "--rm",
          "hashicorp/terraform-mcp-server"
        ]
      }
    }
  }
}

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.

{
  "servers": {
    "terraform": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "hashicorp/terraform-mcp-server"
      ]
    }
  }
}

More about using MCP server tools in Claude Desktop user documentation.

Usage with Claude Desktop

{
  "mcpServers": {
    "terraform": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "hashicorp/terraform-mcp-server"
      ]
    }
  }
}

The following sets of tools are available:

Toolset Tool Description
providers resolveProviderDocID Queries the Terraform Registry to find and list available documentation for a specific provider using the specified serviceSlug. Returns a list of provider document IDs with their titles and categories for resources, data sources, functions, or guides.
providers getProviderDocs Fetches the complete documentation content for a specific provider resource, data source, or function using a document ID obtained from the resolveProviderDocID tool. Returns the raw documentation in markdown format.
modules searchModules Searches the Terraform Registry for modules based on specified moduleQuery with pagination. Returns a list of module IDs with their names, descriptions, download counts, verification status, and publish dates
modules moduleDetails Retrieves detailed documentation for a module using a module ID obtained from the searchModules tool including inputs, outputs, configuration, submodules, and examples.

If you don't have Docker, you can use make build to build the binary directly from source code. You should configure your server to use the built executable as its command.

  1. Clone the repository:
git clone https://github.com/hashicorp/terraform-mcp-server.git
cd terraform-mcp-server
  1. Build the binary:
{
  "mcp": {
    "servers": {
      "terraform": {
        "command": "/path/to/terraform-mcp-server",
        "args": ["stdio"]
      }
    }
  }
}

Building the Docker Image locally

Before using the server, you need to build the Docker image locally:

  1. Clone the repository:
git clone https://github.com/hashicorp/terraform-mcp-server.git
cd terraform-mcp-server
  1. Build the Docker image:

This will create a local Docker image that you can use in the following configuration.

{
  "servers": {
    "terraform": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "terraform-mcp-server"
      ]
    }
  }
}
  • Go (check go.mod file for specific version)
  • Docker (optional, for container builds)
# Run all tests
make test

# Run e2e tests
make test-e2e
make build        # Build the binary
make test         # Run all tests
make test-e2e     # Run end-to-end tests
make clean        # Remove build artifacts
make deps         # Download dependencies
make docker-build # Build docker image
  1. Fork the repository
  2. Create your feature branch
  3. Make your changes
  4. Run tests
  5. Submit a pull request

This project is licensed under the terms of the MPL-2.0 open source license. Please refer to LICENSE file for the full terms.

For security issues, please contact [email protected] or follow our security policy.

For bug reports and feature requests, please open an issue on GitHub.

For general questions and discussions, open a GitHub Discussion.

联系我们 contact @ memedata.com