From 397f3f0f2dd03dea0a822eb1386d16a41f2fbed0 Mon Sep 17 00:00:00 2001 From: Aleksandrov Nikita Date: Mon, 16 Nov 2026 22:27:46 +0300 Subject: [PATCH 1/2] fix(mcp): ignore empty semantic queries and clarify mode conflicts Signed-off-by: Aleksandrov Nikita --- src/mcp/mcp.c | 19 +++++++++------ tests/test_mcp.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c index 419ed0d1f..5298f41c9 100644 --- a/src/mcp/mcp.c +++ b/src/mcp/mcp.c @@ -581,7 +581,8 @@ static const tool_def_t TOOLS[] = { "\"relationship\":{\"type\":\"string\"},\"min_degree\":{\"type\":\"integer\"}," "\"max_degree\":{\"type\":\"integer\"},\"exclude_entry_points\":{\"type\":\"boolean\"}," "\"include_connected\":{\"type\":\"boolean\"},\"semantic_query\":{" - "\"type\":\"array\",\"items\":{\"type\":\"string\"},\"description\":\"Not with query.\"}," + "\"type\":\"array\",\"items\":{\"type\":\"string\"},\"description\":\"Empty array is ignored. " + "For nonempty arrays, omit query entirely.\"}," "\"semantic_limit\":{\"type\":\"integer\",\"default\":50,\"minimum\":0," "\"maximum\":500}," "\"semantic_offset\":{\"type\":\"integer\",\"default\":0,\"minimum\":0," @@ -4794,7 +4795,7 @@ static sq_run_status_t run_semantic_query_core(const char *args, cbm_store_t *st yyjson_doc *args_doc = yyjson_read(args, strlen(args), 0); yyjson_val *args_root = args_doc ? yyjson_doc_get_root(args_doc) : NULL; yyjson_val *sq_val = args_root ? yyjson_obj_get(args_root, "semantic_query") : NULL; - if (out_present && sq_val) { + if (out_present && sq_val && (!yyjson_is_arr(sq_val) || yyjson_arr_size(sq_val) > 0)) { *out_present = true; } sq_run_status_t status = SQ_RUN_OK; @@ -4824,10 +4825,11 @@ static sq_run_status_t run_semantic_query_core(const char *args, cbm_store_t *st return status; } -static bool search_graph_arg_present(const char *args, const char *name) { +static bool search_graph_semantic_query_present(const char *args) { yyjson_doc *doc = yyjson_read(args, strlen(args), 0); yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL; - bool present = root && yyjson_is_obj(root) && yyjson_obj_get(root, name) != NULL; + yyjson_val *sq_val = root ? yyjson_obj_get(root, "semantic_query") : NULL; + bool present = sq_val && (!yyjson_is_arr(sq_val) || yyjson_arr_size(sq_val) > 0); if (doc) { yyjson_doc_free(doc); } @@ -5619,13 +5621,14 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) { * and return early. If FTS5 is unavailable or the query is empty after * tokenization, fall through to the regex path. */ char *query = cbm_mcp_get_string_arg(args, "query"); - if (query && search_graph_arg_present(args, "semantic_query")) { + if (query && search_graph_semantic_query_present(args)) { free(query); free(project); return cbm_mcp_text_result( - "query and semantic_query are mutually exclusive — use query for BM25 full-text " - "ranking or semantic_query for vector ranking, then issue a separate request for " - "the other mode.", + "query and semantic_query are mutually exclusive. For BM25 search, remove the " + "semantic_query key (an empty array is also accepted). For vector search, remove the " + "query key entirely. To run both modes, send two separate requests; do not retry " + "unchanged arguments.", true); } if (query && query[0]) { diff --git a/tests/test_mcp.c b/tests/test_mcp.c index 225d3e6fd..7c1f56e8f 100644 --- a/tests/test_mcp.c +++ b/tests/test_mcp.c @@ -5104,6 +5104,48 @@ TEST(tool_search_graph_bm25_ranks_exact_name_first) { PASS(); } +TEST(tool_search_graph_empty_semantic_query_is_ignored) { + cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL); + ASSERT_NOT_NULL(srv); + cbm_store_t *store = cbm_mcp_server_store(srv); + ASSERT_NOT_NULL(store); + cbm_mcp_server_set_project(srv, "bm25-find"); + bm25_findability_fixture(store, "bm25-find"); + + const char *cases[] = { + "\"query\":\"Table\",\"label\":\"Class\",", + "\"query\":\"Table\",\"semantic_limit\":0,", + "\"query\":\"\",", + "\"name_pattern\":\"^Table$\",", + "", + }; + const char *formats[] = {"json", "tree"}; + for (size_t f = 0; f < sizeof(formats) / sizeof(formats[0]); f++) { + for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + char args[512]; + snprintf(args, sizeof(args), "{\"project\":\"bm25-find\",%s\"format\":\"%s\"}", + cases[i], formats[f]); + char *baseline = cbm_mcp_handle_tool(srv, "search_graph", args); + ASSERT_NOT_NULL(baseline); + ASSERT_NULL(strstr(baseline, "\"isError\":true")); + ASSERT_NOT_NULL(strstr(baseline, "Table")); + + snprintf(args, sizeof(args), + "{\"project\":\"bm25-find\",%s\"semantic_query\":[],\"format\":\"%s\"}", + cases[i], formats[f]); + char *response = cbm_mcp_handle_tool(srv, "search_graph", args); + ASSERT_NOT_NULL(response); + ASSERT_NULL(strstr(response, "\"isError\":true")); + ASSERT_STR_EQ(response, baseline); + free(response); + free(baseline); + } + } + + cbm_mcp_server_free(srv); + PASS(); +} + /* `query` selects the BM25 fast path, which returns before semantic_query is * evaluated. Accepting both therefore used to report success while silently * discarding one of the caller's two requested result sets. The contract is @@ -5139,12 +5181,30 @@ TEST(tool_search_graph_rejects_bm25_and_semantic_query_together) { ASSERT_NOT_NULL(response); ASSERT_NOT_NULL(strstr(response, "\"isError\":true")); ASSERT_NOT_NULL(strstr(response, "query and semantic_query are mutually exclusive")); + ASSERT_NOT_NULL(strstr(response, "remove the semantic_query key")); + ASSERT_NOT_NULL(strstr(response, "remove the query key entirely")); + ASSERT_NOT_NULL(strstr(response, "do not retry unchanged arguments")); ASSERT_NULL(strstr(response, "\"search_mode\":\"bm25\"")); free(response); + const char *invalid[] = {"null", "\"meaning\"", "[42]", "[\"meaning\",42]"}; + for (size_t i = 0; i < sizeof(invalid) / sizeof(invalid[0]); i++) { + char args[256]; + snprintf(args, sizeof(args), + "{\"project\":\"mixed-search-modes\",\"query\":\"mixedneedle\"," + "\"semantic_query\":%s,\"format\":\"json\"}", + invalid[i]); + response = cbm_mcp_handle_tool(srv, "search_graph", args); + ASSERT_NOT_NULL(response); + ASSERT_NOT_NULL(strstr(response, "\"isError\":true")); + ASSERT_NULL(strstr(response, "\"search_mode\":\"bm25\"")); + free(response); + } + char *tools = cbm_mcp_tools_list(); ASSERT_NOT_NULL(tools); - ASSERT_NOT_NULL(strstr(tools, "Not with query")); + ASSERT_NOT_NULL(strstr(tools, "Empty array is ignored")); + ASSERT_NOT_NULL(strstr(tools, "omit query entirely")); free(tools); cbm_mcp_server_free(srv); @@ -28818,6 +28878,7 @@ SUITE(mcp) { RUN_TEST(tool_search_graph_bm25_post_window_filters_rank_every_candidate); RUN_TEST(tool_search_graph_bm25_empty_window_ranks_every_candidate); RUN_TEST(tool_search_graph_bm25_ranks_exact_name_first); + RUN_TEST(tool_search_graph_empty_semantic_query_is_ignored); RUN_TEST(tool_search_graph_rejects_bm25_and_semantic_query_together); RUN_TEST(tool_search_graph_semantic_ceiling_never_emits_unusable_continuation); RUN_TEST(tool_search_graph_semantic_pagination_is_lossless_and_independent); From 5772a50421243bacad8d8bc3962f11e30224e2d4 Mon Sep 17 00:00:00 2001 From: Aleksandrov Nikita Date: Mon, 16 Nov 2026 22:37:31 +0300 Subject: [PATCH 2/2] fix(mcp): keep semantic query guidance within metadata budget Signed-off-by: Aleksandrov Nikita --- src/mcp/mcp.c | 4 ++-- tests/test_mcp.c | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c index 5298f41c9..002b8fced 100644 --- a/src/mcp/mcp.c +++ b/src/mcp/mcp.c @@ -581,8 +581,8 @@ static const tool_def_t TOOLS[] = { "\"relationship\":{\"type\":\"string\"},\"min_degree\":{\"type\":\"integer\"}," "\"max_degree\":{\"type\":\"integer\"},\"exclude_entry_points\":{\"type\":\"boolean\"}," "\"include_connected\":{\"type\":\"boolean\"},\"semantic_query\":{" - "\"type\":\"array\",\"items\":{\"type\":\"string\"},\"description\":\"Empty array is ignored. " - "For nonempty arrays, omit query entirely.\"}," + "\"type\":\"array\",\"items\":{\"type\":\"string\"},\"description\":\"[] ignored; " + "otherwise omit query.\"}," "\"semantic_limit\":{\"type\":\"integer\",\"default\":50,\"minimum\":0," "\"maximum\":500}," "\"semantic_offset\":{\"type\":\"integer\",\"default\":0,\"minimum\":0," diff --git a/tests/test_mcp.c b/tests/test_mcp.c index 7c1f56e8f..de1c532cb 100644 --- a/tests/test_mcp.c +++ b/tests/test_mcp.c @@ -5203,8 +5203,7 @@ TEST(tool_search_graph_rejects_bm25_and_semantic_query_together) { char *tools = cbm_mcp_tools_list(); ASSERT_NOT_NULL(tools); - ASSERT_NOT_NULL(strstr(tools, "Empty array is ignored")); - ASSERT_NOT_NULL(strstr(tools, "omit query entirely")); + ASSERT_NOT_NULL(strstr(tools, "[] ignored; otherwise omit query.")); free(tools); cbm_mcp_server_free(srv);