development
documentation
public
infrastructure
automation
What is Terraform MCP Server for seamless integration with Terraform Registry APIs.?
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. It supports dual transport modes, module search, and registry integration.
Documentation
Terraform MCP Server
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.
Features
Dual Transport Support: Both Stdio and StreamableHTTP transports
Terraform Provider Discovery: Query and explore Terraform providers and their documentation
Module Search & Analysis: Search and retrieve detailed information about Terraform modules
Registry Integration: Direct integration with Terraform Registry APIs
Container Ready: Docker support for easy deployment
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.
Security Note: When using the StreamableHTTP transport in production, always configure the MCP_ALLOWED_ORIGINS environment variable to restrict access to trusted origins only. This helps prevent DNS rebinding attacks and other cross-origin vulnerabilities.
Prerequisites
To run the server in a container, you will need to have Docker installed.
Once Docker is installed, you will need to ensure Docker is running.
Transport Support
The Terraform MCP Server supports multiple transport protocols:
1. Stdio Transport (Default)
Standard input/output communication using JSON-RPC messages. Ideal for local development and direct integration with MCP clients.
2. StreamableHTTP Transport
Modern HTTP-based transport supporting both direct HTTP requests and Server-Sent Events (SSE) streams. This is the recommended transport for remote/distributed setups.
Features:
Endpoint: http://{hostname}:8080/mcp
Health Check: http://{hostname}:8080/health
Environment Configuration: Set TRANSPORT_MODE=http or TRANSPORT_PORT=8080 to enable
Environment Variables:
Variable
Description
Default
TRANSPORT_MODE
Set to streamable-http to enable HTTP transport (legacy http value still supported)
The Terraform MCP Server supports two session modes when using the StreamableHTTP transport:
Stateful Mode (Default): Maintains session state between requests, enabling context-aware operations.
Stateless Mode: Each request is processed independently without maintaining session state, which can be useful for high-availability deployments or when using load balancers.
To enable stateless mode, set the environment variable:
export MCP_SESSION_MODE=stateless
Installation# Usage with VS 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).
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.
Queries the Terraform Registry to find and list available documentation for a specific provider using the specified service_slug. Returns a list of provider document IDs with their titles and categories for resources, data sources, functions, or guides.
providers
get_provider_details
Fetches the complete documentation content for a specific provider resource, data source, or function using a document ID obtained from the search_providers tool. Returns the raw documentation in markdown format.
modules
search_modules
Searches the Terraform Registry for modules based on specified module_query with pagination. Returns a list of module IDs with their names, descriptions, download counts, verification status, and publish dates
modules
get_module_details
Retrieves detailed documentation for a module using a module ID obtained from the search_modules tool including inputs, outputs, configuration, submodules, and examples.
policies
search_policies
Queries the Terraform Registry to find and list the appropriate Sentinel Policy based on the provided query policy_query. Returns a list of matching policies with terraform_policy_id(s) with their name, title and download counts.
policies
get_policy_details
Retrieves detailed documentation for a policy set using a terraform_policy_id obtained from the search_policies tool including policy readme and implementation details.
Resource Configuration# Available resources
Resource URI
Description
/terraform/style-guide
Terraform Style Guide - Provides access to the official Terraform style guide documentation in markdown format
/terraform/module-development
Terraform Module Development Guide - Comprehensive guide covering module composition, structure, providers, publishing, and refactoring best practices
Before using the server, you need to build the Docker image locally:
Clone the repository:
git clone https://github.com/hashicorp/terraform-mcp-server.git
cd terraform-mcp-server
Build the Docker image:
make docker-build
This will create a local Docker image that you can use in the following configuration.
docker run -i --rm terraform-mcp-server:dev
# Run in streamable-http mode
docker run -p 8080:8080 --rm -e TRANSPORT_MODE=streamable-http -e TRANSPORT_HOST=0.0.0.0 terraform-mcp-server:dev
Note: When running in Docker, you should set TRANSPORT_HOST=0.0.0.0 to allow connections from outside the container.