Skip to content

Commit c6f564e

Browse files
authored
Merge pull request DeusData#1370 from DeusData/fix/1361-help-missing-tool
fix(cli): derive --help tool list from the MCP registry
2 parents 69599b7 + 644c8fd commit c6f564e

4 files changed

Lines changed: 121 additions & 4 deletions

File tree

‎src/main.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -888,10 +888,13 @@ static void print_help(void) {
888888
printf(" Manual/UI MCP boundaries: Qodo, Warp, JetBrains AI/ACP, Replit,\n");
889889
printf(" Plandex, SWE-agent, BLACKBOX, GitHub cloud agents, Jules,\n");
890890
printf(" CodeRabbit.\n");
891-
printf("\nTools: index_repository, search_graph, query_graph, trace_path,\n");
892-
printf(" get_code_snippet, get_graph_schema, get_architecture, search_code,\n");
893-
printf(" list_projects, delete_project, index_status, detect_changes,\n");
894-
printf(" manage_adr, ingest_traces\n");
891+
/* Rendered from the MCP tool registry: a hand-maintained copy here
892+
* omitted check_index_coverage (#1361) and could silently drift again. */
893+
char *tools_help = cbm_mcp_tools_help_list();
894+
if (tools_help) {
895+
printf("\n%s", tools_help);
896+
free(tools_help);
897+
}
895898
}
896899

897900
/* ── Main ───────────────────────────────────────────────────────── */

‎src/mcp/mcp.c‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ enum {
3434
MCP_CONTENT_PREFIX = 15, /* strlen("Content-Length:") */
3535
MCP_RETURN_2 = 2,
3636
MCP_TOOLS_PAGE_SIZE = 8,
37+
MCP_HELP_TOOLS_WRAP_COL = 74, /* --help tool list stays readable on 80-col terminals */
3738
MCP_MAX_CROSS_REPO_TARGETS = 4096,
3839
};
3940
#define MCP_MS_TO_US 1000LL
@@ -88,6 +89,7 @@ enum {
8889
#include <yyjson/yyjson.h>
8990
#include <ctype.h>
9091
#include <limits.h>
92+
#include <stdarg.h> // va_list, for the bounded help-list appender
9193
#include <stdint.h> // int64_t
9294
#include <stdio.h>
9395
#include <stdlib.h>
@@ -890,6 +892,74 @@ const char *cbm_mcp_tool_input_schema(const char *tool_name) {
890892
return NULL;
891893
}
892894

895+
int cbm_mcp_tool_count(void) {
896+
return TOOL_COUNT;
897+
}
898+
899+
const char *cbm_mcp_tool_name(int index) {
900+
if (index < 0 || index >= TOOL_COUNT) {
901+
return NULL;
902+
}
903+
return TOOLS[index].name;
904+
}
905+
906+
/* Render the top-level --help "Tools:" block from the registry tools/list
907+
* serves. The list used to be hand-maintained in the help text and drifted
908+
* when check_index_coverage was added (#1361); deriving it here makes that
909+
* divergence impossible. Heap-allocated; caller frees. */
910+
/* Append at out[len] and return the bytes ACTUALLY written.
911+
*
912+
* snprintf returns the length it WOULD have written, so accumulating that value
913+
* lets len run past cap; the next `cap - len` then underflows to a huge size_t
914+
* and the following write lands outside the buffer. CodeQL flagged exactly that
915+
* shape here, and it is the same class #1173 just fixed in the Cypher list
916+
* builder. The capacity computed below does happen to be sufficient today —
917+
* which makes this the more dangerous version, not the safer one: the code is
918+
* correct only by an argument made ten lines away, so renaming a tool or
919+
* changing the wrap rule would turn it into an overflow with nothing to notice.
920+
* Clamping makes `len <= cap - 1` a local invariant no later edit can void. */
921+
static size_t help_append(char *out, size_t cap, size_t len, const char *fmt, ...) {
922+
if (len + 1 >= cap) {
923+
return 0;
924+
}
925+
va_list args;
926+
va_start(args, fmt);
927+
int written = vsnprintf(out + len, cap - len, fmt, args);
928+
va_end(args);
929+
if (written <= 0) {
930+
return 0;
931+
}
932+
size_t room = cap - len - 1;
933+
return (size_t)written > room ? room : (size_t)written;
934+
}
935+
936+
char *cbm_mcp_tools_help_list(void) {
937+
size_t cap = SLEN("Tools:") + 2; /* trailing newline + NUL */
938+
for (int i = 0; i < TOOL_COUNT; i++) {
939+
cap += strlen(TOOLS[i].name) + SLEN(" ,\n "); /* per-tool worst case incl. a wrap */
940+
}
941+
char *out = malloc(cap);
942+
if (!out) {
943+
return NULL;
944+
}
945+
size_t len = help_append(out, cap, 0, "Tools:");
946+
size_t col = len;
947+
for (int i = 0; i < TOOL_COUNT; i++) {
948+
const char *sep = (i + 1 < TOOL_COUNT) ? "," : "";
949+
size_t item = SLEN(" ") + strlen(TOOLS[i].name) + strlen(sep);
950+
if (i > 0 && col + item > MCP_HELP_TOOLS_WRAP_COL) {
951+
len += help_append(out, cap, len, "\n ");
952+
col = 1;
953+
}
954+
size_t wrote = help_append(out, cap, len, " %s%s", TOOLS[i].name, sep);
955+
len += wrote;
956+
col += wrote;
957+
}
958+
len += help_append(out, cap, len, "\n");
959+
(void)len; /* final length is not needed; the buffer is NUL-terminated */
960+
return out;
961+
}
962+
893963
static int mcp_tools_cursor_offset(const char *params_json, bool *has_cursor_out) {
894964
if (has_cursor_out) {
895965
*has_cursor_out = false;

‎src/mcp/mcp.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ char *cbm_mcp_tools_list(void);
6767
* NULL if the tool is unknown. Backs the CLI flag parser + per-tool --help. */
6868
const char *cbm_mcp_tool_input_schema(const char *tool_name);
6969

70+
/* Registry accessors: the number of tools tools/list advertises, and the name
71+
* of tool `index` (static, do not free; NULL when out of range). */
72+
int cbm_mcp_tool_count(void);
73+
const char *cbm_mcp_tool_name(int index);
74+
75+
/* Render the top-level --help "Tools:" block from the registry so the help
76+
* text cannot drift from tools/list (#1361). Heap-allocated; caller frees. */
77+
char *cbm_mcp_tools_help_list(void);
78+
7079
/* Format the initialize response. params_json is the raw initialize params
7180
* (used for protocol version negotiation). Returns heap-allocated JSON. */
7281
char *cbm_mcp_initialize_response(const char *params_json);

‎tests/test_mcp.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,40 @@ TEST(mcp_tools_list) {
762762
PASS();
763763
}
764764

765+
/* #1361: --help omitted check_index_coverage because its tool list was a
766+
* hand-maintained copy. The list is now rendered from the registry; this pins
767+
* the render so a formatter bug cannot reintroduce a silent omission. */
768+
TEST(mcp_tools_help_list_matches_registry) {
769+
char *help = cbm_mcp_tools_help_list();
770+
ASSERT_NOT_NULL(help);
771+
int count = cbm_mcp_tool_count();
772+
ASSERT_GT(count, 0);
773+
for (int i = 0; i < count; i++) {
774+
const char *name = cbm_mcp_tool_name(i);
775+
ASSERT_NOT_NULL(name);
776+
ASSERT_NOT_NULL(strstr(help, name));
777+
}
778+
/* Exactly one comma between consecutive tools: the rendered cardinality
779+
* equals the registry's, so truncation or duplication fails here. */
780+
int commas = 0;
781+
for (const char *p = help; *p; p++) {
782+
if (*p == ',') {
783+
commas++;
784+
}
785+
}
786+
ASSERT_EQ(commas, count - 1);
787+
/* Wrapped for an 80-column terminal. */
788+
const char *line = help;
789+
while (line && *line) {
790+
const char *nl = strchr(line, '\n');
791+
size_t line_len = nl ? (size_t)(nl - line) : strlen(line);
792+
ASSERT_LT((int)line_len, 80);
793+
line = nl ? nl + 1 : NULL;
794+
}
795+
free(help);
796+
PASS();
797+
}
798+
765799
TEST(mcp_tools_list_latest_metadata) {
766800
char *json = cbm_mcp_tools_list();
767801
ASSERT_NOT_NULL(json);
@@ -9642,6 +9676,7 @@ SUITE(mcp) {
96429676
/* MCP protocol helpers */
96439677
RUN_TEST(mcp_initialize_response);
96449678
RUN_TEST(mcp_tools_list);
9679+
RUN_TEST(mcp_tools_help_list_matches_registry);
96459680
RUN_TEST(mcp_tools_list_latest_metadata);
96469681
RUN_TEST(mcp_tools_have_behavior_annotations);
96479682
RUN_TEST(mcp_index_repository_declares_name_override_issue571);

0 commit comments

Comments
 (0)