Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

feat: include property definitions per label in get_graph_schema

test9021c22b14b8
diff --git a/README.md b/README.md
index f45ced0fd..ba65736ff 100644
--- a/README.md
+++ b/README.md
@@ -317,7 +317,7 @@ codebase-memory-mcp cli --raw search_graph '{"label": "Function"}' | jq '.result
| `trace_call_path` | BFS traversal — who calls a function and what it calls. Depth 1-5. |
| `detect_changes` | Map git diff to affected symbols + blast radius with risk classification. |
| `query_graph` | Execute Cypher-like graph queries (read-only). |
-| `get_graph_schema` | Node/edge counts, relationship patterns. Run this first. |
+| `get_graph_schema` | Node/edge counts, relationship patterns, property definitions per label. Run this first. |
| `get_code_snippet` | Read source code for a function by qualified name. |
| `get_architecture` | Codebase overview: languages, packages, routes, hotspots, clusters, ADR. |
| `search_code` | Grep-like text search within indexed project files. |
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 7c0ce7817..ae5b5c314 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -994,6 +994,11 @@ static char *handle_get_graph_schema(cbm_mcp_server_t *srv, const char *args) {
yyjson_mut_val *lbl = yyjson_mut_obj(doc);
yyjson_mut_obj_add_str(doc, lbl, "label", schema.node_labels[i].label);
yyjson_mut_obj_add_int(doc, lbl, "count", schema.node_labels[i].count);
+ yyjson_mut_val *props = yyjson_mut_arr(doc);
+ for (int j = 0; j < schema.node_labels[i].property_count; j++) {
+ yyjson_mut_arr_add_str(doc, props, schema.node_labels[i].properties[j]);
+ }
+ yyjson_mut_obj_add_val(doc, lbl, "properties", props);
yyjson_mut_arr_add_val(labels, lbl);
}
yyjson_mut_obj_add_val(doc, root, "node_labels", labels);
@@ -1003,6 +1008,11 @@ static char *handle_get_graph_schema(cbm_mcp_server_t *srv, const char *args) {
yyjson_mut_val *typ = yyjson_mut_obj(doc);
yyjson_mut_obj_add_str(doc, typ, "type", schema.edge_types[i].type);
yyjson_mut_obj_add_int(doc, typ, "count", schema.edge_types[i].count);
+ yyjson_mut_val *eprops = yyjson_mut_arr(doc);
+ for (int j = 0; j < schema.edge_types[i].property_count; j++) {
+ yyjson_mut_arr_add_str(doc, eprops, schema.edge_types[i].properties[j]);
+ }
+ yyjson_mut_obj_add_val(doc, typ, "properties", eprops);
yyjson_mut_arr_add_val(types, typ);
}
yyjson_mut_obj_add_val(doc, root, "edge_types", types);
diff --git a/src/store/store.c b/src/store/store.c
index 3d3f3cbe3..5d73dcce9 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -2696,6 +2696,36 @@ int cbm_deduplicate_hops(const cbm_node_hop_t *hops, int hop_count, cbm_node_hop
/* ── Schema ─────────────────────────────────────────────────────── */
+enum { SCHEMA_MAX_JSON_KEYS = 50 };
+
+/* Discover distinct JSON property keys for a table/column via json_each().
+ * Prepends base_cols, then appends up to SCHEMA_MAX_JSON_KEYS from the query.
+ * Caller must free the returned array and each string in it. */
+static void schema_discover_props(sqlite3 *db, const char *sql, const char *project,
+ const char *filter, const char **base_cols, int base_col_count,
+ char ***out_props, int *out_count) {
+ int pcap = base_col_count + SCHEMA_MAX_JSON_KEYS;
+ char **props = malloc(pcap * sizeof(char *));
+ int pn = 0;
+
+ for (int b = 0; b < base_col_count; b++) {
+ props[pn++] = heap_strdup(base_cols[b]);
+ }
+
+ sqlite3_stmt *pstmt = NULL;
+ if (sqlite3_prepare_v2(db, sql, CBM_NOT_FOUND, &pstmt, NULL) == SQLITE_OK) {
+ bind_text(pstmt, SKIP_ONE, project);
+ bind_text(pstmt, PAIR_LEN, filter);
+ while (sqlite3_step(pstmt) == SQLITE_ROW && pn < pcap) {
+ props[pn++] = heap_strdup((const char *)sqlite3_column_text(pstmt, 0));
+ }
+ sqlite3_finalize(pstmt);
+ }
+
+ *out_props = props;
+ *out_count = pn;
+}
+
int cbm_store_get_schema(cbm_store_t *s, const char *project, cbm_schema_info_t *out) {
memset(out, 0, sizeof(*out));
if (!s || !s->db) {
@@ -2720,6 +2750,8 @@ int cbm_store_get_schema(cbm_store_t *s, const char *project, cbm_schema_info_t
}
arr[n].label = heap_strdup((const char *)sqlite3_column_text(stmt, 0));
arr[n].count = sqlite3_column_int(stmt, SKIP_ONE);
+ arr[n].properties = NULL;
+ arr[n].property_count = 0;
n++;
}
sqlite3_finalize(stmt);
@@ -2727,6 +2759,25 @@ int cbm_store_get_schema(cbm_store_t *s, const char *project, cbm_schema_info_t
out->node_label_count = n;
}
+ /* Node label property keys: base columns + distinct JSON property keys per label */
+ {
+ static const char *node_base_cols[] = {"name", "qualified_name", "file_path", "start_line",
+ "end_line"};
+ const char *prop_sql = "SELECT DISTINCT je.key "
+ "FROM nodes, json_each(nodes.properties) AS je "
+ "WHERE nodes.project = ?1 AND nodes.label = ?2 "
+ " AND nodes.properties != '{}' "
+ "ORDER BY je.key "
+ "LIMIT 50;";
+
+ for (int i = 0; i < out->node_label_count; i++) {
+ schema_discover_props(
+ s->db, prop_sql, project, out->node_labels[i].label, node_base_cols,
+ (int)(sizeof(node_base_cols) / sizeof(node_base_cols[0])),
+ &out->node_labels[i].properties, &out->node_labels[i].property_count);
+ }
+ }
+
/* Edge types */
{
const char *sql = "SELECT type, COUNT(*) FROM edges WHERE project = ?1 GROUP BY type ORDER "
@@ -2745,6 +2796,8 @@ int cbm_store_get_schema(cbm_store_t *s, const char *project, cbm_schema_info_t
}
arr[n].type = heap_strdup((const char *)sqlite3_column_text(stmt, 0));
arr[n].count = sqlite3_column_int(stmt, SKIP_ONE);
+ arr[n].properties = NULL;
+ arr[n].property_count = 0;
n++;
}
sqlite3_finalize(stmt);
@@ -2752,6 +2805,24 @@ int cbm_store_get_schema(cbm_store_t *s, const char *project, cbm_schema_info_t
out->edge_type_count = n;
}
+ /* Edge type property keys: base columns + distinct JSON property keys per type */
+ {
+ static const char *edge_base_cols[] = {"source_id", "target_id"};
+ const char *prop_sql = "SELECT DISTINCT je.key "
+ "FROM edges, json_each(edges.properties) AS je "
+ "WHERE edges.project = ?1 AND edges.type = ?2 "
+ " AND edges.properties != '{}' "
+ "ORDER BY je.key "
+ "LIMIT 50;";
+
+ for (int i = 0; i < out->edge_type_count; i++) {
+ schema_discover_props(s->db, prop_sql, project, out->edge_types[i].type, edge_base_cols,
+ (int)(sizeof(edge_base_cols) / sizeof(edge_base_cols[0])),
+ &out->edge_types[i].properties,
+ &out->edge_types[i].property_count);
+ }
+ }
+
return CBM_STORE_OK;
}
@@ -2761,11 +2832,19 @@ void cbm_store_schema_free(cbm_schema_info_t *out) {
}
for (int i = 0; i < out->node_label_count; i++) {
free((void *)out->node_labels[i].label);
+ for (int j = 0; j < out->node_labels[i].property_count; j++) {
+ free(out->node_labels[i].properties[j]);
+ }
+ free(out->node_labels[i].properties);
}
free(out->node_labels);
for (int i = 0; i < out->edge_type_count; i++) {
free((void *)out->edge_types[i].type);
+ for (int j = 0; j < out->edge_types[i].property_count; j++) {
+ free(out->edge_types[i].properties[j]);
+ }
+ free(out->edge_types[i].properties);
}
free(out->edge_types);
diff --git a/src/store/store.h b/src/store/store.h
index f15132c28..6fa855c3e 100644
--- a/src/store/store.h
+++ b/src/store/store.h
@@ -163,11 +163,15 @@ typedef struct {
typedef struct {
const char *label;
int count;
+ char **properties; /* distinct property keys for this label (base + JSON) */
+ int property_count;
} cbm_label_count_t;
typedef struct {
const char *type;
int count;
+ char **properties; /* distinct property keys for this edge type (base + JSON) */
+ int property_count;
} cbm_type_count_t;
typedef struct {