| 12624 | #endif |
| 12625 | |
| 12626 | char *cbm_cli_build_args_json(const char *tool_name, int argc, char **argv, char **err_out) { |
| 12627 | if (err_out) { |
| 12628 | *err_out = NULL; |
| 12629 | } |
| 12630 | |
| 12631 | /* The tool's input_schema (may be NULL for an unknown tool — then every |
| 12632 | * value is treated as a string). Static lifetime; do not free. */ |
| 12633 | const char *schema_str = cbm_mcp_tool_input_schema(tool_name); |
| 12634 | yyjson_doc *schema_doc = NULL; |
| 12635 | yyjson_val *props = NULL; |
| 12636 | if (schema_str) { |
| 12637 | schema_doc = yyjson_read(schema_str, strlen(schema_str), 0); |
| 12638 | if (schema_doc) { |
| 12639 | props = yyjson_obj_get(yyjson_doc_get_root(schema_doc), "properties"); |
| 12640 | } |
| 12641 | } |
| 12642 | |
| 12643 | yyjson_mut_doc *out = yyjson_mut_doc_new(NULL); |
| 12644 | if (!out) { |
| 12645 | if (schema_doc) { |
| 12646 | yyjson_doc_free(schema_doc); |
| 12647 | } |
| 12648 | return NULL; |
| 12649 | } |
| 12650 | yyjson_mut_val *obj = yyjson_mut_obj(out); |
| 12651 | yyjson_mut_doc_set_root(out, obj); |
| 12652 | |
| 12653 | bool ok = true; |
| 12654 | for (int i = 0; i < argc && ok; i++) { |
| 12655 | const char *arg = argv[i]; |
| 12656 | if (strcmp(arg, "--") == 0) { |
| 12657 | break; /* end of flag parsing */ |
| 12658 | } |
| 12659 | if (strncmp(arg, "--", CLI_PAIR_LEN) != 0) { |
| 12660 | if (err_out) { |
| 12661 | *err_out = cli_heap_msgf("unexpected argument '%s' (expected --flag value)", arg); |
| 12662 | } |
| 12663 | ok = false; |
| 12664 | break; |
| 12665 | } |
| 12666 | |
| 12667 | const char *body = arg + CLI_PAIR_LEN; /* skip leading "--" */ |
| 12668 | const char *eq = strchr(body, '='); |
| 12669 | char key[CLI_BUF_256]; |
| 12670 | const char *value = NULL; |
| 12671 | bool have_value = false; |
| 12672 | |
| 12673 | if (eq) { |
| 12674 | /* --key=value : split on the FIRST '='; value may contain '='/spaces. */ |
| 12675 | size_t klen = (size_t)(eq - body); |
| 12676 | if (klen >= sizeof(key)) { |
| 12677 | klen = sizeof(key) - CLI_SKIP_ONE; |
| 12678 | } |
| 12679 | memcpy(key, body, klen); |
| 12680 | key[klen] = '\0'; |
| 12681 | value = eq + CLI_SKIP_ONE; |
| 12682 | have_value = true; |
| 12683 | } else { |
no test coverage detected