Skip to main content
Glama
carloshpdoc

memorydetective

Count instances by class

countAlive

Count leaked instances per class in a .memgraph file. Specify a class name to get its count, or omit to see the top 20 most-leaked classes. Verify if a fix reduced instance counts.

Instructions

[mg.memory] Count how many times each class appears in a .memgraph's leaked nodes. Provide className (substring) for a single number, or omit it to get the top N most-leaked classes. Use this to confirm whether a fix actually reduced instance counts.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesAbsolute path to a `.memgraph` file.
classNameNoOptional class name (substring). When provided, only that class's count is returned. When omitted, all class counts are returned.
topNNoWhen `className` is omitted, return the top N most-leaked classes (default 20).

Implementation Reference

  • Main handler function 'countAlive' that executes the tool logic: runs leaks, parses the report, counts node occurrences by class name, and returns either a filtered count (if className provided) or the top N most-leaked classes.
    export async function countAlive(
      input: CountAliveInput,
    ): Promise<CountAliveResult> {
      const { report, resolvedPath } = await runLeaksAndParse(input.path);
      const counts = countByClass(report);
      const totalNodes = Array.from(counts.values()).reduce((a, b) => a + b, 0);
    
      if (input.className) {
        let matched = 0;
        for (const [name, n] of counts.entries()) {
          if (name.includes(input.className)) matched += n;
        }
        return {
          ok: true,
          path: resolvedPath,
          totalNodes,
          counts: [{ className: input.className, instanceCount: matched }],
        };
      }
    
      const top = Array.from(counts.entries())
        .map(([name, n]) => ({ className: name, instanceCount: n }))
        .sort((a, b) => b.instanceCount - a.instanceCount)
        .slice(0, input.topN ?? 20);
    
      return { ok: true, path: resolvedPath, totalNodes, counts: top };
    }
  • Helper function 'countByClass' that walks the cycle forest and counts node occurrences by exact className, returning a Map<string, number>.
    export function countByClass(report: LeaksReport): Map<string, number> {
      const counts = new Map<string, number>();
      for (const { node } of walkCycles(report.cycles)) {
        if (!node.className) continue;
        counts.set(node.className, (counts.get(node.className) ?? 0) + 1);
      }
      return counts;
    }
  • Zod schema for the countAlive tool: path (string), className (optional string), topN (number, default 20).
    export const countAliveSchema = z.object({
      path: z.string().min(1).describe("Absolute path to a `.memgraph` file."),
      className: z
        .string()
        .optional()
        .describe(
          "Optional class name (substring). When provided, only that class's count is returned. When omitted, all class counts are returned.",
        ),
      topN: z
        .number()
        .int()
        .positive()
        .default(20)
        .describe(
          "When `className` is omitted, return the top N most-leaked classes (default 20).",
        ),
    });
  • TypeScript interface CountAliveResult defining the return shape: ok, path, totalNodes, counts array.
    export interface CountAliveResult {
      ok: boolean;
      path: string;
      /** Total nodes counted in the cycle forest (across all classes). */
      totalNodes: number;
      /** Per-class counts. When `className` is given, contains a single entry. */
      counts: Array<{ className: string; instanceCount: number }>;
    }
  • src/index.ts:169-180 (registration)
    Registration of the 'countAlive' tool with the MCP server using server.registerTool, including title, description, inputSchema, and async handler.
    server.registerTool(
      "countAlive",
      {
        title: "Count instances by class",
        description:
          "[mg.memory] Count how many times each class appears in a `.memgraph`'s leaked nodes. Provide `className` (substring) for a single number, or omit it to get the top N most-leaked classes. Use this to confirm whether a fix actually reduced instance counts.",
        inputSchema: countAliveSchema.shape,
      },
      async (input) => {
        const result = await countAlive(input);
        return {
          content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
Behavior4/5

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

Without annotations, the description carries full burden. It discloses that it works on leaked nodes and returns counts. It explains the two modes and default topN. However, it does not mention error handling, permissions, or if the tool is read-only, but for a counting tool, this is sufficient.

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?

Two sentences, no wasted words. The first sentence states purpose and source, the second explains usage and motivation. Front-loaded and efficient.

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

Completeness5/5

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

Given the simple nature (counting with three params, no output schema), the description is complete. It explains what is returned (counts per class) and the condition for different outputs. Sibling tools provide context for memory debugging.

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

Parameters5/5

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

Schema coverage is 100%, but the description adds significant value: it clarifies that className is a substring match and explains the difference between providing it or not. It also notes the default topN value, which is not in the schema.

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 verb 'Count' and the resource 'instances by class in a .memgraph leaked nodes'. It distinguishes between two modes (with className for a single number, without for top N), which differentiates it from siblings like analyzeMemgraph or diffMemgraphs.

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

Usage Guidelines4/5

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

Explicitly says 'Use this to confirm whether a fix actually reduced instance counts', providing clear context. It explains the two usage cases (with or without className) but does not mention alternatives or when not to use, missing a point for a 5.

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/carloshpdoc/memorydetective'

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