manage_containers
Execute Docker container operations including listing, starting, stopping, removing, and restarting containers through natural language commands.
Instructions
Manage Docker containers (list, start, stop, remove)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Action to perform on containers | |
| all | No | Include stopped containers when listing | |
| container | No | Container name or ID (required for start, stop, remove, restart) |
Implementation Reference
- src/index.ts:1208-1261 (handler)Handler function that implements the core logic for managing Docker containers. It handles actions: list (with optional all flag), start, stop, remove, restart specific containers by building and executing appropriate Docker CLI commands via executeDockerCommand helper.}, async ({ action, container, all }) => { try { let command: string; switch (action) { case "list": command = all ? "docker ps -a" : "docker ps"; break; case "start": if (!container) throw new Error("Container name or ID is required for start action"); command = `docker start ${container}`; break; case "stop": if (!container) throw new Error("Container name or ID is required for stop action"); command = `docker stop ${container}`; break; case "remove": if (!container) throw new Error("Container name or ID is required for remove action"); command = `docker rm ${container}`; break; case "restart": if (!container) throw new Error("Container name or ID is required for restart action"); command = `docker restart ${container}`; break; } const result = await executeDockerCommand(command); return { content: [ { type: "text", text: `Container ${action} completed:\n\n${result.stdout}${result.stderr ? `\nWarnings:\n${result.stderr}` : ""}` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error managing containers: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } } ); // Register Docker image management tool server.registerTool( "manage_images",
- src/index.ts:1200-1207 (schema)Tool schema definition including title, description, and Zod inputSchema for parameters: action (enum), container (string optional), all (boolean optional).{ title: "Manage Docker Containers", description: "Manage Docker containers (list, start, stop, remove)", inputSchema: { action: z.enum(["list", "start", "stop", "remove", "restart"]).describe("Action to perform on containers"), container: z.string().optional().describe("Container name or ID (required for start, stop, remove, restart)"), all: z.boolean().optional().describe("Include stopped containers when listing") }
- src/index.ts:1198-1262 (registration)Registration of the 'manage_containers' tool with McpServer using server.registerTool, including schema and inline handler function.server.registerTool( "manage_containers", { title: "Manage Docker Containers", description: "Manage Docker containers (list, start, stop, remove)", inputSchema: { action: z.enum(["list", "start", "stop", "remove", "restart"]).describe("Action to perform on containers"), container: z.string().optional().describe("Container name or ID (required for start, stop, remove, restart)"), all: z.boolean().optional().describe("Include stopped containers when listing") } }, async ({ action, container, all }) => { try { let command: string; switch (action) { case "list": command = all ? "docker ps -a" : "docker ps"; break; case "start": if (!container) throw new Error("Container name or ID is required for start action"); command = `docker start ${container}`; break; case "stop": if (!container) throw new Error("Container name or ID is required for stop action"); command = `docker stop ${container}`; break; case "remove": if (!container) throw new Error("Container name or ID is required for remove action"); command = `docker rm ${container}`; break; case "restart": if (!container) throw new Error("Container name or ID is required for restart action"); command = `docker restart ${container}`; break; } const result = await executeDockerCommand(command); return { content: [ { type: "text", text: `Container ${action} completed:\n\n${result.stdout}${result.stderr ? `\nWarnings:\n${result.stderr}` : ""}` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error managing containers: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } } ); // Register Docker image management tool server.registerTool( "manage_images", {
- src/index.ts:383-390 (helper)Helper function executeDockerCommand used by the tool to run Docker CLI commands asynchronously using child_process.exec promisified.async function executeDockerCommand(command: string): Promise<{ stdout: string; stderr: string }> { try { const result = await execAsync(command); return result; } catch (error: any) { throw new Error(`Docker command failed: ${error.message}`); } }