| 12274 | } |
| 12275 | |
| 12276 | char *cbm_cli_build_args_json(const char *tool_name, int argc, char **argv, char **err_out) { |
| 12277 | if (err_out) { |
| 12278 | *err_out = NULL; |
| 12279 | } |
| 12280 | |
| 12281 | /* The tool's input_schema (may be NULL for an unknown tool — then every |
| 12282 | * value is treated as a string). Static lifetime; do not free. */ |
| 12283 | const char *schema_str = cbm_mcp_tool_input_schema(tool_name); |
| 12284 | yyjson_doc *schema_doc = NULL; |
| 12285 | yyjson_val *props = NULL; |
| 12286 | if (schema_str) { |
| 12287 | schema_doc = yyjson_read(schema_str, strlen(schema_str), 0); |
| 12288 | if (schema_doc) { |
| 12289 | props = yyjson_obj_get(yyjson_doc_get_root(schema_doc), "properties"); |
| 12290 | } |
| 12291 | } |
| 12292 | |
| 12293 | yyjson_mut_doc *out = yyjson_mut_doc_new(NULL); |
| 12294 | if (!out) { |
| 12295 | if (schema_doc) { |
| 12296 | yyjson_doc_free(schema_doc); |
| 12297 | } |
| 12298 | return NULL; |
| 12299 | } |
| 12300 | yyjson_mut_val *obj = yyjson_mut_obj(out); |
| 12301 | yyjson_mut_doc_set_root(out, obj); |
| 12302 | |
| 12303 | bool ok = true; |
| 12304 | for (int i = 0; i < argc && ok; i++) { |
| 12305 | const char *arg = argv[i]; |
| 12306 | if (strcmp(arg, "--") == 0) { |
| 12307 | break; /* end of flag parsing */ |
| 12308 | } |
| 12309 | if (strncmp(arg, "--", CLI_PAIR_LEN) != 0) { |
| 12310 | if (err_out) { |
| 12311 | *err_out = cli_heap_msgf("unexpected argument '%s' (expected --flag value)", arg); |
| 12312 | } |
| 12313 | ok = false; |
| 12314 | break; |
| 12315 | } |
| 12316 | |
| 12317 | const char *body = arg + CLI_PAIR_LEN; /* skip leading "--" */ |
| 12318 | const char *eq = strchr(body, '='); |
| 12319 | char key[CLI_BUF_256]; |
| 12320 | const char *value = NULL; |
| 12321 | bool have_value = false; |
| 12322 | |
| 12323 | if (eq) { |
| 12324 | /* --key=value : split on the FIRST '='; value may contain '='/spaces. */ |
| 12325 | size_t klen = (size_t)(eq - body); |
| 12326 | if (klen >= sizeof(key)) { |
| 12327 | klen = sizeof(key) - CLI_SKIP_ONE; |
| 12328 | } |
| 12329 | memcpy(key, body, klen); |
| 12330 | key[klen] = '\0'; |
| 12331 | value = eq + CLI_SKIP_ONE; |
| 12332 | have_value = true; |
| 12333 | } else { |
no test coverage detected