Count instances by class
countAliveCount 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
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Absolute path to a `.memgraph` file. | |
| className | No | Optional class name (substring). When provided, only that class's count is returned. When omitted, all class counts are returned. | |
| topN | No | When `className` is omitted, return the top N most-leaked classes (default 20). |
Implementation Reference
- src/tools/countAlive.ts:45-71 (handler)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 }; } - src/tools/countAlive.ts:36-43 (helper)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; } - src/tools/countAlive.ts:6-22 (schema)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).", ), }); - src/tools/countAlive.ts:26-33 (schema)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) }],