Skip to content

Commit 08f6d1d

Browse files
authored
Merge pull request DeusData#1141 from M-Marbouh/fix/manage-adr-sections-guard
fix(mcp): reject misleading ADR sections writes
2 parents facd32b + 58ab633 commit 08f6d1d

2 files changed

Lines changed: 67 additions & 3 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,11 @@ static const tool_def_t TOOLS[] = {
650650

651651
{"manage_adr", "Manage ADR", "Create or update Architecture Decision Records",
652652
"{\"type\":\"object\",\"properties\":{\"project\":{\"type\":\"string\"},\"mode\":{\"type\":"
653-
"\"string\",\"enum\":[\"get\",\"update\",\"sections\"]},\"content\":{\"type\":\"string\"},"
654-
"\"sections\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}}},\"required\":[\"project\"]"
655-
"}"},
653+
"\"string\",\"enum\":[\"get\",\"update\",\"sections\"],\"description\":\"update replaces "
654+
"the entire ADR document; sections only lists existing "
655+
"headings\"},\"content\":{\"type\":\"string\",\"description\":\"Complete replacement document "
656+
"required by update\"}},\"additionalProperties\":false,"
657+
"\"required\":[\"project\"]}"},
656658

657659
{"ingest_traces", "Ingest traces", "Ingest runtime traces to enhance the knowledge graph",
658660
"{\"type\":\"object\",\"properties\":{\"traces\":{\"type\":\"array\",\"items\":{\"type\":"
@@ -10372,6 +10374,28 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
1037210374
mode_str = heap_strdup("get");
1037310375
}
1037410376

10377+
/* `sections` used to be advertised as an input argument, but it was never
10378+
* consumed: mode=update still replaced the whole document. Reject the old
10379+
* shape explicitly before opening a store so a stale client cannot mistake
10380+
* a whole-document replacement for a section-scoped update. */
10381+
bool has_sections_arg = false;
10382+
yyjson_doc *args_doc = yyjson_read(args, strlen(args), 0);
10383+
if (args_doc) {
10384+
yyjson_val *args_root = yyjson_doc_get_root(args_doc);
10385+
has_sections_arg =
10386+
args_root && yyjson_is_obj(args_root) && yyjson_obj_get(args_root, "sections") != NULL;
10387+
yyjson_doc_free(args_doc);
10388+
}
10389+
if (has_sections_arg) {
10390+
free(project);
10391+
free(mode_str);
10392+
free(content);
10393+
return cbm_mcp_text_result(
10394+
"{\"status\":\"invalid_arguments\",\"error\":\"The sections argument is not an "
10395+
"update primitive and has been removed. No ADR write was performed.\"}",
10396+
true);
10397+
}
10398+
1037510399
bool mutation_held = false;
1037610400
if (project) {
1037710401
mutation_held = mcp_project_mutation_begin(srv, project);
@@ -10479,6 +10503,7 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
1047910503
if ((strcmp(mode_str, "update") == 0 || strcmp(mode_str, "store") == 0) && content) {
1048010504
if (cbm_store_adr_store(store, project, content) == CBM_STORE_OK) {
1048110505
yyjson_mut_obj_add_str(doc, root_obj, "status", "updated");
10506+
yyjson_mut_obj_add_str(doc, root_obj, "semantics", "whole_document_replaced");
1048210507
} else {
1048310508
yyjson_mut_obj_add_str(doc, root_obj, "status", "write_error");
1048410509
is_error = true;

‎tests/test_mcp.c‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4252,6 +4252,44 @@ TEST(tool_manage_adr_unified_backend_issue256) {
42524252
PASS();
42534253
}
42544254

4255+
TEST(tool_manage_adr_rejects_removed_sections_argument) {
4256+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
4257+
ASSERT_NOT_NULL(srv);
4258+
cbm_store_t *st = cbm_mcp_server_store(srv);
4259+
ASSERT_NOT_NULL(st);
4260+
ASSERT_EQ(cbm_store_upsert_project(st, "adr-sections-guard", "/tmp/adr-sections-guard"),
4261+
CBM_STORE_OK);
4262+
cbm_mcp_server_set_project(srv, "adr-sections-guard");
4263+
ASSERT_EQ(cbm_store_adr_store(st, "adr-sections-guard", "## PURPOSE\nOriginal ADR.\n"),
4264+
CBM_STORE_OK);
4265+
4266+
mcp_mutation_guard_probe_t probe = {0};
4267+
cbm_mcp_server_set_project_mutation_guard(srv, mcp_mutation_guard_probe_begin,
4268+
mcp_mutation_guard_probe_end, &probe);
4269+
4270+
char *resp = cbm_mcp_server_handle(
4271+
srv, "{\"jsonrpc\":\"2.0\",\"id\":122,\"method\":\"tools/call\","
4272+
"\"params\":{\"name\":\"manage_adr\",\"arguments\":{"
4273+
"\"project\":\"adr-sections-guard\",\"mode\":\"update\","
4274+
"\"sections\":[\"PURPOSE\"],\"content\":\"## PURPOSE\\nReplacement ADR.\\n\"}}}");
4275+
ASSERT_NOT_NULL(resp);
4276+
ASSERT_NOT_NULL(strstr(resp, "invalid_arguments"));
4277+
ASSERT_NOT_NULL(strstr(resp, "No ADR write was performed"));
4278+
ASSERT_NOT_NULL(strstr(resp, "\"isError\":true"));
4279+
free(resp);
4280+
ASSERT_EQ(probe.begin_count, 0);
4281+
ASSERT_EQ(probe.end_count, 0);
4282+
4283+
cbm_adr_t adr;
4284+
memset(&adr, 0, sizeof(adr));
4285+
ASSERT_EQ(cbm_store_adr_get(st, "adr-sections-guard", &adr), CBM_STORE_OK);
4286+
ASSERT_STR_EQ(adr.content, "## PURPOSE\nOriginal ADR.\n");
4287+
cbm_store_adr_free(&adr);
4288+
4289+
cbm_mcp_server_free(srv);
4290+
PASS();
4291+
}
4292+
42554293
TEST(tool_manage_adr_mutation_guard_balances_success) {
42564294
const char *project = "guard-adr-success";
42574295
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
@@ -9796,6 +9834,7 @@ SUITE(mcp) {
97969834
RUN_TEST(tool_manage_adr_no_project);
97979835
RUN_TEST(tool_manage_adr_get_with_existing_adr);
97989836
RUN_TEST(tool_manage_adr_unified_backend_issue256);
9837+
RUN_TEST(tool_manage_adr_rejects_removed_sections_argument);
97999838
RUN_TEST(tool_index_repository_reports_store_backed_adr);
98009839
RUN_TEST(tool_index_repository_resolves_root_path_from_project_name_issue1211);
98019840
RUN_TEST(tool_index_repository_unknown_project_name_still_requires_repo_path);

0 commit comments

Comments
 (0)