Skip to main content
Glama

container_stats

Monitor Docker container performance by retrieving CPU, memory, and network statistics for running containers.

Instructions

Get CPU, memory, and network stats for a running Docker container.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
idYesContainer ID or name

Implementation Reference

  • The containerStats function is the core handler that executes the tool logic. It retrieves Docker container stats via dockerode, calculates CPU percentage from cpu_stats, computes memory usage/limit/percentage, and aggregates network RX/TX bytes across all interfaces, returning a formatted ContainerStats object.
    export async function containerStats(id: string): Promise<ContainerStats> {
      const container = docker.getContainer(id);
      const stats = (await container.stats({
        stream: false,
      })) as {
        cpu_stats: CpuStats;
        precpu_stats: CpuStats;
        memory_stats: MemoryStats;
        networks?: NetworkStats;
      };
    
      const cpuStats = stats.cpu_stats;
      const precpuStats = stats.precpu_stats;
      const cpuDelta =
        cpuStats.cpu_usage.total_usage - precpuStats.cpu_usage.total_usage;
      const systemDelta = cpuStats.system_cpu_usage - precpuStats.system_cpu_usage;
      const cpuCount = cpuStats.cpu_usage.percpu_usage?.length ?? 1;
      const cpuPercent =
        systemDelta > 0
          ? ((cpuDelta / systemDelta) * cpuCount * 100).toFixed(2)
          : "0.00";
    
      const memUsage = stats.memory_stats.usage ?? 0;
      const memLimit = stats.memory_stats.limit ?? 0;
      const memPercent =
        memLimit > 0 ? ((memUsage / memLimit) * 100).toFixed(2) : "0.00";
    
      const networks = stats.networks ?? {};
      let rxBytes = 0;
      let txBytes = 0;
      for (const iface of Object.values(networks)) {
        rxBytes += iface.rx_bytes ?? 0;
        txBytes += iface.tx_bytes ?? 0;
      }
    
      return {
        cpu_percent: `${cpuPercent}%`,
        memory_usage: formatBytes(memUsage),
        memory_limit: formatBytes(memLimit),
        memory_percent: `${memPercent}%`,
        network_rx: formatBytes(rxBytes),
        network_tx: formatBytes(txBytes),
      };
    }
  • The ContainerStats interface defines the output schema with typed fields: cpu_percent, memory_usage, memory_limit, memory_percent, network_rx, and network_tx (all strings with formatted values).
    export interface ContainerStats {
      cpu_percent: string;
      memory_usage: string;
      memory_limit: string;
      memory_percent: string;
      network_rx: string;
      network_tx: string;
    }
  • src/index.ts:134-153 (registration)
    Registration of the 'container_stats' tool using server.tool() with a zod schema defining 'id' parameter (string, required). The handler calls containerStats(id) and formats the result into a human-readable text output showing CPU, memory, and network statistics.
    server.tool(
      "container_stats",
      "Get CPU, memory, and network stats for a running Docker container.",
      { id: z.string().describe("Container ID or name") },
      async ({ id }) => {
        const stats = await containerStats(id);
        return {
          content: [
            {
              type: "text",
              text: [
                `CPU:     ${stats.cpu_percent}`,
                `Memory:  ${stats.memory_usage} / ${stats.memory_limit} (${stats.memory_percent})`,
                `Network: ↓ ${stats.network_rx}  ↑ ${stats.network_tx}`,
              ].join("\n"),
            },
          ],
        };
      },
    );
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It mentions the tool retrieves stats but lacks details on behavioral traits such as permissions required, rate limits, error handling, or whether it's a read-only operation. The description is minimal and does not compensate for the absence of annotations.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's purpose without unnecessary words. It is front-loaded and appropriately sized, making it easy to understand quickly with zero waste.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (retrieving stats for a container) and no annotations or output schema, the description is adequate but incomplete. It covers the basic purpose but lacks details on output format, error conditions, or dependencies, which could hinder an AI agent's ability to use it effectively without additional context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% description coverage, with the 'id' parameter documented as 'Container ID or name.' The description does not add any meaning beyond this, such as format examples or constraints. With high schema coverage, the baseline score of 3 is appropriate as the schema handles the parameter documentation.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Get') and the resource ('CPU, memory, and network stats for a running Docker container'), distinguishing it from siblings like container_logs (logs) or exec_command (execute commands). It precisely identifies what the tool does without being vague or tautological.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage by specifying 'for a running Docker container,' suggesting it should be used when a container is active. However, it does not explicitly state when to use this tool versus alternatives (e.g., list_containers for status vs. stats) or provide exclusions, leaving some ambiguity in context.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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