Skip to content

Commit 986f9fd

Browse files
committed
fix(mcp): reject misleading ADR sections writes
Signed-off-by: Mustapha <m88e54ik@gmail.com>
1 parent 97ce23f commit 986f9fd

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,9 +642,10 @@ static const tool_def_t TOOLS[] = {
642642

643643
{"manage_adr", "Manage ADR", "Create or update Architecture Decision Records",
644644
"{\"type\":\"object\",\"properties\":{\"project\":{\"type\":\"string\"},\"mode\":{\"type\":"
645-
"\"string\",\"enum\":[\"get\",\"update\",\"sections\"]},\"content\":{\"type\":\"string\"},"
646-
"\"sections\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}}},\"required\":[\"project\"]"
647-
"}"},
645+
"\"string\",\"enum\":[\"get\",\"update\",\"sections\"],\"description\":\"update replaces "
646+
"the entire ADR document; sections only lists existing headings\"},\"content\":{\"type\":\"string\","
647+
"\"description\":\"Complete replacement document required by update\"}},\"additionalProperties\":false,"
648+
"\"required\":[\"project\"]}"},
648649

649650
{"ingest_traces", "Ingest traces", "Ingest runtime traces to enhance the knowledge graph",
650651
"{\"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) {
1015810159
mode_str = heap_strdup("get");
1015910160
}
1016010161

10162+
/* `sections` used to be advertised as an input argument, but it was never
10163+
* consumed: mode=update still replaced the whole document. Reject the old
10164+
* shape explicitly before opening a store so a stale client cannot mistake
10165+
* a whole-document replacement for a section-scoped update. */
10166+
bool has_sections_arg = false;
10167+
yyjson_doc *args_doc = yyjson_read(args, strlen(args), 0);
10168+
if (args_doc) {
10169+
yyjson_val *args_root = yyjson_doc_get_root(args_doc);
10170+
has_sections_arg = args_root && yyjson_is_obj(args_root) &&
10171+
yyjson_obj_get(args_root, "sections") != NULL;
10172+
yyjson_doc_free(args_doc);
10173+
}
10174+
if (has_sections_arg) {
10175+
free(project);
10176+
free(mode_str);
10177+
free(content);
10178+
return cbm_mcp_text_result(
10179+
"{\"status\":\"invalid_arguments\",\"error\":\"The sections argument is not an "
10180+
"update primitive and has been removed. No ADR write was performed.\"}",
10181+
true);
10182+
}
10183+
1016110184
bool mutation_held = false;
1016210185
if (project) {
1016310186
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) {
1026510288
if ((strcmp(mode_str, "update") == 0 || strcmp(mode_str, "store") == 0) && content) {
1026610289
if (cbm_store_adr_store(store, project, content) == CBM_STORE_OK) {
1026710290
yyjson_mut_obj_add_str(doc, root_obj, "status", "updated");
10291+
yyjson_mut_obj_add_str(doc, root_obj, "semantics", "whole_document_replaced");
1026810292
} else {
1026910293
yyjson_mut_obj_add_str(doc, root_obj, "status", "write_error");
1027010294
is_error = true;

‎tests/test_mcp.c‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4060,6 +4060,44 @@ TEST(tool_manage_adr_unified_backend_issue256) {
40604060
PASS();
40614061
}
40624062

4063+
TEST(tool_manage_adr_rejects_removed_sections_argument) {
4064+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
4065+
ASSERT_NOT_NULL(srv);
4066+
cbm_store_t *st = cbm_mcp_server_store(srv);
4067+
ASSERT_NOT_NULL(st);
4068+
ASSERT_EQ(cbm_store_upsert_project(st, "adr-sections-guard", "/tmp/adr-sections-guard"),
4069+
CBM_STORE_OK);
4070+
cbm_mcp_server_set_project(srv, "adr-sections-guard");
4071+
ASSERT_EQ(cbm_store_adr_store(st, "adr-sections-guard", "## PURPOSE\nOriginal ADR.\n"),
4072+
CBM_STORE_OK);
4073+
4074+
mcp_mutation_guard_probe_t probe = {0};
4075+
cbm_mcp_server_set_project_mutation_guard(srv, mcp_mutation_guard_probe_begin,
4076+
mcp_mutation_guard_probe_end, &probe);
4077+
4078+
char *resp = cbm_mcp_server_handle(
4079+
srv, "{\"jsonrpc\":\"2.0\",\"id\":122,\"method\":\"tools/call\","
4080+
"\"params\":{\"name\":\"manage_adr\",\"arguments\":{"
4081+
"\"project\":\"adr-sections-guard\",\"mode\":\"update\","
4082+
"\"sections\":[\"PURPOSE\"],\"content\":\"## PURPOSE\\nReplacement ADR.\\n\"}}}");
4083+
ASSERT_NOT_NULL(resp);
4084+
ASSERT_NOT_NULL(strstr(resp, "invalid_arguments"));
4085+
ASSERT_NOT_NULL(strstr(resp, "No ADR write was performed"));
4086+
ASSERT_NOT_NULL(strstr(resp, "\"isError\":true"));
4087+
free(resp);
4088+
ASSERT_EQ(probe.begin_count, 0);
4089+
ASSERT_EQ(probe.end_count, 0);
4090+
4091+
cbm_adr_t adr;
4092+
memset(&adr, 0, sizeof(adr));
4093+
ASSERT_EQ(cbm_store_adr_get(st, "adr-sections-guard", &adr), CBM_STORE_OK);
4094+
ASSERT_STR_EQ(adr.content, "## PURPOSE\nOriginal ADR.\n");
4095+
cbm_store_adr_free(&adr);
4096+
4097+
cbm_mcp_server_free(srv);
4098+
PASS();
4099+
}
4100+
40634101
TEST(tool_manage_adr_mutation_guard_balances_success) {
40644102
const char *project = "guard-adr-success";
40654103
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
@@ -9423,6 +9461,7 @@ SUITE(mcp) {
94239461
RUN_TEST(tool_manage_adr_no_project);
94249462
RUN_TEST(tool_manage_adr_get_with_existing_adr);
94259463
RUN_TEST(tool_manage_adr_unified_backend_issue256);
9464+
RUN_TEST(tool_manage_adr_rejects_removed_sections_argument);
94269465
RUN_TEST(tool_index_repository_reports_store_backed_adr);
94279466
RUN_TEST(tool_index_repository_dot_uses_absolute_project_key_and_preserves_adr);
94289467
RUN_TEST(index_repository_relative_path_uses_explicit_session_root);

0 commit comments

Comments
 (0)