Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/mcp/mcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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\":\"[] ignored; "
"otherwise omit query.\"},"
"\"semantic_limit\":{\"type\":\"integer\",\"default\":50,\"minimum\":0,"
"\"maximum\":500},"
"\"semantic_offset\":{\"type\":\"integer\",\"default\":0,\"minimum\":0,"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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]) {
Expand Down
62 changes: 61 additions & 1 deletion tests/test_mcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -5139,12 +5181,29 @@ 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, "[] ignored; otherwise omit query."));
free(tools);

cbm_mcp_server_free(srv);
Expand Down Expand Up @@ -28818,6 +28877,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);
Expand Down
Loading