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.

fix(mcp): reject misleading ADR sections writes

Mustapha986f9fd49803
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index fb5a750ec..934942af4 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -642,9 +642,10 @@ static const tool_def_t TOOLS[] = {
{"manage_adr", "Manage ADR", "Create or update Architecture Decision Records",
"{\"type\":\"object\",\"properties\":{\"project\":{\"type\":\"string\"},\"mode\":{\"type\":"
- "\"string\",\"enum\":[\"get\",\"update\",\"sections\"]},\"content\":{\"type\":\"string\"},"
- "\"sections\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}}},\"required\":[\"project\"]"
- "}"},
+ "\"string\",\"enum\":[\"get\",\"update\",\"sections\"],\"description\":\"update replaces "
+ "the entire ADR document; sections only lists existing headings\"},\"content\":{\"type\":\"string\","
+ "\"description\":\"Complete replacement document required by update\"}},\"additionalProperties\":false,"
+ "\"required\":[\"project\"]}"},
{"ingest_traces", "Ingest traces", "Ingest runtime traces to enhance the knowledge graph",
"{\"type\":\"object\",\"properties\":{\"traces\":{\"type\":\"array\",\"items\":{\"type\":"
@@ -10158,6 +10159,28 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
mode_str = heap_strdup("get");
}
+ /* `sections` used to be advertised as an input argument, but it was never
+ * consumed: mode=update still replaced the whole document. Reject the old
+ * shape explicitly before opening a store so a stale client cannot mistake
+ * a whole-document replacement for a section-scoped update. */
+ bool has_sections_arg = false;
+ yyjson_doc *args_doc = yyjson_read(args, strlen(args), 0);
+ if (args_doc) {
+ yyjson_val *args_root = yyjson_doc_get_root(args_doc);
+ has_sections_arg = args_root && yyjson_is_obj(args_root) &&
+ yyjson_obj_get(args_root, "sections") != NULL;
+ yyjson_doc_free(args_doc);
+ }
+ if (has_sections_arg) {
+ free(project);
+ free(mode_str);
+ free(content);
+ return cbm_mcp_text_result(
+ "{\"status\":\"invalid_arguments\",\"error\":\"The sections argument is not an "
+ "update primitive and has been removed. No ADR write was performed.\"}",
+ true);
+ }
+
bool mutation_held = false;
if (project) {
mutation_held = mcp_project_mutation_begin(srv, project);
@@ -10265,6 +10288,7 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
if ((strcmp(mode_str, "update") == 0 || strcmp(mode_str, "store") == 0) && content) {
if (cbm_store_adr_store(store, project, content) == CBM_STORE_OK) {
yyjson_mut_obj_add_str(doc, root_obj, "status", "updated");
+ yyjson_mut_obj_add_str(doc, root_obj, "semantics", "whole_document_replaced");
} else {
yyjson_mut_obj_add_str(doc, root_obj, "status", "write_error");
is_error = true;
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index f1809dccd..0cbaa4573 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -4060,6 +4060,44 @@ TEST(tool_manage_adr_unified_backend_issue256) {
PASS();
}
+TEST(tool_manage_adr_rejects_removed_sections_argument) {
+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
+ ASSERT_NOT_NULL(srv);
+ cbm_store_t *st = cbm_mcp_server_store(srv);
+ ASSERT_NOT_NULL(st);
+ ASSERT_EQ(cbm_store_upsert_project(st, "adr-sections-guard", "/tmp/adr-sections-guard"),
+ CBM_STORE_OK);
+ cbm_mcp_server_set_project(srv, "adr-sections-guard");
+ ASSERT_EQ(cbm_store_adr_store(st, "adr-sections-guard", "## PURPOSE\nOriginal ADR.\n"),
+ CBM_STORE_OK);
+
+ mcp_mutation_guard_probe_t probe = {0};
+ cbm_mcp_server_set_project_mutation_guard(srv, mcp_mutation_guard_probe_begin,
+ mcp_mutation_guard_probe_end, &probe);
+
+ char *resp = cbm_mcp_server_handle(
+ srv, "{\"jsonrpc\":\"2.0\",\"id\":122,\"method\":\"tools/call\","
+ "\"params\":{\"name\":\"manage_adr\",\"arguments\":{"
+ "\"project\":\"adr-sections-guard\",\"mode\":\"update\","
+ "\"sections\":[\"PURPOSE\"],\"content\":\"## PURPOSE\\nReplacement ADR.\\n\"}}}");
+ ASSERT_NOT_NULL(resp);
+ ASSERT_NOT_NULL(strstr(resp, "invalid_arguments"));
+ ASSERT_NOT_NULL(strstr(resp, "No ADR write was performed"));
+ ASSERT_NOT_NULL(strstr(resp, "\"isError\":true"));
+ free(resp);
+ ASSERT_EQ(probe.begin_count, 0);
+ ASSERT_EQ(probe.end_count, 0);
+
+ cbm_adr_t adr;
+ memset(&adr, 0, sizeof(adr));
+ ASSERT_EQ(cbm_store_adr_get(st, "adr-sections-guard", &adr), CBM_STORE_OK);
+ ASSERT_STR_EQ(adr.content, "## PURPOSE\nOriginal ADR.\n");
+ cbm_store_adr_free(&adr);
+
+ cbm_mcp_server_free(srv);
+ PASS();
+}
+
TEST(tool_manage_adr_mutation_guard_balances_success) {
const char *project = "guard-adr-success";
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
@@ -9423,6 +9461,7 @@ SUITE(mcp) {
RUN_TEST(tool_manage_adr_no_project);
RUN_TEST(tool_manage_adr_get_with_existing_adr);
RUN_TEST(tool_manage_adr_unified_backend_issue256);
+ RUN_TEST(tool_manage_adr_rejects_removed_sections_argument);
RUN_TEST(tool_index_repository_reports_store_backed_adr);
RUN_TEST(tool_index_repository_dot_uses_absolute_project_key_and_preserves_adr);
RUN_TEST(index_repository_relative_path_uses_explicit_session_root);