Skip to main content
Glama

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
NameRequiredDescriptionDefault
idYesContainer ID or name
commandYesCommand and arguments, e.g. ['ls', '-la']

Implementation Reference

  • 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)" }],
        };
      },
    );
  • 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']"),
    },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ofershap/mcp-server-docker'

If you have feedback or need assistance with the MCP directory API, please join our Discord server