Terraform最佳实践MCP服务器
注意: 这是一个社区维护的MCP服务器。它不是来自HashiCorp或任何云提供商的官方模型上下文协议服务器。
模型上下文协议(MCP)服务器,用于通过CLI分析生成更好的Terraform (tflint, checkov, trivy, kics, infracost),最佳实践指南 terraform-best-practices.com、云提供商推荐(Azure、AWS、GCP)以及Terraform Registry资源和模块指导。
概述
该服务器在CLI分析、最佳实践检索和交互式报告工作流中提供18个MCP工具:
run_tflint来检测Terraform代码。run_checkov运行安全性和遵从性扫描。run_trivy扫描Terraform配置中的漏洞。run_kics运行IaC安全和合规扫描。run_infracost估算云计算成本。fetch_terraform_best_practices检索精心策划的Terraform最佳实践。fetch_provider_best_practices检索云提供商Terraform指南。fetch_terraform_registry_guidance检索Terraform Registry资源和模块指南。analyze_terraform_code分析代码结构、模块化和最佳实践。analyze_terraform_performance识别运行时和性能瓶颈。analyze_state_management评估后端状态策略和协作安全性。generate_cost_report生成成本分析和优化建议。generate_terraform_module_docs从Terraform代码生成模块文档。recommend_terraform_modules从检测到的模式中推荐可重用模块。suggest_terraform_architecture提出多环境、多区域模式。suggest_terraform_testing_strategy根据风险概况推荐ci驱动的测试。suggest_security_hardening提供安全加固建议。generate_compliance_summary从扫描输出创建遵从性报告。
平台的兼容性
该MCP服务器与平台无关,可在本地和CI环境中工作:
- 地方发展
- GitHub的行为
- GitLab CI / CD
- Azure DevOps
- CircleCI, Jenkins和Bitbucket管道
- 人工智能代理和副驾驶工作流程
主要需求是Node.js和可选的CLI工具 PATH. 如果缺少命令行,服务器将返回安装指导,而不是静默失败。
工具矩阵
| 工具 | 类别 | 典型的结果 |
|---|---|---|
run_tflint | CLI分析 | 对Terraform代码的Lint发现 |
run_checkov | CLI分析 | 安全性和遵从性扫描结果 |
run_trivy | CLI分析 | 针对IaC的漏洞扫描结果 |
run_kics | CLI分析 | IaC安全调查结果 |
run_infracost | CLI分析 | 当前IaC的每月费用基准 |
fetch_terraform_best_practices | 指导 | 来自terraform-bestpractices.com的最佳实践清单 |
fetch_provider_best_practices | 指导 | 云提供商(Azure/AWS/GCP) Terraform推荐 |
fetch_terraform_registry_guidance | 指导 | 提供程序、资源和模块的注册指导 |
analyze_terraform_code | 分析 | 代码结构和模块化评估 |
analyze_terraform_performance | 分析 | 性能瓶颈和优化报告 |
analyze_state_management | 分析 | 状态后端策略和协作安全审查 |
generate_cost_report | 报告 | 成本分析及优化建议 |
generate_terraform_module_docs | 报告 | 生成的模块文档标记 |
recommend_terraform_modules | 报告 | 来自代码模式的可重用模块建议 |
suggest_terraform_architecture | 体系结构 | 多环境和多区域架构模式 |
suggest_terraform_testing_strategy | 测试 | 基于风险概况的ci驱动测试策略 |
suggest_security_hardening | 安全 | 来自扫描输出的安全加固建议 |
generate_compliance_summary | 合规 | 来自扫描输出的遵从性报告 |
工具
run_tflint
对Terraform项目目录运行tflint。
输入:
path(字符串,可选):要扫描的地形项目路径。默认值:..extraArgs(string[],可选):额外的CLI参数。timeoutMs(integer,可选):命令超时时间,单位为毫秒。
run_checkov
在Terraform目录上运行checkov。
输入:
path(字符串,可选):要扫描的地形项目路径。默认值:..extraArgs(string[],可选):额外的CLI参数。timeoutMs(integer,可选):命令超时时间,单位为毫秒。
run_trivy
对Terraform代码运行琐碎配置扫描。
输入:
path(字符串,可选):要扫描的地形项目路径。默认值:..extraArgs(string[],可选):额外的CLI参数。timeoutMs(integer,可选):命令超时时间,单位为毫秒。
run_kics
对地球代码运行kics IaC扫描。
输入:
path(字符串,可选):要扫描的地形项目路径。默认值:..extraArgs(string[],可选):额外的CLI参数。timeoutMs(integer,可选):命令超时时间,单位为毫秒。
run_infracost
运行Terraform目录的infracost breakdown。
输入:
path(字符串,可选):要扫描的地形项目路径。默认值:..extraArgs(string[],可选):额外的CLI参数。timeoutMs(integer,可选):命令超时时间,单位为毫秒。
fetch_terraform_best_practices
从策划检查和terraform-bestpractices.com获取Terraform最佳实践指导和可选的实时摘要。
输入:
topic(字符串,可选):主题过滤器,如状态、模块、安全性或命名。liveFetch(boolean,可选):当为true时,获取并汇总实时内容。默认值:true.
fetch_provider_best_practices
从策划检查和可选的实时提供商文档摘要中获取针对Azure、AWS或GCP的Terraform最佳实践指南。
输入:
provider(字符串,必需):云提供商(azure,aws,或者gcp)。topic(字符串,可选):关注区域,如状态、IAM、模块、网络或成本。liveFetch(布尔值,可选):当为true时,获取并汇总链接的提供商指导页面。默认值:true.
fetch_terraform_registry_guidance
获取提供程序、资源和模块的Terraform Registry指南。
输入:
provider(字符串,可选):提供商名称(例如:aws,azurerm,google)。resource(字符串,可选):与提供者一起使用的资源类型(例如:s3_bucket,resource_group)。module(string,可选):表单中的模块路径namespace/name/provider.topic(字符串,可选):摘要输出的主题过滤器。liveFetch(布尔值,可选):当为true时,获取并汇总所选的注册表页面。默认值:true.
analyze_terraform_code
分析Terraform代码结构、模块化和最佳实践。
输入:
code(string, required):要分析的Terraform代码片段(最多50,000个字符)。focusArea(字符串,可选):分析焦点区域(modularity,variables,outputs,locals,general). 默认值:general.
analyze_terraform_performance
分析Terraform运行时和性能瓶颈以及优化机会。
输入:
terraformCode(字符串,必选):用于以性能为重点的启发式的地形代码。stateSizeMb(数字,可选):以MB为单位的状态大小,用于支持规模的建议。workspaceCount(整数,可选):共享同一根堆栈的工作区数量。providerRateLimitSensitive(boolean,可选):当为true时,强调提供程序API节流保护。默认值:true.
analyze_state_management
评估后端状态策略和协作安全性。
输入:
terraformCode(字符串,必选):包括后端和状态相关配置的地形代码。teamSize(整数,可选):应用Terraform变更的工程师数量。默认值:6.environmentCount(整数,可选):资产管理的环境数量。默认值:2.currentBackend(string,可选):正在使用的后端状态(auto,s3,azurerm,gcs,remote,local,unknown). 默认值:auto.useWorkspaces(boolean,可选):是否通过工作区管理多个环境。默认值:false.
generate_cost_report
根据Infracost输出的优化建议进行成本分析。
输入:
infracostJson(字符串,必选):来自infracost分解或diff命令的JSON输出。includeOptimizations(boolean,可选):当为true时,生成成本优化建议。默认值:true.
generate_terraform_module_docs
从Terraform代码生成模块文档标记。
输入:
terraformCode(字符串,必选):将模块代码转化为文档。moduleName(字符串,可选):生成的降价文档中使用的友好名称。默认值:terraform-module.includeUsageExample(boolean,可选):包含使用示例部分。默认值:true.includeInputsOutputsTables(boolean,可选):将输入和输出呈现为标记表。默认值:true.
recommend_terraform_modules
从检测到的代码模式中推荐可重用的Terraform模块。
输入:
terraformCode(string, required):用于推断模块推荐的Terraform代码。provider(字符串,可选):首选云提供商(aws,azure,gcp,any). 默认值:any.deploymentIntent(字符串,可选):主要部署目标(networking,kubernetes,serverless,storage,database,observability,security,general). 默认值:general.maxRecommendations(整数,可选):最大建议返回(1—10)。默认值:5.
suggest_terraform_architecture
建议多环境、多区域的建筑模式。
输入:
workloadType(字符串,可选):主工作负载配置文件(web-api,data-platform,event-driven,platform-foundation,general). 默认值:general.environments(string[],可选):目标环境(dev,test,stage,prod,sandbox,dr). 默认值:["dev", "prod"].multiRegion(boolean,可选):工作负载是否跨多个区域运行。默认值:false.complianceProfile(字符串,可选):合规配置文件(none,cis,pci-dss,hipaa,sox). 默认值:none.teamSize(整数,可选):操作Terraform代码的工程师数量。默认值:6.currentPainPoints(字符串,可选):当前架构中的痛点或约束。includeReferenceLayout(boolean,可选):包含建议的存储库和文件夹布局。默认值:true.
suggest_terraform_testing_strategy
根据风险概况推荐ci驱动的Terraform测试策略。
输入:
terraformCode(字符串,可选):用于复杂性感知建议的Terraform代码。deploymentCriticality(字符串,可选):业务影响级别(low,medium,high,mission-critical). 默认值:medium.changeFrequency(字符串,可选):引入基础设施更改的频率(low,medium,high). 默认值:medium.ciSystem(字符串,可选):目标CI系统(github-actions,azure-devops,gitlab,circleci,jenkins,other). 默认值:github-actions.includeExamplePipeline(boolean,可选):包含一个示例管道序列。默认值:true.
suggest_security_hardening
根据扫描输出提供安全加固建议。
输入:
scanOutput(字符串,必选):来自安全扫描工具(checkov、trivy或kics)的输出。scanTool(字符串,可选):扫描工具的名称(checkov,trivy,kics). 默认值:checkov.
generate_compliance_summary
根据扫描输出创建遵从性报告。
输入:
checkovOutput(string,可选):checkov扫描的输出。trivyOutput(string,可选):trivy scan的输出。kicsOutput(字符串,可选):kics扫描的输出。complianceFramework(字符串,可选):要评估的遵从性框架(cis,pci-dss,hipaa,sox,general). 默认值:general.
提示食谱
使用这些最小的提示在副驾驶聊天与 @tf-best-practices. 命令行工具默认为当前工作区的根目录 path 提供。添加 #file 或者 #folder 上下文可以提高对聊天的理解,但是工具执行路径是由显式的工具输入控制的(例如, path)。
快速入门(零参数提示)
@tf-best-practices run run_tflint
@tf-best-practices run run_checkov
@tf-best-practices run run_trivy
@tf-best-practices run run_kics
@tf-best-practices run run_infracost
@tf-best-practices run fetch_terraform_best_practices
CLI分析
@tf-best-practices run run_tflint with path ./modules/network
@tf-best-practices run run_checkov with path ./environments/prod
@tf-best-practices run run_trivy with path .
@tf-best-practices run run_kics with path .
@tf-best-practices run run_infracost with path .
最佳做法及指引
@tf-best-practices run fetch_terraform_best_practices with topic modules
@tf-best-practices run fetch_provider_best_practices with provider azure
@tf-best-practices run fetch_terraform_registry_guidance with provider azurerm and resource resource_group
代码分析
@tf-best-practices run analyze_terraform_code with code <paste code> and focusArea modularity
@tf-best-practices run analyze_terraform_performance with terraformCode <paste code>
@tf-best-practices run analyze_state_management with terraformCode <paste code>
报告及建议
@tf-best-practices run generate_cost_report with infracostJson <paste json>
@tf-best-practices run generate_terraform_module_docs with terraformCode <paste code>
@tf-best-practices run recommend_terraform_modules with terraformCode <paste code> and provider azure
@tf-best-practices run suggest_terraform_architecture with workloadType web-api and environments ["dev","stage","prod"]
@tf-best-practices run suggest_terraform_testing_strategy with ciSystem github-actions
@tf-best-practices run suggest_security_hardening with scanOutput <paste output> and scanTool checkov
@tf-best-practices run generate_compliance_summary with complianceFramework cis
依赖关系
系统依赖关系
- node . js
>=24.14.0 - npm(绑定在Node.js中)
- 可选的cli在
PATH对于命令工具:tflintcheckovtrivykicsinfracost
如果缺少命令行,服务器将返回安装指导,而不是静默失败。
npm依赖性
运行时:
@modelcontextprotocol/sdk(MCP服务器SDK)zod(输入模式验证)
发展:
typescript(建立/编译)tsx(开发跑步者)vitest(单元测试运行器)@types/node(node . js打字)
环境变量
INFRACOST_API_KEY:用于云支持成本估算的Infracost API密钥。
安装
npm install
npm run build
配置
使用VS Code副驾驶聊天
此工作空间在 .vscode/mcp.json:
{
"servers": {
"tf-best-practices": {
"type": "stdio",
"command": "npm",
"args": ["run", "dev"]
}
}
}
重新加载VS Code (Cmd+Shift+P -> Developer: Reload Window)。
然后从Copilot Chat中查询服务器 @tf-best-practices,例如:
@tf-best-practices run run_tflint
@tf-best-practices run fetch_terraform_best_practices with topic modules
与其他MCP客户端的使用
使用内置入口点的演播室传输:
{
"name": "terraform-best-practices-mcp-server",
"type": "stdio",
"command": "node",
"args": ["/absolute/path/to/terraform-best-practices-mcp/dist/index.js"]
}
首先使用 npm run build,然后启动MCP客户端。
运行
npm start
发展模式:
npm run dev
调试
使用 .vscode/launch.json:
Debug MCP Server(运行npm run dev)Debug MCP Server (Built)(运行dist/index.js在构建)
中设置断点 src/index.ts,然后按 F5.
测试
看到 文档/ TESTING.md 用于副驾驶聊天场景,手动JSON-RPC检查和故障排除指导。
在副驾驶聊天快速验证提示:
@tf-best-practices run run_tflint
交互式VS Code工作流
1. 安全分类
- 运行扫描:
@tf-best-practices run run_checkov
- 请求加固建议:
@tf-best-practices run suggest_security_hardening with scanOutput <paste checkov output> and scanTool checkov
- 生成合规性摘要:
@tf-best-practices run generate_compliance_summary with checkovOutput <paste output> and complianceFramework cis
2. 成本影响检讨
- 生成成本基准:
@tf-best-practices run run_infracost
- 制作成本报告:
@tf-best-practices run generate_cost_report with infracostJson <paste json>
3. 架构评审
- 分析代码质量:
@tf-best-practices run analyze_terraform_code with code <paste code> and focusArea modularity
- 评审状态管理:
@tf-best-practices run analyze_state_management with terraformCode <paste code>
- 获取架构建议:
@tf-best-practices run suggest_terraform_architecture with workloadType web-api and multiRegion true
最佳实践
- 在使用指导工具之前,从CLI分析工具开始建立基线。
- 使用
fetch_terraform_best_practices与社区惯例保持一致。 - 使用
fetch_provider_best_practices对于特定于提供程序的模式。 - 保持
timeoutMs对于大型Terraform项目来说足够高。 - 运行
suggest_terraform_testing_strategy当加入新的CI/CD管道时。 - 使用
generate_compliance_summary定期审核准备。
其他用例
- 拉取请求中的预合并安全性和遵从性门。
- 基础设施变更的自动化成本影响审查。
- 针对新建项目的体系结构模式建议。
- 为共享Terraform模块生成模块文档。
- 通过部署风险概要定义ci驱动的测试策略。
- 审计跟踪的合规性证据生成。
相关项目
起程拓殖资源
模型上下文协议
社区与贡献
归属与许可
由卡尔·道森在 在鼹鼠洞的底部 组织。
开发
npm run lint
npm test
npm run build
许可证
在麻省理工学院许可下许可。
