tf-dialect

tf-dialect

Exposes your organization's Terraform style guide to AI coding agents, enabling them to generate, validate, and ensure Infrastructure as Code follows your specific conventions, naming standards, security defaults, and best practices.

Category
访问服务器

README

tf-dialect

tf-dialect is an MCP (Model Context Protocol) server that exposes your organization's Terraform style guide to AI coding agents, ensuring they generate context-aware, organization-specific Infrastructure as Code instead of generic HCL.

Configure once, use with any MCP-capable coding agent (Claude Desktop, Cline, etc.).

Quick Start

# Clone the repository
git clone https://github.com/utpaljaynadiger/tf-dialect.git
cd tf-dialect

# Install dependencies
npm install

# Build the project
npm run build

# Create your style configuration
cp terraform-style.example.yaml terraform-style.yaml

# Edit terraform-style.yaml with your organization's standards
# Then configure in your MCP client (see "Running the Server" section)

Why tf-dialect?

The Problem

AI coding assistants generate generic Terraform code that violates your org's standards. Your existing tools (tflint, Sentinel, module registries) are reactive—they catch violations after code is written. Developers waste cycles fixing preventable issues.

The Solution

tf-dialect exposes your Terraform standards to AI agents via MCP before code generation. AI learns your naming conventions, required tags, approved modules, and security defaults, then generates compliant code on first try.

Before/After

Without tf-dialect:

# AI generates generic code
resource "aws_s3_bucket" "logs" {
  bucket = "my-logs-bucket"
}
# ❌ Wrong naming, missing tags, no encryption, not using approved module
# → 3 commits to fix tflint/Sentinel violations

With tf-dialect:

# AI calls get_style_guide() + list_examples() first
module "logs_bucket" {
  source = "../modules/s3-bucket"
  
  name = "acme-prod-logs"
  kms_key_id = data.aws_kms_key.standard.arn
  
  tags = {
    CostCenter  = "engineering"
    Team        = "platform"
    Environment = "prod"
  }
}
# ✅ Passes all checks on first commit

Positioning

Tool Phase Purpose
tf-dialect Pre-generation Teach AI your standards
Module Registry Reference Provide reusable modules
tflint/checkov Post-generation Static analysis
Sentinel/OPA Runtime Policy enforcement

tf-dialect is complementary—it makes AI agents aware of your module registry and helps generate code that passes your existing validation tools.

Target Users

  • Platform teams: Standardizing AI-generated IaC across your org
  • Developers: Using Claude/Copilot/ChatGPT for Terraform
  • Organizations: With existing Terraform standards that AI doesn't know about

Features

  • 📚 Style Guide Management: Define your Terraform conventions in a single YAML file
  • 🔍 Validation: Check Terraform snippets against your organization's rules
  • 📝 Code Examples: Provide reusable snippets for common patterns
  • 🛡️ Security Defaults: Enforce security best practices automatically
  • 🏗️ Code Generation: Generate compliant Terraform resources
  • 🤖 AI-Native: Works seamlessly with MCP-capable coding agents

Installation

npm install
npm run build

Configuration

  1. Copy the example config:
cp terraform-style.example.yaml terraform-style.yaml
  1. Edit terraform-style.yaml to match your organization's standards:
modules:
  pattern: "root + shared-modules"
  shared_module_path: "modules/"
  prefer_shared_modules: true

naming:
  resource_format: "<project>-<env>-<component>-<extra?>"
  variable_case: "snake_case"
  output_case: "snake_case"

tagging:
  required_tags:
    - "environment"
    - "owner"
    - "cost_center"
  defaults:
    environment: "${var.environment}"
    owner: "infra-team"

security_defaults:
  s3_bucket:
    block_public_acls: true
    versioning: true
    encryption: "aws:kms"
  rds:
    storage_encrypted: true
    backup_retention_period: 7

examples:
  s3_private_bucket: |
    module "logs_bucket" {
      source = "../modules/s3-bucket"
      name   = "${local.project}-${var.environment}-logs"
      tags   = local.default_tags
    }

Running the Server

Standalone

npm run mcp

With Claude Desktop

Add to your Claude Desktop config (~/Library/Application Support/Claude/claude_desktop_config.json on macOS):

{
  "mcpServers": {
    "tf-dialect": {
      "command": "node",
      "args": ["/absolute/path/to/tf-dialect/dist/index.js"],
      "env": {
        "TERRAFORM_STYLE_PATH": "/absolute/path/to/your/terraform-style.yaml"
      }
    }
  }
}

Or if terraform-style.yaml is in the same directory as the server:

{
  "mcpServers": {
    "tf-dialect": {
      "command": "node",
      "args": ["/absolute/path/to/tf-dialect/dist/index.js"]
    }
  }
}

With Cline VSCode Extension

Add to your MCP settings:

{
  "mcpServers": {
    "tf-dialect": {
      "command": "node",
      "args": ["/absolute/path/to/tf-dialect/dist/index.js"]
    }
  }
}

MCP Tools

The server exposes four tools that AI agents can use:

1. get_style_guide

Get the complete Terraform style guide configuration.

Input: None

Output:

{
  "modules": { ... },
  "naming": { ... },
  "tagging": { ... },
  "providers": { ... },
  "security_defaults": { ... },
  "examples": { ... }
}

Example agent prompt:

"Show me the Terraform style guide for this project"


2. list_examples

List code examples, optionally filtered by resource type or search term.

Input:

{
  "resourceType": "s3_bucket",  // optional
  "search": "postgres"          // optional
}

Output:

{
  "examples": [
    {
      "name": "s3_private_bucket",
      "code": "module \"logs_bucket\" { ... }"
    }
  ]
}

Example agent prompts:

"Show me examples of S3 buckets" "List all RDS examples"


3. validate_snippet

Validate Terraform code against the style guide.

Input:

{
  "code": "resource \"aws_s3_bucket\" \"example\" { ... }",
  "filePath": "main.tf"  // optional
}

Output:

{
  "valid": false,
  "violations": [
    {
      "ruleId": "required_tag_missing",
      "severity": "error",
      "message": "Missing required tags: environment, owner",
      "line": 5,
      "suggestion": "Add the following tags: environment = \"...\", owner = \"...\""
    }
  ]
}

Example agent prompts:

"Validate this Terraform code against our style guide" "Check if this S3 bucket configuration is compliant"


4. generate_resource

Generate a Terraform resource following organization standards.

Input:

{
  "resourceType": "aws_s3_bucket",
  "env": "prod",
  "service": "analytics",
  "purpose": "logs",       // optional
  "extraTags": {           // optional
    "team": "data"
  }
}

Output:

{
  "code": "resource \"aws_s3_bucket\" \"this\" { ... }"
}

Supported resource types:

  • aws_s3_bucket
  • aws_db_instance
  • Others (generates generic stub with TODOs)

Example agent prompts:

"Generate an S3 bucket for prod analytics logs" "Create an RDS instance for the staging API database"


Validation Rules

tf-dialect enforces the following rules:

Required Tags

Ensures all resources include required tags defined in your config.

Forbidden Patterns

Blocks dangerous patterns like:

  • 0.0.0.0/0 in security groups
  • Hardcoded credentials
  • Custom regex patterns you define

Security Defaults

Enforces security best practices:

S3 Buckets:

  • Block public access
  • Enable versioning
  • Enable encryption (KMS or AES256)

RDS Instances:

  • Enable storage encryption
  • Set backup retention period
  • Other configurable defaults

Naming Conventions

Validates resource names follow your format:

  • <project>-<env>-<component>-<extra?>
  • Checks component count and structure

Development

# Install dependencies
npm install

# Build
npm run build

# Watch mode
npm run dev

Example Workflow

  1. Agent asks about style:

    • Agent calls get_style_guide
    • Learns your organization's conventions
  2. Agent needs an example:

    • Agent calls list_examples with resourceType: "rds"
    • Gets working RDS configuration examples
  3. Agent generates code:

    • Agent calls generate_resource or writes code
    • Then calls validate_snippet to check compliance
  4. Agent fixes violations:

    • Reads violation suggestions
    • Updates code to be compliant

Use Cases

  • Onboarding: New team members' AI assistants learn your standards instantly
  • Consistency: All Terraform code follows the same patterns across teams
  • Security: Enforce security defaults automatically in generated code
  • Productivity: AI generates compliant code on first try, not generic HCL

License

MIT

Contributing

Contributions welcome! This is an OSS-friendly project designed for IaC power users.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选