|
34 | 34 | MCP_CONTENT_PREFIX = 15, /* strlen("Content-Length:") */ |
35 | 35 | MCP_RETURN_2 = 2, |
36 | 36 | MCP_TOOLS_PAGE_SIZE = 8, |
| 37 | + MCP_HELP_TOOLS_WRAP_COL = 74, /* --help tool list stays readable on 80-col terminals */ |
37 | 38 | MCP_MAX_CROSS_REPO_TARGETS = 4096, |
38 | 39 | }; |
39 | 40 | #define MCP_MS_TO_US 1000LL |
|
88 | 89 | #include <yyjson/yyjson.h> |
89 | 90 | #include <ctype.h> |
90 | 91 | #include <limits.h> |
| 92 | +#include <stdarg.h> // va_list, for the bounded help-list appender |
91 | 93 | #include <stdint.h> // int64_t |
92 | 94 | #include <stdio.h> |
93 | 95 | #include <stdlib.h> |
@@ -890,6 +892,74 @@ const char *cbm_mcp_tool_input_schema(const char *tool_name) { |
890 | 892 | return NULL; |
891 | 893 | } |
892 | 894 |
|
| 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 | + |
893 | 963 | static int mcp_tools_cursor_offset(const char *params_json, bool *has_cursor_out) { |
894 | 964 | if (has_cursor_out) { |
895 | 965 | *has_cursor_out = false; |
|
0 commit comments