LSP Integration
Using terraform-ls for hover docs, completions, diagnostics, and formatting
LSP Integration Guide
Terry-Form MCP includes a full Language Server Protocol (LSP) client that wraps terraform-ls 0.38.5. This provides intelligent code features for Terraform files.
Overview
The LSP integration provides 5 tools:
| Tool | Description |
|---|---|
terraform_validate_lsp |
Validate a file using LSP diagnostics |
terraform_hover |
Get documentation at a cursor position |
terraform_complete |
Get code completion suggestions |
terraform_format_lsp |
Format a Terraform file |
terraform_lsp_status |
Check LSP server status |
Plus 2 diagnostic tools:
| Tool | Description |
|---|---|
terry_lsp_init |
Manually initialize LSP for a workspace |
terry_lsp_debug |
Debug LSP functionality |
How It Works
graph LR
A[MCP Tool Call] --> B[terraform_lsp_client.py]
B --> C[terraform-ls Process]
C --> D[LSP Response]
D --> B
B --> E[Formatted Result]
The LSP client:
- Spawns a
terraform-ls servesubprocess - Communicates via JSON-RPC over stdio
- Initializes the workspace root
- Processes requests (hover, complete, validate, format)
- Returns structured results
The first LSP call may take 1-2 seconds while terraform-ls initializes and indexes the workspace. Subsequent calls are faster.
Validating Files
Get LSP diagnostics for a Terraform file:
{
"tool": "terraform_validate_lsp",
"arguments": {
"file_path": "main.tf",
"workspace_path": "my-project"
}
}
Response:
{
"file_path": "main.tf",
"diagnostics": [
{
"range": {
"start": {"line": 10, "character": 5},
"end": {"line": 10, "character": 15}
},
"severity": "error",
"message": "Unknown resource type 'aws_s3_buckt'"
}
],
"valid": false
}
Hover Documentation
Get documentation for any symbol at a specific position:
{
"tool": "terraform_hover",
"arguments": {
"file_path": "main.tf",
"line": 5,
"character": 10,
"workspace_path": "my-project"
}
}
Positions are 0-based (first line = 0, first character = 0).
Returns provider documentation, attribute descriptions, and type information.
Code Completions
Get completion suggestions at a cursor position:
{
"tool": "terraform_complete",
"arguments": {
"file_path": "main.tf",
"line": 12,
"character": 4,
"workspace_path": "my-project"
}
}
Response:
{
"completions": [
{
"label": "instance_type",
"kind": "Property",
"detail": "string",
"documentation": "The instance type to use",
"insertText": "instance_type = \"${1:t3.micro}\""
}
]
}
Formatting
Format a Terraform file according to HCL standards:
{
"tool": "terraform_format_lsp",
"arguments": {
"file_path": "main.tf",
"workspace_path": "my-project"
}
}
Returns edit instructions that were applied to the file.
LSP Status
Check the current state of the LSP server:
{"tool": "terraform_lsp_status"}
Response:
{
"status": "active",
"initialized": true,
"capabilities": ["completion", "hover", "validation", "formatting"],
"workspace_root": "/mnt/workspace/my-project"
}
Initialization
The LSP client initializes automatically on the first LSP tool call. To manually initialize for a specific workspace:
{
"tool": "terry_lsp_init",
"arguments": {
"workspace_path": "my-project"
}
}
Debugging
If LSP features aren’t working, use the debug tool:
{"tool": "terry_lsp_debug"}
This checks:
- terraform-ls binary availability and version
- LSP client state (initialized, workspace, process active)
- terraform-ls help output
Troubleshooting
LSP Not Initializing
- Check terraform-ls is installed:
terry_environment_check - Ensure workspace has
.tffiles - Try manual init:
terry_lsp_initwith the workspace path - Check debug output:
terry_lsp_debug
Slow Responses
- First call takes 1-2 seconds for initialization
- Large workspaces with many modules take longer
- Ensure
inithas been run on the workspace first
No Completions
- Completions require an initialized workspace (run
terrywithinitfirst) - Ensure the cursor position is valid (0-based line and character)
- Check that the file exists and is readable:
terry_file_check
Wrong Workspace
The LSP client uses a single workspace root. If you switch between workspaces, re-initialize with terry_lsp_init for the new workspace.