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.

Improve error handling, diagnostics, and cancellation across MCP tools and CLI

Martin Vogel99723b3626f0
diff --git a/src/main.c b/src/main.c
index 0c7c710b7..54dbfb62a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -40,6 +40,7 @@ enum {
#include "ui/config.h"
#include "ui/http_server.h"
#include "ui/embedded_assets.h"
+#include <yyjson/yyjson.h>
#include <stdio.h>
#include <stdlib.h>
@@ -61,6 +62,17 @@ static atomic_int g_shutdown = 0;
static void signal_handler(int sig) {
(void)sig;
atomic_store(&g_shutdown, 1);
+
+ /* Cancel any in-progress pipeline (async-signal-safe: only does atomic_store) */
+ if (g_server) {
+ cbm_pipeline_t *p = cbm_mcp_server_active_pipeline(g_server);
+ if (p) {
+ cbm_pipeline_cancel(p);
+ }
+ }
+ /* Release pipeline lock to prevent stale lock on restart */
+ cbm_pipeline_unlock();
+
if (g_watcher) {
cbm_watcher_stop(g_watcher);
}
@@ -94,6 +106,11 @@ static void *http_thread(void *arg) {
static int watcher_index_fn(const char *project_name, const char *root_path, void *user_data) {
(void)user_data;
+ /* Skip indexing if shutdown is in progress */
+ if (atomic_load(&g_shutdown)) {
+ return 0;
+ }
+
/* Non-blocking: skip if another pipeline is already running.
* Watcher will retry on next poll cycle (5-60s). */
if (!cbm_pipeline_try_lock()) {
@@ -117,29 +134,65 @@ static int watcher_index_fn(const char *project_name, const char *root_path, voi
/* ── CLI mode ───────────────────────────────────────────────────── */
+#define CLI_USAGE "Usage: codebase-memory-mcp cli [--progress] [--json] <tool_name> [json_args]\n"
+
+/* Extract text content from MCP tool result envelope and print it.
+ * MCP results: {"content":[{"type":"text","text":"..."}],"isError":...}
+ * Returns 1 if the result was an error, 0 otherwise. */
+static int cli_print_mcp_result(const char *result) {
+ yyjson_doc *doc = yyjson_read(result, strlen(result), 0);
+ if (!doc) {
+ printf("%s\n", result);
+ return 0;
+ }
+
+ yyjson_val *root = yyjson_doc_get_root(doc);
+ yyjson_val *err_val = yyjson_obj_get(root, "isError");
+ bool is_error = err_val && yyjson_get_bool(err_val);
+
+ const char *text = NULL;
+ yyjson_val *content = yyjson_obj_get(root, "content");
+ if (yyjson_is_arr(content) && yyjson_arr_size(content) > 0) {
+ yyjson_val *tv = yyjson_obj_get(yyjson_arr_get_first(content), "text");
+ text = tv ? yyjson_get_str(tv) : NULL;
+ }
+
+ if (text) {
+ (void)fprintf(is_error ? stderr : stdout, "%s\n", text);
+ } else {
+ printf("%s\n", result);
+ }
+
+ yyjson_doc_free(doc);
+ return is_error ? SKIP_ONE : 0;
+}
+
+/* Strip a flag from argv, returning true if found. */
+static bool cli_strip_flag(int *argc, char **argv, const char *flag) {
+ for (int i = 0; i < *argc; i++) {
+ if (strcmp(argv[i], flag) != 0) {
+ continue;
+ }
+ for (int j = i; j < *argc - SKIP_ONE; j++) {
+ argv[j] = argv[j + SKIP_ONE];
+ }
+ (*argc)--;
+ return true;
+ }
+ return false;
+}
+
static int run_cli(int argc, char **argv) {
if (argc < MAIN_MIN_ARGC) {
- (void)fprintf(stderr,
- "Usage: codebase-memory-mcp cli [--progress] <tool_name> [json_args]\n");
+ (void)fprintf(stderr, CLI_USAGE);
return SKIP_ONE;
}
- /* Strip --progress flag from argv. */
- bool progress = false;
- for (int i = 0; i < argc; i++) {
- if (strcmp(argv[i], "--progress") == 0) {
- progress = true;
- for (int j = i; j < argc - SKIP_ONE; j++) {
- argv[j] = argv[j + SKIP_ONE];
- }
- argc--;
- break;
- }
- }
+ bool progress = cli_strip_flag(&argc, argv, "--progress");
+ bool raw_json = cli_strip_flag(&argc, argv, "--json");
if (argc < MAIN_MIN_ARGC) {
- (void)fprintf(stderr,
- "Usage: codebase-memory-mcp cli [--progress] <tool_name> [json_args]\n");
+ (void)fprintf(stderr, CLI_USAGE);
return SKIP_ONE;
}
@@ -152,7 +205,7 @@ static int run_cli(int argc, char **argv) {
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
if (!srv) {
- (void)fprintf(stderr, "Failed to create server\n");
+ (void)fprintf(stderr, "error: failed to create server\n");
if (progress) {
cbm_progress_sink_fini();
}
@@ -160,8 +213,14 @@ static int run_cli(int argc, char **argv) {
}
char *result = cbm_mcp_handle_tool(srv, tool_name, args_json);
+ int exit_code = 0;
+
if (result) {
- printf("%s\n", result);
+ if (raw_json) {
+ printf("%s\n", result);
+ } else {
+ exit_code = cli_print_mcp_result(result);
+ }
free(result);
}
@@ -169,7 +228,7 @@ static int run_cli(int argc, char **argv) {
if (progress) {
cbm_progress_sink_fini();
}
- return 0;
+ return exit_code;
}
/* ── Help ───────────────────────────────────────────────────────── */
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 3372826b5..310034f0f 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -69,6 +69,7 @@ enum {
#include <string.h>
#include <sys/stat.h>
#include <time.h>
+#include <errno.h>
/* ── Constants ────────────────────────────────────────────────── */
@@ -573,6 +574,10 @@ struct cbm_mcp_server {
struct cbm_config *config; /* external config ref (not owned) */
cbm_thread_t autoindex_tid;
bool autoindex_active; /* true if auto-index thread was started */
+
+ /* Active pipeline tracking for cancellation support */
+ cbm_pipeline_t *active_pipeline; /* non-NULL while index_repository runs */
+ int64_t active_request_id; /* JSON-RPC id of the in-progress tool call */
};
cbm_mcp_server_t *cbm_mcp_server_new(const char *store_path) {
@@ -665,6 +670,10 @@ bool cbm_mcp_server_has_cached_store(cbm_mcp_server_t *srv) {
return (srv && srv->store != NULL) != 0;
}
+cbm_pipeline_t *cbm_mcp_server_active_pipeline(cbm_mcp_server_t *srv) {
+ return srv ? srv->active_pipeline : NULL;
+}
+
/* ── Cache dir + project DB path helpers ───────────────────────── */
/* Returns the cache directory. Writes to buf, returns buf for convenience. */
@@ -886,27 +895,41 @@ static char *handle_list_projects(cbm_mcp_server_t *srv, const char *args) {
yyjson_mut_doc_set_root(doc, root);
yyjson_mut_val *arr = yyjson_mut_arr(doc);
- if (d) {
- cbm_dirent_t *entry;
- while ((entry = cbm_readdir(d)) != NULL) {
- const char *name = entry->name;
- size_t len = strlen(name);
- if (!is_project_db_file(name, len)) {
- continue;
- }
- char full_path[CBM_SZ_2K];
- snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, name);
- struct stat st;
- if (stat(full_path, &st) != 0) {
- continue;
- }
- build_project_json_entry(doc, arr, dir_path, name, len, &st);
+ if (!d) {
+ char msg[CBM_SZ_1K];
+ snprintf(msg, sizeof(msg),
+ "{\"error\":\"cannot read cache directory: %s\",\"hint\":"
+ "\"Check directory permissions or run index_repository first.\"}",
+ dir_path);
+ yyjson_mut_doc_free(doc);
+ return cbm_mcp_text_result(msg, true);
+ }
+
+ cbm_dirent_t *entry;
+ while ((entry = cbm_readdir(d)) != NULL) {
+ const char *name = entry->name;
+ size_t len = strlen(name);
+ if (!is_project_db_file(name, len)) {
+ continue;
+ }
+ char full_path[CBM_SZ_2K];
+ snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, name);
+ struct stat st;
+ if (stat(full_path, &st) != 0) {
+ continue;
}
- cbm_closedir(d);
+ build_project_json_entry(doc, arr, dir_path, name, len, &st);
}
+ cbm_closedir(d);
yyjson_mut_obj_add_val(doc, root, "projects", arr);
+ /* Guide user when no projects are indexed */
+ if (yyjson_mut_arr_size(arr) == 0) {
+ yyjson_mut_obj_add_str(doc, root, "hint",
+ "No projects indexed. Call index_repository(repo_path=...) first.");
+ }
+
char *json = yy_doc_to_str(doc);
yyjson_mut_doc_free(doc);
@@ -926,8 +949,10 @@ static char *handle_list_projects(cbm_mcp_server_t *srv, const char *args) {
static char *verify_project_indexed(cbm_store_t *store, const char *project) {
cbm_project_t proj_check = {0};
if (cbm_store_get_project(store, project, &proj_check) != CBM_STORE_OK) {
- return cbm_mcp_text_result(
- "{\"error\":\"project not indexed — run index_repository first\"}", true);
+ char *err = build_project_list_error("project not indexed — run index_repository first");
+ char *res = cbm_mcp_text_result(err, true);
+ free(err);
+ return res;
}
cbm_project_free_fields(&proj_check);
return NULL;
@@ -1379,6 +1404,25 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
yyjson_mut_doc_set_root(doc, root);
emit_search_results(doc, root, &out, store, relationship, include_connected, offset);
+
+ /* Add diagnostic hint when zero results */
+ if (out.total == 0) {
+ if (name_pattern && label) {
+ yyjson_mut_obj_add_str(
+ doc, root, "hint",
+ "No results. Try removing the label filter or broadening the name_pattern regex.");
+ } else if (name_pattern) {
+ yyjson_mut_obj_add_str(
+ doc, root, "hint",
+ "No nodes match this pattern. Check spelling or try a broader regex.");
+ } else if (label) {
+ yyjson_mut_obj_add_str(
+ doc, root, "hint",
+ "No nodes with this label. Available labels: Function, Method, Class, "
+ "Interface, Route, Variable, Module, Package, File, Folder.");
+ }
+ }
+
bool sq_type_error = run_semantic_query(doc, root, args, store, project, limit);
if (sq_type_error) {
@@ -1475,6 +1519,13 @@ static char *handle_query_graph(cbm_mcp_server_t *srv, const char *args) {
yyjson_mut_obj_add_val(doc, root, "rows", rows);
yyjson_mut_obj_add_int(doc, root, "total", result.row_count);
+ if (result.row_count == 0) {
+ yyjson_mut_obj_add_str(
+ doc, root, "hint",
+ "Query returned no results. Use get_graph_schema() to see available labels and "
+ "edge types.");
+ }
+
char *json = yy_doc_to_str(doc);
yyjson_mut_doc_free(doc);
cbm_cypher_result_free(&result);
@@ -1502,6 +1553,11 @@ static char *handle_index_status(cbm_mcp_server_t *srv, const char *args) {
yyjson_mut_obj_add_int(doc, root, "nodes", nodes);
yyjson_mut_obj_add_int(doc, root, "edges", edges);
yyjson_mut_obj_add_str(doc, root, "status", nodes > 0 ? "ready" : "empty");
+ if (nodes == 0) {
+ yyjson_mut_obj_add_str(
+ doc, root, "hint",
+ "Project is empty. Re-run index_repository(repo_path=...) to populate.");
+ }
} else {
yyjson_mut_obj_add_str(doc, root, "status", "no_project");
}
@@ -1546,11 +1602,22 @@ static char *handle_delete_project(cbm_mcp_server_t *srv, const char *args) {
bool exists = (access(path, F_OK) == 0);
const char *status = "not_found";
+ const char *error_detail = NULL;
+ bool is_error = false;
+
if (exists) {
- (void)cbm_unlink(path);
+ int rc = cbm_unlink(path);
(void)cbm_unlink(wal);
(void)cbm_unlink(shm);
- status = "deleted";
+ if (rc == 0) {
+ status = "deleted";
+ } else {
+ status = "delete_failed";
+ error_detail = strerror(errno);
+ is_error = true;
+ }
+ } else {
+ is_error = true;
}
cbm_pipeline_unlock();
@@ -1561,12 +1628,15 @@ static char *handle_delete_project(cbm_mcp_server_t *srv, const char *args) {
yyjson_mut_doc_set_root(doc, root);
yyjson_mut_obj_add_str(doc, root, "project", name);
yyjson_mut_obj_add_str(doc, root, "status", status);
+ if (error_detail) {
+ yyjson_mut_obj_add_str(doc, root, "error", error_detail);
+ }
char *json = yy_doc_to_str(doc);
yyjson_mut_doc_free(doc);
free(name);
- char *result = cbm_mcp_text_result(json, false);
+ char *result = cbm_mcp_text_result(json, is_error);
free(json);
return result;
}
@@ -1815,13 +1885,20 @@ static char *handle_trace_call_path(cbm_mcp_server_t *srv, const char *args) {
cbm_store_find_nodes_by_name(store, project, func_name, &nodes, &node_count);
if (node_count == 0) {
+ enum { HINT_BUF_SZ = 512 };
+ char hint[HINT_BUF_SZ];
+ snprintf(hint, sizeof(hint),
+ "{\"error\":\"function not found\",\"function_name\":\"%s\","
+ "\"hint\":\"Use search_graph(name_pattern=\\\".*%s.*\\\") to find the exact "
+ "name, then pass it to trace_path.\"}",
+ func_name, func_name);
free(func_name);
free(project);
free(direction);
free(mode);
free(param_name);
cbm_store_free_nodes(nodes, 0);
- return cbm_mcp_text_result("{\"error\":\"function not found\"}", true);
+ return cbm_mcp_text_result(hint, true);
}
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
@@ -1999,9 +2076,13 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
free(srv->current_project);
srv->current_project = NULL;
- /* Serialize pipeline runs to prevent concurrent writes */
+ /* Serialize pipeline runs to prevent concurrent writes.
+ * Track active pipeline so signal handler and notifications/cancelled
+ * can cancel it mid-run. */
cbm_pipeline_lock();
+ srv->active_pipeline = p;
int rc = cbm_pipeline_run(p);
+ srv->active_pipeline = NULL;
cbm_pipeline_unlock();
cbm_pipeline_free(p);
@@ -2022,6 +2103,12 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
yyjson_mut_obj_add_str(doc, root, "project", project_name);
yyjson_mut_obj_add_str(doc, root, "status", rc == 0 ? "indexed" : "error");
+ if (rc != 0) {
+ yyjson_mut_obj_add_str(doc, root, "hint",
+ "Pipeline failed. Check repo_path exists and contains source files. "
+ "Try mode='fast' for a quicker diagnostic run.");
+ }
+
if (rc == 0) {
cbm_store_t *store = resolve_store(srv, project_name);
if (store) {
@@ -2944,11 +3031,14 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
/* ── Phase 1: Grep scan ──────────────────────────────────── */
char tmpfile[CBM_SZ_256];
if (!write_pattern_file(tmpfile, sizeof(tmpfile), pattern)) {
+ char errmsg[CBM_SZ_256];
+ snprintf(errmsg, sizeof(errmsg), "search failed: cannot create temp file (%s)",
+ strerror(errno));
free(root_path);
free(pattern);
free(project);
free(file_pattern);
- return cbm_mcp_text_result("search failed: temp file", true);
+ return cbm_mcp_text_result(errmsg, true);
}
/* No grep-level match limit — let grep find all matches, then dedup and
@@ -3132,11 +3222,15 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
FILE *fp = cbm_popen(cmd, "r");
if (!fp) {
+ char errmsg[CBM_SZ_256];
+ snprintf(errmsg, sizeof(errmsg),
+ "git diff failed: cannot execute command (%s). Check that git is installed.",
+ strerror(errno));
free(root_path);
free(project);
free(base_branch);
free(scope);
- return cbm_mcp_text_result("git diff failed", true);
+ return cbm_mcp_text_result(errmsg, true);
}
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
@@ -3168,7 +3262,17 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
detect_add_impacted_symbols(store, project, line, doc, impacted);
}
}
- cbm_pclose(fp);
+ int git_status = cbm_pclose(fp);
+
+ bool is_error = false;
+ if (git_status != 0 && file_count == 0) {
+ char hint_buf[CBM_SZ_256];
+ snprintf(hint_buf, sizeof(hint_buf),
+ "git diff exited with status %d. Check that branch '%s' exists.", git_status,
+ base_branch);
+ yyjson_mut_obj_add_strcpy(doc, root_obj, "hint", hint_buf);
+ is_error = true;
+ }
yyjson_mut_obj_add_val(doc, root_obj, "changed_files", changed);
yyjson_mut_obj_add_int(doc, root_obj, "changed_count", file_count);
@@ -3182,7 +3286,7 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
free(base_branch);
free(scope);
- char *result = cbm_mcp_text_result(json, false);
+ char *result = cbm_mcp_text_result(json, is_error);
free(json);
return result;
}
@@ -3270,6 +3374,7 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
yyjson_mut_val *root_obj = yyjson_mut_obj(doc);
yyjson_mut_doc_set_root(doc, root_obj);
+ bool is_error = false;
if (strcmp(mode_str, "update") == 0 && content) {
cbm_mkdir(adr_dir);
FILE *fp = fopen(adr_path, "w");
@@ -3279,6 +3384,8 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
yyjson_mut_obj_add_str(doc, root_obj, "status", "updated");
} else {
yyjson_mut_obj_add_str(doc, root_obj, "status", "write_error");
+ yyjson_mut_obj_add_str(doc, root_obj, "error", strerror(errno));
+ is_error = true;
}
} else if (strcmp(mode_str, "sections") == 0) {
adr_list_sections(doc, root_obj, adr_path);
@@ -3294,7 +3401,7 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
free(mode_str);
free(content);
- char *result = cbm_mcp_text_result(json, false);
+ char *result = cbm_mcp_text_result(json, is_error);
free(json);
return result;
}
@@ -3638,8 +3745,16 @@ char *cbm_mcp_server_handle(cbm_mcp_server_t *srv, const char *line) {
return cbm_jsonrpc_format_error(0, JSONRPC_PARSE_ERROR, "Parse error");
}
- /* Notifications (no id) → no response */
+ /* Notifications (no id) → handle cancellation, then no response */
if (!req.has_id) {
+ if (req.method && strcmp(req.method, "notifications/cancelled") == 0) {
+ /* MCP cancellation: cancel the active pipeline if request ID matches */
+ if (srv->active_pipeline) {
+ cbm_pipeline_cancel(srv->active_pipeline);
+ cbm_log_info("mcp.cancelled", "request_id_active",
+ srv->active_request_id > 0 ? "yes" : "none");
+ }
+ }
cbm_jsonrpc_request_free(&req);
return NULL;
}
diff --git a/src/mcp/mcp.h b/src/mcp/mcp.h
index 7e65912e9..0ebcc6f83 100644
--- a/src/mcp/mcp.h
+++ b/src/mcp/mcp.h
@@ -125,6 +125,14 @@ cbm_store_t *cbm_mcp_server_store(cbm_mcp_server_t *srv);
* This prevents resolve_store() from trying to open a .db file when tools specify a project. */
void cbm_mcp_server_set_project(cbm_mcp_server_t *srv, const char *project);
+/* ── Cancellation support ─────────────────────────────────────── */
+
+struct cbm_pipeline; /* forward decl */
+
+/* Get the currently active pipeline (for signal handler cancellation).
+ * Returns NULL if no pipeline is running. */
+struct cbm_pipeline *cbm_mcp_server_active_pipeline(cbm_mcp_server_t *srv);
+
/* ── URI helpers ───────────────────────────────────────────────── */
/* Parse a file:// URI and extract the filesystem path.