Skip to content

Persistent project knowledge graph for coding agents. MCP server with semantic search, in-process embeddings, and web explorer.

fix(mcp): treat empty parent_id as omitted

0xk3vin3408c6010e7e
diff --git a/src/__tests__/create-concept.test.ts b/src/__tests__/create-concept.test.ts
new file mode 100644
index 0000000..d233ccc
--- /dev/null
+++ b/src/__tests__/create-concept.test.ts
@@ -0,0 +1,94 @@
+import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
+import { KnowledgeDB } from "../db.js";
+import { createConcept } from "../tools.js";
+import fs from "fs";
+import path from "path";
+import os from "os";
+
+vi.mock("../embeddings.js", () => ({
+ embed: vi.fn(async () => {
+ const embedding = new Float32Array([1, 0, 0]);
+ return Buffer.from(embedding.buffer, embedding.byteOffset, embedding.byteLength);
+ }),
+ embeddingText: vi.fn((name: string, kind: string, summary: string) =>
+ `${kind}: ${name} — ${summary}`
+ ),
+ findTopK: vi.fn(),
+}));
+
+let db: KnowledgeDB;
+let tmpDir: string;
+
+beforeEach(() => {
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "megamemory-create-concept-test-"));
+ db = new KnowledgeDB(path.join(tmpDir, "knowledge.db"));
+});
+
+afterEach(() => {
+ db.close();
+ fs.rmSync(tmpDir, { recursive: true, force: true });
+});
+
+describe("createConcept tool", () => {
+ it("creates a root concept when parent_id is omitted", async () => {
+ const result = await createConcept(db, {
+ name: "Root Concept",
+ kind: "module",
+ summary: "A root concept",
+ });
+
+ expect(result.id).toBe("root-concept");
+ expect(db.getNode("root-concept")!.parent_id).toBeNull();
+ expect(db.getRootNodes().map((node) => node.id)).toContain("root-concept");
+ });
+
+ it("treats empty string parent_id as omitted and stores null", async () => {
+ const result = await createConcept(db, {
+ name: "Empty Parent Root",
+ kind: "feature",
+ summary: "A root concept created with an empty parent_id",
+ parent_id: "",
+ });
+
+ const node = db.getNode("empty-parent-root")!;
+ expect(result.id).toBe("empty-parent-root");
+ expect(node.parent_id).toBeNull();
+ expect(db.getRootNodes().map((root) => root.id)).toContain("empty-parent-root");
+ });
+
+ it("creates a child concept for a valid non-empty parent_id", async () => {
+ const parent = await createConcept(db, {
+ name: "Parent Concept",
+ kind: "module",
+ summary: "A parent concept",
+ });
+
+ const child = await createConcept(db, {
+ name: "Child Concept",
+ kind: "feature",
+ summary: "A child concept",
+ parent_id: parent.id,
+ });
+
+ const childNode = db.getNode("parent-concept/child-concept")!;
+ expect(child.id).toBe("parent-concept/child-concept");
+ expect(childNode.parent_id).toBe("parent-concept");
+ expect(db.getChildren("parent-concept").map((node) => node.id)).toContain(
+ "parent-concept/child-concept"
+ );
+ });
+
+ it("rejects invalid non-empty parent_id without normalizing it", async () => {
+ await expect(
+ createConcept(db, {
+ name: "Orphan Concept",
+ kind: "feature",
+ summary: "Should not be created",
+ parent_id: "null",
+ })
+ ).rejects.toThrow('Parent concept "null" does not exist.');
+
+ expect(db.getNode("null/orphan-concept")).toBeUndefined();
+ expect(db.getNode("orphan-concept")).toBeUndefined();
+ });
+});
diff --git a/src/index.ts b/src/index.ts
index 0b25284..b4c07c2 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -275,7 +275,7 @@ async function startMcpServer() {
kind: NodeKindEnum.describe("Type of concept: feature, module, pattern, config, decision, component"),
summary: z.string().describe("What this concept is. Be specific: include parameter names, defaults, file paths, behavior details."),
why: z.string().optional().describe("Why this exists or was built this way"),
- parent_id: z.string().optional().describe("Parent concept ID for nesting"),
+ parent_id: z.string().optional().describe("Parent concept ID for nesting; empty string is treated as omitted"),
file_refs: z.array(z.string()).optional().describe("Relevant file paths + optional line ranges"),
edges: z.array(z.object({
to: z.string().describe("Target concept ID"),
diff --git a/src/tools.ts b/src/tools.ts
index 241d3ad..97c9ee5 100644
--- a/src/tools.ts
+++ b/src/tools.ts
@@ -151,7 +151,8 @@ export async function createConcept(
db: KnowledgeDB,
input: CreateConceptInput
): Promise<{ id: string; message: string }> {
- const id = makeId(input.name, input.parent_id);
+ const parentId = input.parent_id === "" ? undefined : input.parent_id;
+ const id = makeId(input.name, parentId);
// Check if node already exists
if (db.nodeExists(id)) {
@@ -159,8 +160,8 @@ export async function createConcept(
}
// Validate parent exists if specified
- if (input.parent_id && !db.nodeExists(input.parent_id)) {
- throw new Error(`Parent concept "${input.parent_id}" does not exist.`);
+ if (parentId && !db.nodeExists(parentId)) {
+ throw new Error(`Parent concept "${parentId}" does not exist.`);
}
// Generate embedding
@@ -175,7 +176,7 @@ export async function createConcept(
summary: input.summary,
why: input.why ?? null,
file_refs: input.file_refs ? JSON.stringify(input.file_refs) : null,
- parent_id: input.parent_id ?? null,
+ parent_id: parentId ?? null,
created_by_task: input.created_by_task ?? null,
embedding,
},