Skip to content

Commit 9a46fbc

Browse files
authored
Added automatic container runtime detection for MCP server integration with Docker and Podman support (#2139)
* feat(mcp-server-integration): TF-28514: Added configurable container runtime for MCP server integration * feat(mcp-server-integration): TF-28514: Added changelog * feat(mcp-server-integration): TF-28514: Modified: image version * feat(mcp-server-integration): TF-28514: Added: automatic container runtime detection for MCP server integration with Docker and Podman support
1 parent 9969655 commit 9a46fbc

File tree

3 files changed

+67
-30
lines changed

3 files changed

+67
-30
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: ENHANCEMENTS
2+
body: Added automatic container runtime detection for MCP server integration with Docker and Podman support
3+
time: 2025-10-07T12:49:45.840468+05:30
4+
custom:
5+
Issue: "2139"
6+
Repository: vscode-terraform

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,20 @@ You can enable or disable this feature via:
185185
- The Command Palette: `HashiCorp Terraform: Enable MCP Server` or `HashiCorp Terraform: Disable MCP Server`
186186
- Settings: `terraform.mcp.server.enable`
187187

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

190-
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.
190+
The extension automatically detects available container runtimes on your system and uses them in the following priority order:
191+
192+
1. **Docker** (preferred) - If Docker is installed and running
193+
2. **Podman** (fallback) - If Docker is not available but Podman is installed and running
194+
195+
**Supported container runtimes:**
196+
- `docker` - Requires Docker to be installed and running
197+
- `podman` - Requires Podman to be installed and running
198+
199+
**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.
200+
201+
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.
191202

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

src/features/mcpServer.ts

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { config } from '../utils/vscode';
1111

1212
const execAsync = promisify(exec);
1313

14+
type ContainerRuntime = 'docker' | 'podman';
15+
1416
interface McpServerDefinition {
1517
label: string;
1618
command: string;
@@ -98,10 +100,11 @@ export class McpServerFeature {
98100
return [];
99101
}
100102

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

@@ -120,43 +123,60 @@ export class McpServerFeature {
120123
return definition;
121124
}
122125

123-
const dockerAvailable = await this.dockerValidations();
124-
if (!dockerAvailable) {
125-
throw new Error('Docker is required but not available or running');
126+
const availableRuntime = await this.getAvailableContainerRuntime();
127+
if (!availableRuntime) {
128+
const errorMessage =
129+
'Please install and start a Docker compatible runtime (docker or podman) to use this feature.';
130+
void vscode.window.showErrorMessage(errorMessage, 'Docker', 'Podman').then((selection) => {
131+
if (selection === 'Docker') {
132+
void vscode.env.openExternal(vscode.Uri.parse('https://docs.docker.com/get-started/'));
133+
} else if (selection === 'Podman') {
134+
void vscode.env.openExternal(vscode.Uri.parse('https://podman.io/get-started'));
135+
}
136+
});
137+
throw new Error(errorMessage);
126138
}
127139

128-
this.reporter.sendTelemetryEvent('terraform-mcp-server-start');
129-
return definition;
140+
this.outputChannel.appendLine(
141+
`Container runtime command to use for running the HashiCorp Terraform MCP Server ${availableRuntime}`,
142+
);
143+
144+
// Update the definition to use the available container runtime
145+
const resolvedDefinition: McpServerDefinition = {
146+
...definition,
147+
command: availableRuntime,
148+
};
149+
150+
this.reporter.sendTelemetryEvent('terraform-mcp-server-start', {
151+
containerRuntime: availableRuntime,
152+
});
153+
154+
return resolvedDefinition;
130155
}
131156

132-
private async dockerValidations(): Promise<boolean> {
133-
try {
134-
if (!(await this.checkDockerRunning())) {
135-
return false;
136-
}
157+
// Check which container runtime is available, preferring Docker over Podman
158+
private async getAvailableContainerRuntime(): Promise<ContainerRuntime | null> {
159+
// Check Docker first (preferred)
160+
if (await this.checkContainerRuntimeAvailable('docker')) {
161+
return 'docker';
162+
}
137163

138-
return true;
139-
} catch (error) {
140-
this.logError('Docker validation error', error);
141-
return false;
164+
// Fall back to Podman
165+
if (await this.checkContainerRuntimeAvailable('podman')) {
166+
return 'podman';
142167
}
168+
169+
return null;
143170
}
144171

145-
// Check if container runtime is available and running
146-
// The 'docker info' command validates both installation and daemon status
147-
private async checkDockerRunning(): Promise<boolean> {
172+
// Check if a specific container runtime is available and running
173+
// The 'info' command validates both installation and daemon status
174+
private async checkContainerRuntimeAvailable(runtime: ContainerRuntime): Promise<boolean> {
148175
try {
149-
await execAsync('docker info', { timeout: 5000 });
176+
await execAsync(`${runtime} info`, { timeout: 5000 });
150177
return true;
151178
} catch (error) {
152-
this.logError('Docker daemon check failed', error);
153-
void vscode.window
154-
.showWarningMessage('Please install and start a Docker compatible runtime to use this feature.', 'Learn More')
155-
.then((selection) => {
156-
if (selection === 'Learn More') {
157-
void vscode.env.openExternal(vscode.Uri.parse('https://docs.docker.com/get-started/'));
158-
}
159-
});
179+
this.logError(`${runtime} daemon check failed`, error);
160180
return false;
161181
}
162182
}

0 commit comments

Comments
 (0)