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(mcp): infra filters for search_graph and a topology trace mode

Martin Vogel722a7334ec54
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 5834ac89c..63b9e8a35 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -562,6 +562,9 @@ static const tool_def_t TOOLS[] = {
"\"query\":{\"type\":\"string\"},"
"\"label\":{\"type\":\"string\"},\"name_pattern\":{\"type\":\"string\"},\"qn_pattern\":{"
"\"type\":\"string\"},\"file_pattern\":{\"type\":\"string\"},"
+ "\"kind\":{\"type\":\"string\",\"description\":\"Infra object kind of a Resource, e.g. "
+ "Deployment, HelmRelease.\"},\"namespace\":{\"type\":\"string\",\"description\":"
+ "\"Effective Kubernetes namespace of a Resource.\"},"
"\"relationship\":{\"type\":\"string\"},\"min_degree\":{\"type\":\"integer\"},"
"\"max_degree\":{\"type\":\"integer\"},\"exclude_entry_points\":{\"type\":\"boolean\"},"
"\"include_connected\":{\"type\":\"boolean\"},\"semantic_query\":{"
@@ -619,8 +622,9 @@ static const tool_def_t TOOLS[] = {
"\"cursor\":{\"type\":\"string\",\"description\":\"Pass next/next_cursor with traversal "
"args unchanged; budget may increase.\"},"
"\"mode\":{"
- "\"type\":\"string\",\"enum\":[\"calls\",\"data_flow\",\"cross_service\"],\"default\":"
- "\"calls\",\"description\":\"calls, argument-aware data_flow, or service edges.\"},"
+ "\"type\":\"string\",\"enum\":[\"calls\",\"data_flow\",\"cross_service\",\"topology\"],"
+ "\"default\":\"calls\",\"description\":\"calls, argument-aware data_flow, service edges, "
+ "or topology: infra objects, routes and controllers (start at a Resource or Route).\"},"
"\"parameter_name\":{\"type\":\"string\",\"description\":\"data_flow parameter filter.\"},"
"\"edge_types\":{\"type\":\"array\",\"items\":{"
"\"type\":\"string\"}},\"risk_labels\":{\"type\":\"boolean\",\"default\":false,"
@@ -1531,6 +1535,23 @@ char *cbm_mcp_get_string_arg(const char *args_json, const char *key) {
return result;
}
+/* String argument `key` copied into `buf` (truncated to fit): buf, or NULL
+ * when absent or empty. Nothing to free. */
+static const char *mcp_string_arg_buf(const char *args_json, const char *key, char *buf,
+ size_t buf_sz) {
+ buf[0] = '\0';
+ yyjson_doc *doc = yyjson_read(args_json, strlen(args_json), 0);
+ if (!doc) {
+ return NULL;
+ }
+ yyjson_val *val = yyjson_obj_get(yyjson_doc_get_root(doc), key);
+ if (val && yyjson_is_str(val)) {
+ snprintf(buf, buf_sz, "%s", yyjson_get_str(val));
+ }
+ yyjson_doc_free(doc);
+ return buf[0] ? buf : NULL;
+}
+
static char *canonicalize_repo_path_if_exists(char *repo_path) {
if (!repo_path) {
return NULL;
@@ -3918,6 +3939,8 @@ enum {
BM25_BIND_FILE = 6,
BM25_BIND_LABEL = 7,
BM25_BIND_EXACT = 8,
+ BM25_BIND_KIND = 9,
+ BM25_BIND_NS = 10,
BM25_SQL_AUTO_LEN = -1,
/* Inner FTS5 candidate cap. SQLite can early-terminate a plain FTS5 query
* (no JOIN/WHERE on outer table) of the form:
@@ -4175,8 +4198,9 @@ static char *bm25_render(const bm25_output_row_t *rows, int returned, int total,
* Returns NULL if FTS5 is unavailable or the query produced no usable tokens,
* in which case the caller falls back to the regex-based search path. */
static char *bm25_search(cbm_store_t *store, const char *project, const char *query,
- const char *file_pattern, const char *label, int limit, int offset,
- bool tree_format, size_t max_output_bytes) {
+ const char *file_pattern, const char *label, const char *kind,
+ const char *ns, int limit, int offset, bool tree_format,
+ size_t max_output_bytes) {
sqlite3 *db = cbm_store_get_db(store);
if (!db) {
return NULL;
@@ -4239,6 +4263,14 @@ static char *bm25_search(cbm_store_t *store, const char *project, const char *qu
* in the structural mode (2026-09-16 probe: `label=Class` was ignored
* here and five Methods came back). MIRRORED in the count query. */
" AND (?7 IS NULL OR n.label = ?7) "
+ /* Infra filters, guarded (json_extract raises on malformed JSON).
+ * MIRRORED in the count query. */
+ " AND (?9 IS NULL OR (CASE WHEN json_valid(n.properties) THEN "
+ " json_extract(n.properties,'$.kind') END) = ?9) "
+ " AND (?10 IS NULL OR (CASE WHEN json_valid(n.properties) THEN "
+ " json_extract(n.properties,'$.obj_key') END) LIKE ('%/%/' || "
+ " replace(replace(replace(?10,'\\','\\\\'),'%','\\%'),'_','\\_') || '/%') "
+ " ESCAPE '\\') "
/* rank ties are common (boosted floats) — the id tie-break makes
* offset pages contractually stable across calls. */
"ORDER BY rank, n.id "
@@ -4265,25 +4297,42 @@ static char *bm25_search(cbm_store_t *store, const char *project, const char *qu
sqlite3_bind_null(stmt, BM25_BIND_LABEL);
}
sqlite3_bind_text(stmt, BM25_BIND_EXACT, query, BM25_SQL_AUTO_LEN, MCP_SQLITE_TRANSIENT);
+ if (kind && kind[0]) {
+ sqlite3_bind_text(stmt, BM25_BIND_KIND, kind, BM25_SQL_AUTO_LEN, MCP_SQLITE_TRANSIENT);
+ } else {
+ sqlite3_bind_null(stmt, BM25_BIND_KIND);
+ }
+ if (ns && ns[0]) {
+ sqlite3_bind_text(stmt, BM25_BIND_NS, ns, BM25_SQL_AUTO_LEN, MCP_SQLITE_TRANSIENT);
+ } else {
+ sqlite3_bind_null(stmt, BM25_BIND_NS);
+ }
/* Count hits within the same inner-limit window — capped at BM25_INNER_LIMIT.
* Uses the identical subquery structure so the FTS5 early-exit applies here too. */
int total = 0;
{
- const char *count_sql = "SELECT COUNT(*) FROM ("
- " SELECT fts.rowid FROM ("
- " SELECT rowid FROM nodes_fts WHERE nodes_fts MATCH ?1"
- " ORDER BY " BM25_WEIGHTS " LIMIT ?3"
- " ) fts "
- " JOIN nodes n ON n.id = fts.rowid "
- " WHERE n.project = ?2 "
- /* MIRRORS the ranked query's filter verbatim — same weights, same
- * label exclusions. Changing one alone reports a total that does
- * not describe the rows returned. */
- " AND n.label NOT IN ('File','Folder','Variable','Project')"
- " AND (?6 IS NULL OR n.file_path LIKE ?6)"
- " AND (?7 IS NULL OR n.label = ?7)"
- ")";
+ const char *count_sql =
+ "SELECT COUNT(*) FROM ("
+ " SELECT fts.rowid FROM ("
+ " SELECT rowid FROM nodes_fts WHERE nodes_fts MATCH ?1"
+ " ORDER BY " BM25_WEIGHTS " LIMIT ?3"
+ " ) fts "
+ " JOIN nodes n ON n.id = fts.rowid "
+ " WHERE n.project = ?2 "
+ /* MIRRORS the ranked query's filter verbatim — same weights, same
+ * label exclusions. Changing one alone reports a total that does
+ * not describe the rows returned. */
+ " AND n.label NOT IN ('File','Folder','Variable','Project')"
+ " AND (?6 IS NULL OR n.file_path LIKE ?6)"
+ " AND (?7 IS NULL OR n.label = ?7)"
+ " AND (?9 IS NULL OR (CASE WHEN json_valid(n.properties) "
+ " THEN json_extract(n.properties,'$.kind') END) = ?9)"
+ " AND (?10 IS NULL OR (CASE WHEN json_valid(n.properties) "
+ " THEN json_extract(n.properties,'$.obj_key') END) "
+ " LIKE ('%/%/' || replace(replace(replace(?10,'\\','\\\\'),'%','\\%'),"
+ " '_','\\_') || '/%') ESCAPE '\\')"
+ ")";
sqlite3_stmt *cs = NULL;
if (sqlite3_prepare_v2(db, count_sql, BM25_SQL_AUTO_LEN, &cs, NULL) == SQLITE_OK) {
sqlite3_bind_text(cs, BM25_BIND_QUERY, fts_query, BM25_SQL_AUTO_LEN,
@@ -4303,6 +4352,17 @@ static char *bm25_search(cbm_store_t *store, const char *project, const char *qu
} else {
sqlite3_bind_null(cs, BM25_BIND_LABEL);
}
+ if (kind && kind[0]) {
+ sqlite3_bind_text(cs, BM25_BIND_KIND, kind, BM25_SQL_AUTO_LEN,
+ MCP_SQLITE_TRANSIENT);
+ } else {
+ sqlite3_bind_null(cs, BM25_BIND_KIND);
+ }
+ if (ns && ns[0]) {
+ sqlite3_bind_text(cs, BM25_BIND_NS, ns, BM25_SQL_AUTO_LEN, MCP_SQLITE_TRANSIENT);
+ } else {
+ sqlite3_bind_null(cs, BM25_BIND_NS);
+ }
if (sqlite3_step(cs) == SQLITE_ROW) {
total = sqlite3_column_int(cs, 0);
}
@@ -5281,8 +5341,12 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
if (query && query[0]) {
char *q_file_pattern = cbm_mcp_get_string_arg(args, "file_pattern");
char *q_label = cbm_mcp_get_string_arg(args, "label");
- char *bm25_json = bm25_search(store, project, query, q_file_pattern, q_label, limit, offset,
- !json_format, max_output_bytes);
+ char q_kind_buf[CBM_SZ_256];
+ char q_ns_buf[CBM_SZ_256];
+ const char *q_kind = mcp_string_arg_buf(args, "kind", q_kind_buf, sizeof(q_kind_buf));
+ const char *q_ns = mcp_string_arg_buf(args, "namespace", q_ns_buf, sizeof(q_ns_buf));
+ char *bm25_json = bm25_search(store, project, query, q_file_pattern, q_label, q_kind, q_ns,
+ limit, offset, !json_format, max_output_bytes);
free(q_file_pattern);
free(q_label);
if (bm25_json) {
@@ -5296,6 +5360,10 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
free(query);
char *label = cbm_mcp_get_string_arg(args, "label");
+ char kind_buf[CBM_SZ_256];
+ char ns_buf[CBM_SZ_256];
+ const char *kind_arg = mcp_string_arg_buf(args, "kind", kind_buf, sizeof(kind_buf));
+ const char *ns_arg = mcp_string_arg_buf(args, "namespace", ns_buf, sizeof(ns_buf));
char *name_pattern = cbm_mcp_get_string_arg(args, "name_pattern");
char *qn_pattern = cbm_mcp_get_string_arg(args, "qn_pattern");
char *file_pattern = cbm_mcp_get_string_arg(args, "file_pattern");
@@ -5328,6 +5396,8 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
.offset = offset,
.min_degree = min_degree,
.max_degree = max_degree,
+ .kind = kind_arg,
+ .namespace_name = ns_arg,
};
const char *fields[SG_MAX_EXTRA_FIELDS];
yyjson_doc *fields_owner = NULL;
@@ -8589,6 +8659,11 @@ static yyjson_doc *resolve_trace_edge_types(const char *args, const char *mode,
"HTTP_CALLS", "ASYNC_CALLS", "DATA_FLOWS", "CALLS",
"CROSS_HTTP_CALLS", "CROSS_ASYNC_CALLS", "CROSS_CHANNEL", "CROSS_GRPC_CALLS",
"CROSS_GRAPHQL_CALLS", "CROSS_TRPC_CALLS"};
+ /* topology: traffic (INFRA_MAPS), object references (USAGE), ordering
+ * (DEPENDS_ON), controllers (HANDLES) and what an object declares
+ * (DEFINES: an Ingress's routes) */
+ static const char *mode_topology[] = {"INFRA_MAPS", "USAGE", "DEPENDS_ON", "HANDLES",
+ "DEFINES"};
*out_count = 0;
@@ -8621,6 +8696,9 @@ static yyjson_doc *resolve_trace_edge_types(const char *args, const char *mode,
} else if (mode && strcmp(mode, "cross_service") == 0) {
defaults = mode_cross_svc;
n_defaults = (int)(sizeof(mode_cross_svc) / sizeof(mode_cross_svc[0]));
+ } else if (mode && strcmp(mode, "topology") == 0) {
+ defaults = mode_topology;
+ n_defaults = (int)(sizeof(mode_topology) / sizeof(mode_topology[0]));
}
for (int i = 0; i < n_defaults; i++) {
out_types[i] = defaults[i];
diff --git a/src/store/store.c b/src/store/store.c
index 4f76915d5..c08e3988c 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -6542,6 +6542,33 @@ static void where_add_like_hints(const char *column, const char *pattern, char *
}
}
+/* Infra filters: a Resource's kind property and the namespace of its
+ * obj_key "group/Kind/ns/name" (no part holds a '/', so `%/%/<ns>/%` pins
+ * the third; the pattern is built in SQL from the bound name, LIKE
+ * metacharacters escaped). Properties are guarded: json_extract raises on
+ * malformed JSON. */
+static void search_where_infra(const cbm_search_params_t *params, char *where, int where_sz,
+ int *wlen, int *nparams, search_bind_t *binds, int *bind_idx) {
+ char clause[CBM_SZ_256];
+ if (params->kind && params->kind[0]) {
+ snprintf(clause, sizeof(clause),
+ "(CASE WHEN json_valid(n.properties) THEN json_extract(n.properties,'$.kind') "
+ "END) = ?%d",
+ *bind_idx + SKIP_ONE);
+ *wlen = where_append(where, where_sz, *wlen, nparams, clause);
+ where_bind_text(binds, bind_idx, params->kind);
+ }
+ if (params->namespace_name && params->namespace_name[0]) {
+ snprintf(clause, sizeof(clause),
+ "(CASE WHEN json_valid(n.properties) THEN json_extract(n.properties,'$.obj_key') "
+ "END) LIKE ('%%/%%/' || replace(replace(replace(?%d,'\\','\\\\'),'%%','\\%%'),"
+ "'_','\\_') || '/%%') ESCAPE '\\'",
+ *bind_idx + SKIP_ONE);
+ *wlen = where_append(where, where_sz, *wlen, nparams, clause);
+ where_bind_text(binds, bind_idx, params->namespace_name);
+ }
+}
+
/* Build basic WHERE clauses: project, label, name, file, qn patterns. */
static int search_where_basic(const cbm_search_params_t *params, char *where, int where_sz,
int *wlen, int *nparams, search_bind_t *binds, int *bind_idx,
@@ -6601,6 +6628,7 @@ static int search_where_basic(const cbm_search_params_t *params, char *where, in
where_bind_text(binds, bind_idx, lp);
}
}
+ search_where_infra(params, where, where_sz, wlen, nparams, binds, bind_idx);
return *nparams;
}
diff --git a/src/store/store.h b/src/store/store.h
index d64041192..15d02dc80 100644
--- a/src/store/store.h
+++ b/src/store/store.h
@@ -231,6 +231,8 @@ typedef struct {
const char *sort_by; /* "relevance" / "name" / "degree", NULL = relevance */
bool case_sensitive;
const char **exclude_labels; /* NULL-terminated array, or NULL */
+ const char *kind; /* a Resource's kind property ("Deployment"), NULL = any */
+ const char *namespace_name; /* the namespace of a Resource's obj_key, NULL = any */
} cbm_search_params_t;
typedef struct {
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index ec87497fd..8dbce1871 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -1561,6 +1561,100 @@ TEST(probe_k8s_ingress_and_httproute_expose_routes) {
PASS();
}
+/* ── MCP surface: infra filters + topology (P1.5) ───────────────────── */
+
+/* Run MCP `tool` on the indexed fixture; `args_fmt` holds one %s, the
+ * project. The response is the caller's to free; NULL when indexing failed. */
+static char *gpf_tool_on(const GpfFile *files, int nfiles, const char *tool,
+ const char *args_fmt) {
+ GpfProj lp;
+ cbm_store_t *store = gpf_index_files(&lp, files, nfiles);
+ char *resp = NULL;
+ if (store && lp.srv) {
+ char args[2048];
+ snprintf(args, sizeof(args), args_fmt, lp.project);
+ resp = cbm_mcp_handle_tool(lp.srv, tool, args);
+ }
+ gpf_cleanup(&lp, store);
+ return resp;
+}
+
+static const GpfFile k_two_teams[] = {
+ {"k8s/a.yaml",
+ "apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: web\n namespace: team-a\n---\n"
+ "apiVersion: v1\nkind: Service\nmetadata:\n name: web\n namespace: team-a\n"},
+ {"k8s/b.yaml",
+ "apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: api\n namespace: team-b\n"}};
+
+/* search_graph narrows Resources by kind and by effective namespace, in the
+ * structural and the BM25 query mode alike. */
+TEST(probe_mcp_search_filters_kind_and_namespace) {
+ char *r = gpf_tool_on(k_two_teams, 2, "search_graph",
+ "{\"project\":\"%s\",\"label\":\"Resource\",\"kind\":\"Deployment\","
+ "\"format\":\"json\"}");
+ ASSERT_NOT_NULL(r);
+ ASSERT_NOT_NULL(strstr(r, "Deployment/team-a/web"));
+ ASSERT_NOT_NULL(strstr(r, "Deployment/team-b/api"));
+ ASSERT_NULL(strstr(r, "Service/team-a/web"));
+ free(r);
+ r = gpf_tool_on(k_two_teams, 2, "search_graph",
+ "{\"project\":\"%s\",\"label\":\"Resource\",\"namespace\":\"team-a\","
+ "\"format\":\"json\"}");
+ ASSERT_NOT_NULL(r);
+ ASSERT_NOT_NULL(strstr(r, "Deployment/team-a/web"));
+ ASSERT_NOT_NULL(strstr(r, "Service/team-a/web"));
+ ASSERT_NULL(strstr(r, "Deployment/team-b/api"));
+ free(r);
+ r = gpf_tool_on(k_two_teams, 2, "search_graph",
+ "{\"project\":\"%s\",\"query\":\"web\",\"kind\":\"Service\","
+ "\"format\":\"json\"}");
+ ASSERT_NOT_NULL(r);
+ ASSERT_NOT_NULL(strstr(r, "Service/team-a/web"));
+ ASSERT_NULL(strstr(r, "Deployment/team-a/web"));
+ free(r);
+ PASS();
+}
+
+/* trace_path mode=topology walks infra edges: from a Service out to its
+ * workload and the ConfigMap the workload mounts, and in to the exposed
+ * route and the Ingress that declares it. The call graph modes do not. */
+TEST(probe_mcp_trace_topology) {
+ static const char doc[] =
+ "apiVersion: networking.k8s.io/v1\nkind: Ingress\nmetadata:\n name: shop\nspec:\n"
+ " rules:\n - host: shop.example.com\n http:\n paths:\n"
+ " - path: /api\n pathType: Prefix\n backend:\n"
+ " service:\n name: api\n port:\n"
+ " number: 80\n---\n"
+ "apiVersion: v1\nkind: Service\nmetadata:\n name: api\nspec:\n selector:\n"
+ " app: api\n---\n"
+ "apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: api\nspec:\n template:\n"
+ " metadata:\n labels:\n app: api\n spec:\n volumes:\n"
+ " - name: cfg\n configMap:\n name: api-config\n---\n"
+ "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: api-config\n";
+ static const GpfFile files[] = {{"k8s/shop.yaml", doc}};
+ char *r = gpf_tool_on(files, 1, "trace_path",
+ "{\"function_name\":\"Service/api\",\"project\":\"%s\","
+ "\"direction\":\"outbound\",\"mode\":\"topology\",\"format\":\"json\"}");
+ ASSERT_NOT_NULL(r);
+ ASSERT_NOT_NULL(strstr(r, "Deployment/-/api"));
+ ASSERT_NOT_NULL(strstr(r, "ConfigMap/-/api-config"));
+ free(r);
+ r = gpf_tool_on(files, 1, "trace_path",
+ "{\"function_name\":\"Service/api\",\"project\":\"%s\","
+ "\"direction\":\"inbound\",\"mode\":\"topology\",\"format\":\"json\"}");
+ ASSERT_NOT_NULL(r);
+ ASSERT_NOT_NULL(strstr(r, "__route__ingress__shop.example")); /* qn groups split at a dot */
+ ASSERT_NOT_NULL(strstr(r, "Ingress/-/shop"));
+ free(r);
+ r = gpf_tool_on(files, 1, "trace_path",
+ "{\"function_name\":\"Service/api\",\"project\":\"%s\","
+ "\"direction\":\"outbound\",\"format\":\"json\"}");
+ ASSERT_NOT_NULL(r);
+ ASSERT_NULL(strstr(r, "Deployment/-/api"));
+ free(r);
+ PASS();
+}
+
/* A CRD vendored twice (gardener: example/seed-crds and a chart): a custom
* resource is an instance of the copy deployed with it; only when no copy
* shares its bundle does it stand for all of them. */
@@ -3172,6 +3266,8 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_k8s_pdb_and_networkpolicy_select_workloads);
RUN_TEST(probe_k8s_selectors_pick_their_own_version);
RUN_TEST(probe_k8s_ingress_and_httproute_expose_routes);
+ RUN_TEST(probe_mcp_search_filters_kind_and_namespace);
+ RUN_TEST(probe_mcp_trace_topology);
RUN_TEST(probe_k8s_selector_stays_in_its_directory);
RUN_TEST(probe_k8s_selector_within_kustomize_build);
RUN_TEST(probe_k8s_kustomize_namespace_is_effective);