Extract the inner "text" value from an MCP tool result JSON. * The MCP envelope is: {"content":[{"type":"text","text":" "}]} * This returns the unescaped inner JSON. Caller must free. */
| 18518 | * The MCP envelope is: {"content":[{"type":"text","text":"<inner json>"}]} |
| 18519 | * This returns the unescaped inner JSON. Caller must free. */ |
| 18520 | static char *extract_text_content(const char *mcp_result) { |
| 18521 | if (!mcp_result) |
| 18522 | return NULL; |
| 18523 | yyjson_doc *doc = yyjson_read(mcp_result, strlen(mcp_result), 0); |
| 18524 | if (!doc) |
| 18525 | return strdup(mcp_result); /* fallback */ |
| 18526 | yyjson_val *root = yyjson_doc_get_root(doc); |
| 18527 | yyjson_val *content = yyjson_obj_get(root, "content"); |
| 18528 | if (!content) { |
| 18529 | /* Handle JSON-RPC wrapper: {"jsonrpc":...,"result":{"content":[...]}} */ |
| 18530 | yyjson_val *rpc_result = yyjson_obj_get(root, "result"); |
| 18531 | if (rpc_result) { |
| 18532 | content = yyjson_obj_get(rpc_result, "content"); |
| 18533 | } |
| 18534 | } |
| 18535 | if (!content || !yyjson_is_arr(content)) { |
| 18536 | yyjson_doc_free(doc); |
| 18537 | return strdup(mcp_result); |
| 18538 | } |
| 18539 | yyjson_val *item = yyjson_arr_get(content, 0); |
| 18540 | if (!item) { |
| 18541 | yyjson_doc_free(doc); |
| 18542 | return strdup(mcp_result); |
| 18543 | } |
| 18544 | yyjson_val *text = yyjson_obj_get(item, "text"); |
| 18545 | const char *str = yyjson_get_str(text); |
| 18546 | char *result = str ? strdup(str) : strdup(mcp_result); |
| 18547 | yyjson_doc_free(doc); |
| 18548 | return result; |
| 18549 | } |
| 18550 | |
| 18551 | typedef struct { |
| 18552 | char cache[512]; |
no outgoing calls
no test coverage detected