| 4587 | } |
| 4588 | |
| 4589 | char *cbm_cli_build_args_json(const char *tool_name, int argc, char **argv, char **err_out) { |
| 4590 | if (err_out) { |
| 4591 | *err_out = NULL; |
| 4592 | } |
| 4593 | |
| 4594 | /* The tool's input_schema (may be NULL for an unknown tool — then every |
| 4595 | * value is treated as a string). Static lifetime; do not free. */ |
| 4596 | const char *schema_str = cbm_mcp_tool_input_schema(tool_name); |
| 4597 | yyjson_doc *schema_doc = NULL; |
| 4598 | yyjson_val *props = NULL; |
| 4599 | if (schema_str) { |
| 4600 | schema_doc = yyjson_read(schema_str, strlen(schema_str), 0); |
| 4601 | if (schema_doc) { |
| 4602 | props = yyjson_obj_get(yyjson_doc_get_root(schema_doc), "properties"); |
| 4603 | } |
| 4604 | } |
| 4605 | |
| 4606 | yyjson_mut_doc *out = yyjson_mut_doc_new(NULL); |
| 4607 | if (!out) { |
| 4608 | if (schema_doc) { |
| 4609 | yyjson_doc_free(schema_doc); |
| 4610 | } |
| 4611 | return NULL; |
| 4612 | } |
| 4613 | yyjson_mut_val *obj = yyjson_mut_obj(out); |
| 4614 | yyjson_mut_doc_set_root(out, obj); |
| 4615 | |
| 4616 | bool ok = true; |
| 4617 | for (int i = 0; i < argc && ok; i++) { |
| 4618 | const char *arg = argv[i]; |
| 4619 | if (strcmp(arg, "--") == 0) { |
| 4620 | break; /* end of flag parsing */ |
| 4621 | } |
| 4622 | if (strncmp(arg, "--", CLI_PAIR_LEN) != 0) { |
| 4623 | if (err_out) { |
| 4624 | *err_out = cli_heap_msgf("unexpected argument '%s' (expected --flag value)", arg); |
| 4625 | } |
| 4626 | ok = false; |
| 4627 | break; |
| 4628 | } |
| 4629 | |
| 4630 | const char *body = arg + CLI_PAIR_LEN; /* skip leading "--" */ |
| 4631 | const char *eq = strchr(body, '='); |
| 4632 | char key[CLI_BUF_256]; |
| 4633 | const char *value = NULL; |
| 4634 | bool have_value = false; |
| 4635 | |
| 4636 | if (eq) { |
| 4637 | /* --key=value : split on the FIRST '='; value may contain '='/spaces. */ |
| 4638 | size_t klen = (size_t)(eq - body); |
| 4639 | if (klen >= sizeof(key)) { |
| 4640 | klen = sizeof(key) - CLI_SKIP_ONE; |
| 4641 | } |
| 4642 | memcpy(key, body, klen); |
| 4643 | key[klen] = '\0'; |
| 4644 | value = eq + CLI_SKIP_ONE; |
| 4645 | have_value = true; |
| 4646 | } else { |
no test coverage detected