Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20251007-124945.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: Added automatic container runtime detection for MCP server integration with Docker and Podman support
time: 2025-10-07T12:49:45.840468+05:30
custom:
Issue: "2139"
Repository: vscode-terraform
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,20 @@ You can enable or disable this feature via:
- The Command Palette: `HashiCorp Terraform: Enable MCP Server` or `HashiCorp Terraform: Disable MCP Server`
- Settings: `terraform.mcp.server.enable`

Note that enabling this feature requires Docker to be installed and running on your system.
#### Container Runtime Configuration

When enabled, the MCP server runs as a Docker container (`hashicorp/terraform-mcp-server`), providing your AI assistant with contextual knowledge about Terraform providers, modules, and best practices. The extension automatically manages the server lifecycle, starting it when needed for AI interactions.
The extension automatically detects available container runtimes on your system and uses them in the following priority order:

1. **Docker** (preferred) - If Docker is installed and running
2. **Podman** (fallback) - If Docker is not available but Podman is installed and running

**Supported container runtimes:**
- `docker` - Requires Docker to be installed and running
- `podman` - Requires Podman to be installed and running

**Note:** At least one supported container runtime (Docker or Podman) must be installed and running on your system before enabling the MCP server integration. If neither is available, the extension will show an error message with links to installation guides for both runtimes.

When enabled, the MCP server runs as a container (`hashicorp/terraform-mcp-server`), providing your AI assistant with contextual knowledge about Terraform providers, modules, and best practices. The extension automatically manages the server lifecycle, starting it when needed for AI interactions.

![](/docs/hashicorp-terraform-mcp-server.gif)

Expand Down
76 changes: 48 additions & 28 deletions src/features/mcpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { config } from '../utils/vscode';

const execAsync = promisify(exec);

type ContainerRuntime = 'docker' | 'podman';

interface McpServerDefinition {
label: string;
command: string;
Expand Down Expand Up @@ -98,10 +100,11 @@ export class McpServerFeature {
return [];
}

// Use Docker as default command, runtime detection happens in resolveMcpServerDefinition
const server: McpServerDefinition = {
label: 'HashiCorp Terraform MCP Server',
command: 'docker',
args: ['run', '-i', '--rm', 'hashicorp/terraform-mcp-server'],
command: 'docker', // Default to docker, will be resolved later
args: ['run', '-i', '--rm', 'hashicorp/terraform-mcp-server:0.2'],
env: {},
};

Expand All @@ -120,43 +123,60 @@ export class McpServerFeature {
return definition;
}

const dockerAvailable = await this.dockerValidations();
if (!dockerAvailable) {
throw new Error('Docker is required but not available or running');
const availableRuntime = await this.getAvailableContainerRuntime();
if (!availableRuntime) {
const errorMessage =
'Please install and start a Docker compatible runtime (docker or podman) to use this feature.';
void vscode.window.showErrorMessage(errorMessage, 'Docker', 'Podman').then((selection) => {
if (selection === 'Docker') {
void vscode.env.openExternal(vscode.Uri.parse('https://docs.docker.com/get-started/'));
} else if (selection === 'Podman') {
void vscode.env.openExternal(vscode.Uri.parse('https://podman.io/get-started'));
}
});
throw new Error(errorMessage);
}

this.reporter.sendTelemetryEvent('terraform-mcp-server-start');
return definition;
this.outputChannel.appendLine(
`Container runtime command to use for running the HashiCorp Terraform MCP Server ${availableRuntime}`,
);

// Update the definition to use the available container runtime
const resolvedDefinition: McpServerDefinition = {
...definition,
command: availableRuntime,
};

this.reporter.sendTelemetryEvent('terraform-mcp-server-start', {
containerRuntime: availableRuntime,
});

return resolvedDefinition;
}

private async dockerValidations(): Promise<boolean> {
try {
if (!(await this.checkDockerRunning())) {
return false;
}
// Check which container runtime is available, preferring Docker over Podman
private async getAvailableContainerRuntime(): Promise<ContainerRuntime | null> {
// Check Docker first (preferred)
if (await this.checkContainerRuntimeAvailable('docker')) {
return 'docker';
}

return true;
} catch (error) {
this.logError('Docker validation error', error);
return false;
// Fall back to Podman
if (await this.checkContainerRuntimeAvailable('podman')) {
return 'podman';
}

return null;
}

// Check if container runtime is available and running
// The 'docker info' command validates both installation and daemon status
private async checkDockerRunning(): Promise<boolean> {
// Check if a specific container runtime is available and running
// The 'info' command validates both installation and daemon status
private async checkContainerRuntimeAvailable(runtime: ContainerRuntime): Promise<boolean> {
try {
await execAsync('docker info', { timeout: 5000 });
await execAsync(`${runtime} info`, { timeout: 5000 });
return true;
} catch (error) {
this.logError('Docker daemon check failed', error);
void vscode.window
.showWarningMessage('Please install and start a Docker compatible runtime to use this feature.', 'Learn More')
.then((selection) => {
if (selection === 'Learn More') {
void vscode.env.openExternal(vscode.Uri.parse('https://docs.docker.com/get-started/'));
}
});
this.logError(`${runtime} daemon check failed`, error);
return false;
}
}
Expand Down