Skip to content

Commit 108740d

Browse files
committed
fix(mcp): isolate semantic-only JSON search
The JSON search_graph path always ran the structural search, even when semantic_query was the only filter. Reuse the shared structural-filter check so semantic-only JSON calls retain semantic hits without returning unrelated graph nodes.\n\nAdd regression coverage for both default and JSON output. Signed-off-by: JJordan0K <69581081+JJordan0C@users.noreply.github.com>
1 parent d90986b commit 108740d

2 files changed

Lines changed: 76 additions & 38 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2995,22 +2995,6 @@ static bool run_semantic_query_core(const char *args, cbm_store_t *store, const
29952995
return type_error;
29962996
}
29972997

2998-
/* Append the semantic_query vector-search results onto the doc. Returns
2999-
* true if semantic_query was provided as a non-array (type error — caller
3000-
* should surface to the user). */
3001-
static bool run_semantic_query(yyjson_mut_doc *doc, yyjson_mut_val *root, const char *args,
3002-
cbm_store_t *store, const char *project, int limit) {
3003-
cbm_vector_result_t *vresults = NULL;
3004-
int vcount = 0;
3005-
bool type_error =
3006-
run_semantic_query_core(args, store, project, limit, &vresults, &vcount, NULL);
3007-
if (vcount > 0) {
3008-
emit_semantic_results(doc, root, vresults, vcount);
3009-
cbm_store_free_vector_results(vresults, vcount);
3010-
}
3011-
return type_error;
3012-
}
3013-
30142998
/* ── Tree output for search_graph ───────────────────────────────────
30152999
* Default response encoding: grouped tree rows (compact_out.h). The same
30163000
* model is available as structured JSON via format:"json"; include_connected
@@ -3445,6 +3429,9 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
34453429
.min_degree = min_degree,
34463430
.max_degree = max_degree,
34473431
};
3432+
bool has_structural_filters = label || name_pattern || qn_pattern || file_pattern ||
3433+
relationship || exclude_entry_points ||
3434+
min_degree != CBM_NOT_FOUND || max_degree != CBM_NOT_FOUND;
34483435

34493436
if (!legacy_json) {
34503437
const char *fields[SG_MAX_EXTRA_FIELDS];
@@ -3465,10 +3452,7 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
34653452
/* Semantic-only calls get semantic results only: the legacy
34663453
* behavior also ran the UNFILTERED regex search and prepended
34673454
* up to `limit` unrelated enriched nodes to the response. */
3468-
bool has_filters = label || name_pattern || qn_pattern || file_pattern ||
3469-
relationship || exclude_entry_points ||
3470-
min_degree != CBM_NOT_FOUND || max_degree != CBM_NOT_FOUND;
3471-
bool semantic_only = sq_present && !has_filters;
3455+
bool semantic_only = sq_present && !has_structural_filters;
34723456

34733457
cbm_sb_t sb;
34743458
cbm_sb_init(&sb);
@@ -3552,8 +3536,31 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
35523536
true);
35533537
}
35543538

3539+
cbm_vector_result_t *vresults = NULL;
3540+
int vcount = 0;
3541+
bool sq_present = false;
3542+
bool sq_type_error =
3543+
run_semantic_query_core(args, store, project, limit, &vresults, &vcount, &sq_present);
3544+
if (sq_type_error) {
3545+
free(project);
3546+
free(label);
3547+
free(name_pattern);
3548+
free(qn_pattern);
3549+
free(file_pattern);
3550+
free(relationship);
3551+
return cbm_mcp_text_result(
3552+
"semantic_query must be an array of keyword strings, e.g. "
3553+
"[\"send\",\"pubsub\",\"publish\"] — not a single string. Split your query "
3554+
"into individual keywords; each is scored independently via per-keyword "
3555+
"min-cosine.",
3556+
true);
3557+
}
3558+
3559+
bool semantic_only = sq_present && !has_structural_filters;
35553560
cbm_search_output_t out = {0};
3556-
cbm_store_search(store, &params, &out);
3561+
if (!semantic_only) {
3562+
cbm_store_search(store, &params, &out);
3563+
}
35573564

35583565
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
35593566
yyjson_mut_val *root = yyjson_mut_obj(doc);
@@ -3592,23 +3599,9 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
35923599
}
35933600
}
35943601

3595-
bool sq_type_error = run_semantic_query(doc, root, args, store, project, limit);
3596-
3597-
if (sq_type_error) {
3598-
yyjson_mut_doc_free(doc);
3599-
cbm_store_search_free(&out);
3600-
free(project);
3601-
free(label);
3602-
free(name_pattern);
3603-
free(qn_pattern);
3604-
free(file_pattern);
3605-
free(relationship);
3606-
return cbm_mcp_text_result(
3607-
"semantic_query must be an array of keyword strings, e.g. "
3608-
"[\"send\",\"pubsub\",\"publish\"] — not a single string. Split your query "
3609-
"into individual keywords; each is scored independently via per-keyword "
3610-
"min-cosine.",
3611-
true);
3602+
if (vcount > 0) {
3603+
emit_semantic_results(doc, root, vresults, vcount);
3604+
cbm_store_free_vector_results(vresults, vcount);
36123605
}
36133606

36143607
char *json = yy_doc_to_str(doc);

‎tests/test_mcp.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,50 @@ static cbm_mcp_server_t *setup_snippet_server(char *tmp_dir, size_t tmp_sz);
15521552
static void cleanup_snippet_dir(const char *tmp_dir);
15531553
static char *extract_text_content(const char *mcp_result);
15541554

1555+
TEST(tool_search_graph_semantic_only_skips_structural_results_issue1295) {
1556+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
1557+
ASSERT_NOT_NULL(srv);
1558+
cbm_store_t *st = cbm_mcp_server_store(srv);
1559+
const char *proj = "semantic-only";
1560+
cbm_mcp_server_set_project(srv, proj);
1561+
cbm_store_upsert_project(st, proj, "/tmp/semantic-only");
1562+
1563+
cbm_node_t unrelated = {.project = proj,
1564+
.label = "Function",
1565+
.name = "unrelated_node",
1566+
.qualified_name = "semantic-only.unrelated_node",
1567+
.file_path = "unrelated.c",
1568+
.start_line = 1,
1569+
.end_line = 2};
1570+
ASSERT_GT(cbm_store_upsert_node(st, &unrelated), 0);
1571+
1572+
char *resp = cbm_mcp_handle_tool(
1573+
srv, "search_graph",
1574+
"{\"project\":\"semantic-only\",\"semantic_query\":[\"publish\"],\"limit\":5}");
1575+
ASSERT_NOT_NULL(resp);
1576+
char *inner = extract_text_content(resp);
1577+
free(resp);
1578+
ASSERT_NOT_NULL(inner);
1579+
ASSERT_NULL(strstr(inner, "unrelated_node"));
1580+
free(inner);
1581+
1582+
resp = cbm_mcp_handle_tool(srv, "search_graph",
1583+
"{\"project\":\"semantic-only\",\"semantic_query\":[\"publish\"],"
1584+
"\"format\":\"json\",\"limit\":5}");
1585+
ASSERT_NOT_NULL(resp);
1586+
inner = extract_text_content(resp);
1587+
free(resp);
1588+
ASSERT_NOT_NULL(inner);
1589+
ASSERT_NOT_NULL(strstr(inner, "\"total\":0"));
1590+
ASSERT_NOT_NULL(strstr(inner, "\"count\":0"));
1591+
ASSERT_NOT_NULL(strstr(inner, "\"groups\":[]"));
1592+
ASSERT_NULL(strstr(inner, "unrelated_node"));
1593+
free(inner);
1594+
1595+
cbm_mcp_server_free(srv);
1596+
PASS();
1597+
}
1598+
15551599
/* callers_total/callees_total must count what the caller can enumerate: with
15561600
* include_tests=false (default) test-file rows are hidden from the table, so
15571601
* the totals must apply the same filter — a raw visited_count overstated the
@@ -9575,6 +9619,7 @@ SUITE(mcp) {
95759619
RUN_TEST(tool_get_graph_schema_empty);
95769620
RUN_TEST(tool_unknown_tool);
95779621
RUN_TEST(tool_search_graph_basic);
9622+
RUN_TEST(tool_search_graph_semantic_only_skips_structural_results_issue1295);
95789623
RUN_TEST(tool_trace_totals_respect_test_filter);
95799624
RUN_TEST(tool_get_architecture_cycles_detects_scc);
95809625
RUN_TEST(tool_get_code_snippet_clips_whole_file_node);

0 commit comments

Comments
 (0)