tfmcp 0.2.2

Terraform Model Context Protocol Tool - A CLI tool to manage Terraform through MCP
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use crate::prompts::builder::{ToolDescription, ToolExample};
use serde_json::json;

enum ToolDescriptionKind {
    TerraformPlan,
    TerraformApply,
    TerraformValidate,
    TerraformValidateDetailed,
    TerraformDestroy,
    TerraformAnalyze,
    ListResources,
    GetState,
    InitTerraform,
    SetDirectory,
    SecurityStatus,
}

struct DescriptionSpec {
    summary: &'static str,
    usage_guide: &'static str,
    constraints: Vec<&'static str>,
    error_hints: Vec<(&'static str, &'static str)>,
    security_notes: Vec<&'static str>,
    examples: Vec<ToolExample>,
}

/// Create tool description for terraform plan operation
pub fn create_terraform_plan_description() -> ToolDescription {
    create_description(ToolDescriptionKind::TerraformPlan)
}

/// Create tool description for terraform apply operation
pub fn create_terraform_apply_description() -> ToolDescription {
    create_description(ToolDescriptionKind::TerraformApply)
}

/// Create tool description for terraform validate operation
pub fn create_terraform_validate_description() -> ToolDescription {
    create_description(ToolDescriptionKind::TerraformValidate)
}

/// Create tool description for terraform validate detailed operation
pub fn create_terraform_validate_detailed_description() -> ToolDescription {
    create_description(ToolDescriptionKind::TerraformValidateDetailed)
}

/// Create tool description for terraform destroy operation
pub fn create_terraform_destroy_description() -> ToolDescription {
    create_description(ToolDescriptionKind::TerraformDestroy)
}

/// Create tool description for analyzing terraform configurations
pub fn create_terraform_analyze_description() -> ToolDescription {
    create_description(ToolDescriptionKind::TerraformAnalyze)
}

/// Create tool description for listing terraform resources
pub fn create_list_resources_description() -> ToolDescription {
    create_description(ToolDescriptionKind::ListResources)
}

/// Create tool description for getting terraform state
pub fn create_get_state_description() -> ToolDescription {
    create_description(ToolDescriptionKind::GetState)
}

/// Create tool description for initializing terraform
pub fn create_init_terraform_description() -> ToolDescription {
    create_description(ToolDescriptionKind::InitTerraform)
}

/// Create tool description for setting terraform directory
pub fn create_set_directory_description() -> ToolDescription {
    create_description(ToolDescriptionKind::SetDirectory)
}

/// Create tool description for getting security status
pub fn create_security_status_description() -> ToolDescription {
    create_description(ToolDescriptionKind::SecurityStatus)
}

fn create_description(kind: ToolDescriptionKind) -> ToolDescription {
    build_description(description_spec(kind))
}

fn build_description(spec: DescriptionSpec) -> ToolDescription {
    let mut description = ToolDescription::new(spec.summary).with_usage_guide(spec.usage_guide);

    for constraint in spec.constraints {
        description = description.with_constraint(constraint);
    }

    for (error, hint) in spec.error_hints {
        description = description.with_error_hint(error, hint);
    }

    for note in spec.security_notes {
        description = description.with_security_note(note);
    }

    for example in spec.examples {
        description = description.with_example(example);
    }

    description
}

fn description_spec(kind: ToolDescriptionKind) -> DescriptionSpec {
    match kind {
        ToolDescriptionKind::TerraformPlan => DescriptionSpec {
            summary: "Execute 'terraform plan' to show changes that would be made to infrastructure",
            usage_guide: "This tool generates an execution plan showing what Terraform will do when you apply \
                the configuration. It's a safe operation that doesn't make any changes to real infrastructure.",
            constraints: vec![
                "Terraform must be initialized in the target directory",
                "Valid Terraform configuration files must exist",
            ],
            error_hints: vec![
                (
                    "Init Required",
                    "Run terraform init first to initialize the working directory",
                ),
                (
                    "No Configuration",
                    "Ensure .tf files exist in the project directory",
                ),
            ],
            security_notes: vec![
                "This is a read-only operation - no infrastructure changes are made",
            ],
            examples: vec![ToolExample {
                title: "Basic Plan Generation".to_string(),
                description: "Generate a plan for the current Terraform configuration".to_string(),
                input: json!({}),
                expected_output:
                    "JSON-formatted plan showing resources to be created, modified, or destroyed"
                        .to_string(),
            }],
        },
        ToolDescriptionKind::TerraformApply => DescriptionSpec {
            summary: "Apply Terraform configuration to create, update, or delete infrastructure resources",
            usage_guide: "This tool executes the changes shown in a terraform plan. It will modify real infrastructure \
                according to your configuration. Always review the plan before applying changes.",
            constraints: vec![
                "TFMCP_ALLOW_DANGEROUS_OPS must be set to true",
                "Terraform must be initialized",
                "Valid configuration files must exist",
            ],
            error_hints: vec![
                (
                    "Permission Denied",
                    "Set TFMCP_ALLOW_DANGEROUS_OPS=true to enable apply operations",
                ),
                ("Init Required", "Run terraform init first"),
                (
                    "Auto-approve Blocked",
                    "Set TFMCP_ALLOW_AUTO_APPROVE=true for auto-approval",
                ),
            ],
            security_notes: vec![
                "This operation modifies real infrastructure - use with caution",
                "All apply operations are logged for audit purposes",
                "Production directory patterns are automatically blocked",
            ],
            examples: vec![
                ToolExample {
                    title: "Apply with Manual Approval".to_string(),
                    description: "Apply changes with interactive approval".to_string(),
                    input: json!({"auto_approve": false}),
                    expected_output: "Terraform apply output showing resources created/modified"
                        .to_string(),
                },
                ToolExample {
                    title: "Auto-approved Apply".to_string(),
                    description: "Apply changes automatically without manual confirmation"
                        .to_string(),
                    input: json!({"auto_approve": true}),
                    expected_output: "Terraform apply output with automatic approval".to_string(),
                },
            ],
        },
        ToolDescriptionKind::TerraformValidate => DescriptionSpec {
            summary: "Validate Terraform configuration files for syntax and semantic correctness",
            usage_guide: "This tool checks your Terraform configuration for syntax errors, missing required \
                arguments, and other validation issues. It's a safe operation that doesn't access remote state.",
            constraints: vec!["Terraform configuration files must exist in the directory"],
            error_hints: vec![
                (
                    "No Configuration",
                    "Ensure .tf files exist in the project directory",
                ),
                (
                    "Syntax Error",
                    "Check Terraform configuration syntax using terraform fmt",
                ),
            ],
            security_notes: vec!["This is a safe, local operation that doesn't modify anything"],
            examples: vec![ToolExample {
                title: "Basic Validation".to_string(),
                description: "Validate the current Terraform configuration".to_string(),
                input: json!({}),
                expected_output: "Validation result with success status and any error messages"
                    .to_string(),
            }],
        },
        ToolDescriptionKind::TerraformValidateDetailed => DescriptionSpec {
            summary: "Perform comprehensive validation with best practice analysis and detailed diagnostics",
            usage_guide: "This tool performs detailed validation including Terraform's built-in checks plus \
                additional best practice recommendations. It provides detailed diagnostics with file \
                locations and actionable suggestions.",
            constraints: vec!["Terraform configuration files must exist"],
            error_hints: vec![
                ("No Configuration", "Add .tf files to the project directory"),
                (
                    "Best Practice Violation",
                    "Review suggestions to improve configuration quality",
                ),
            ],
            security_notes: vec![
                "Includes security best practice checks",
                "No infrastructure access required - purely local analysis",
            ],
            examples: vec![ToolExample {
                title: "Comprehensive Analysis".to_string(),
                description: "Get detailed validation with best practices".to_string(),
                input: json!({}),
                expected_output:
                    "Detailed report with errors, warnings, suggestions, and file locations"
                        .to_string(),
            }],
        },
        ToolDescriptionKind::TerraformDestroy => DescriptionSpec {
            summary: "Destroy all resources defined in the Terraform configuration",
            usage_guide: "This tool destroys all infrastructure resources managed by Terraform in the current \
                configuration. This is a destructive operation that cannot be undone. Use with extreme caution.",
            constraints: vec![
                "TFMCP_ALLOW_DANGEROUS_OPS must be set to true",
                "Terraform must be initialized",
                "State file must exist with managed resources",
            ],
            error_hints: vec![
                (
                    "Permission Denied",
                    "Set TFMCP_ALLOW_DANGEROUS_OPS=true to enable destroy operations",
                ),
                ("No State", "No Terraform state found - nothing to destroy"),
                (
                    "Auto-approve Blocked",
                    "Set TFMCP_ALLOW_AUTO_APPROVE=true for auto-approval",
                ),
            ],
            security_notes: vec![
                "⚠️ DESTRUCTIVE OPERATION - destroys real infrastructure",
                "All destroy operations are logged for audit purposes",
                "Production directory patterns are automatically blocked",
                "Consider backing up important data before destruction",
            ],
            examples: vec![ToolExample {
                title: "Destroy with Confirmation".to_string(),
                description: "Destroy resources with manual confirmation".to_string(),
                input: json!({"auto_approve": false}),
                expected_output: "Terraform destroy output showing resources removed".to_string(),
            }],
        },
        ToolDescriptionKind::TerraformAnalyze => DescriptionSpec {
            summary: "Analyze Terraform configuration files to extract detailed information about resources, variables, and providers",
            usage_guide: "This tool parses your Terraform configuration files and provides comprehensive analysis \
                including resources, variables, outputs, and provider information. Useful for understanding \
                configuration structure and dependencies.",
            constraints: vec!["Terraform configuration files must exist in the directory"],
            error_hints: vec![
                (
                    "No Configuration",
                    "Add .tf files to the project directory to analyze",
                ),
                (
                    "Parse Error",
                    "Check Terraform syntax - configuration files may be malformed",
                ),
            ],
            security_notes: vec![
                "Analysis is performed locally without accessing remote resources",
            ],
            examples: vec![
                ToolExample {
                    title: "Analyze Current Configuration".to_string(),
                    description: "Analyze all .tf files in the current directory".to_string(),
                    input: json!({}),
                    expected_output:
                        "Structured analysis showing resources, variables, outputs, and providers"
                            .to_string(),
                },
                ToolExample {
                    title: "Analyze Specific Path".to_string(),
                    description: "Analyze configuration in a specific directory".to_string(),
                    input: json!({"path": "/path/to/terraform/config"}),
                    expected_output: "Analysis results for the specified directory".to_string(),
                },
            ],
        },
        ToolDescriptionKind::ListResources => DescriptionSpec {
            summary: "List all resources currently managed by Terraform in the state file",
            usage_guide: "This tool shows all infrastructure resources that are currently being managed by \
                Terraform according to the state file. Useful for understanding what resources exist \
                and their identifiers.",
            constraints: vec!["Terraform must be initialized", "State file must exist"],
            error_hints: vec![
                (
                    "No State",
                    "Run terraform apply to create managed resources first",
                ),
                (
                    "Init Required",
                    "Initialize Terraform working directory first",
                ),
            ],
            security_notes: vec!["Reads from local state file - no remote access required"],
            examples: vec![ToolExample {
                title: "List All Resources".to_string(),
                description: "Show all resources in the current state".to_string(),
                input: json!({}),
                expected_output: "Array of resource identifiers managed by Terraform".to_string(),
            }],
        },
        ToolDescriptionKind::GetState => DescriptionSpec {
            summary: "Retrieve the current Terraform state information",
            usage_guide: "This tool provides access to the current Terraform state, showing the real-world \
                resources that Terraform is managing and their current configuration.",
            constraints: vec!["Terraform must be initialized", "State file must exist"],
            error_hints: vec![
                (
                    "No State",
                    "No state file found - apply configuration first",
                ),
                (
                    "Corrupted State",
                    "State file may be corrupted - check terraform state list",
                ),
            ],
            security_notes: vec![
                "State may contain sensitive information",
                "Read-only operation - state is not modified",
            ],
            examples: vec![ToolExample {
                title: "Get Current State".to_string(),
                description: "Retrieve the complete Terraform state".to_string(),
                input: json!({}),
                expected_output: "Terraform state information including resource details"
                    .to_string(),
            }],
        },
        ToolDescriptionKind::InitTerraform => DescriptionSpec {
            summary: "Initialize a Terraform working directory and download required providers",
            usage_guide: "This tool initializes a Terraform working directory by downloading and installing \
                provider plugins, modules, and setting up the backend. This is typically the first \
                command to run in a new Terraform configuration.",
            constraints: vec![
                "Terraform configuration files must exist",
                "Network access required for downloading providers",
            ],
            error_hints: vec![
                (
                    "No Configuration",
                    "Create .tf files with provider configuration first",
                ),
                (
                    "Network Error",
                    "Check internet connectivity for provider downloads",
                ),
                (
                    "Backend Error",
                    "Verify backend configuration if using remote state",
                ),
            ],
            security_notes: vec![
                "Downloads providers from trusted Terraform Registry",
                "May create local state file containing infrastructure information",
            ],
            examples: vec![ToolExample {
                title: "Initialize Working Directory".to_string(),
                description: "Set up Terraform environment for the current configuration"
                    .to_string(),
                input: json!({}),
                expected_output: "Initialization results showing downloaded providers and modules"
                    .to_string(),
            }],
        },
        ToolDescriptionKind::SetDirectory => DescriptionSpec {
            summary: "Change the active Terraform project directory for subsequent operations",
            usage_guide: "This tool allows you to switch between different Terraform projects by changing \
                the working directory. All subsequent Terraform operations will use the new directory.",
            constraints: vec![
                "Target directory must exist or be creatable",
                "Directory path must be valid",
            ],
            error_hints: vec![
                (
                    "Invalid Path",
                    "Ensure the directory path exists and is accessible",
                ),
                (
                    "Permission Error",
                    "Check read/write permissions for the target directory",
                ),
            ],
            security_notes: vec![
                "Automatically creates sample project if no .tf files exist",
                "Directory changes are logged for audit purposes",
            ],
            examples: vec![ToolExample {
                title: "Switch to Project Directory".to_string(),
                description: "Change to a specific Terraform project".to_string(),
                input: json!({"directory": "/path/to/terraform/project"}),
                expected_output: "Confirmation of directory change with current path".to_string(),
            }],
        },
        ToolDescriptionKind::SecurityStatus => DescriptionSpec {
            summary: "Get current security policy configuration and operational permissions",
            usage_guide: "This tool provides information about the current security settings, including \
                which operations are allowed, audit logging status, and security policy configuration.",
            constraints: vec![],
            error_hints: vec![],
            security_notes: vec![
                "Shows current security policy without exposing sensitive data",
                "Helps understand why certain operations might be blocked",
            ],
            examples: vec![ToolExample {
                title: "Check Security Configuration".to_string(),
                description: "Review current security settings and permissions".to_string(),
                input: json!({}),
                expected_output: "Security policy details, permissions, and audit status"
                    .to_string(),
            }],
        },
    }
}

/// Get all improved tool descriptions
pub fn get_all_tool_descriptions() -> std::collections::HashMap<String, ToolDescription> {
    [
        (
            "get_terraform_plan",
            create_terraform_plan_description as fn() -> ToolDescription,
        ),
        ("apply_terraform", create_terraform_apply_description),
        ("validate_terraform", create_terraform_validate_description),
        (
            "validate_terraform_detailed",
            create_terraform_validate_detailed_description,
        ),
        ("destroy_terraform", create_terraform_destroy_description),
        ("analyze_terraform", create_terraform_analyze_description),
        (
            "list_terraform_resources",
            create_list_resources_description,
        ),
        ("get_terraform_state", create_get_state_description),
        ("init_terraform", create_init_terraform_description),
        ("set_terraform_directory", create_set_directory_description),
        ("get_security_status", create_security_status_description),
    ]
    .into_iter()
    .map(|(name, create)| (name.to_string(), create()))
    .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_all_descriptions_created() {
        let descriptions = get_all_tool_descriptions();

        // Verify all expected tools have descriptions
        assert!(descriptions.contains_key("get_terraform_plan"));
        assert!(descriptions.contains_key("apply_terraform"));
        assert!(descriptions.contains_key("validate_terraform"));
        assert!(descriptions.contains_key("destroy_terraform"));

        // Verify descriptions have content
        for (name, desc) in descriptions {
            assert!(!desc.summary.is_empty(), "Tool {name} missing summary");
        }
    }

    #[test]
    fn test_security_notes_present() {
        let apply_desc = create_terraform_apply_description();
        assert!(!apply_desc.security_notes.is_empty());

        let destroy_desc = create_terraform_destroy_description();
        assert!(!destroy_desc.security_notes.is_empty());
    }

    #[test]
    fn test_examples_present() {
        let apply_desc = create_terraform_apply_description();
        assert!(!apply_desc.examples.is_empty());

        let analyze_desc = create_terraform_analyze_description();
        assert!(!analyze_desc.examples.is_empty());
    }
}