exec_command
Execute commands within running Docker containers to manage processes, run scripts, or perform maintenance tasks directly from your AI assistant.
Instructions
Execute a command inside a running Docker container.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | Container ID or name | |
| command | Yes | Command and arguments, e.g. ['ls', '-la'] |
Implementation Reference
- src/docker.ts:100-122 (handler)Core handler function that executes a command inside a Docker container using Dockerode. Creates an exec instance, starts it, collects output chunks from the stream, and returns the combined output as a string.
export async function execCommand( id: string, command: string[], ): Promise<string> { const container = docker.getContainer(id); const exec = await container.exec({ Cmd: command, AttachStdout: true, AttachStderr: true, }); const stream = await exec.start({ hijack: true, stdin: false }); return new Promise((resolve) => { const chunks: Buffer[] = []; stream.on("data", (chunk: Buffer) => chunks.push(chunk)); stream.on("end", () => { resolve( Buffer.concat(chunks) .toString("utf-8") .replace(/[\x00-\x08]/g, ""), // eslint-disable-line no-control-regex ); }); }); } - src/index.ts:117-132 (registration)Registration of the exec_command tool with the MCP server. Defines the tool name, description, input schema, and async handler that calls execCommand and returns the result as text content.
server.tool( "exec_command", "Execute a command inside a running Docker container.", { id: z.string().describe("Container ID or name"), command: z .array(z.string()) .describe("Command and arguments, e.g. ['ls', '-la']"), }, async ({ id, command }) => { const output = await execCommand(id, command); return { content: [{ type: "text", text: output || "(no output)" }], }; }, ); - src/index.ts:120-125 (schema)Zod schema definition for exec_command tool inputs: 'id' (string for container ID/name) and 'command' (array of strings for command and arguments).
{ id: z.string().describe("Container ID or name"), command: z .array(z.string()) .describe("Command and arguments, e.g. ['ls', '-la']"), },