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. */
| 2775 | * The MCP envelope is: {"content":[{"type":"text","text":"<inner json>"}]} |
| 2776 | * This returns the unescaped inner JSON. Caller must free. */ |
| 2777 | static char *extract_text_content(const char *mcp_result) { |
| 2778 | if (!mcp_result) |
| 2779 | return NULL; |
| 2780 | yyjson_doc *doc = yyjson_read(mcp_result, strlen(mcp_result), 0); |
| 2781 | if (!doc) |
| 2782 | return strdup(mcp_result); /* fallback */ |
| 2783 | yyjson_val *root = yyjson_doc_get_root(doc); |
| 2784 | yyjson_val *content = yyjson_obj_get(root, "content"); |
| 2785 | if (!content) { |
| 2786 | /* Handle JSON-RPC wrapper: {"jsonrpc":...,"result":{"content":[...]}} */ |
| 2787 | yyjson_val *rpc_result = yyjson_obj_get(root, "result"); |
| 2788 | if (rpc_result) { |
| 2789 | content = yyjson_obj_get(rpc_result, "content"); |
| 2790 | } |
| 2791 | } |
| 2792 | if (!content || !yyjson_is_arr(content)) { |
| 2793 | yyjson_doc_free(doc); |
| 2794 | return strdup(mcp_result); |
| 2795 | } |
| 2796 | yyjson_val *item = yyjson_arr_get(content, 0); |
| 2797 | if (!item) { |
| 2798 | yyjson_doc_free(doc); |
| 2799 | return strdup(mcp_result); |
| 2800 | } |
| 2801 | yyjson_val *text = yyjson_obj_get(item, "text"); |
| 2802 | const char *str = yyjson_get_str(text); |
| 2803 | char *result = str ? strdup(str) : strdup(mcp_result); |
| 2804 | yyjson_doc_free(doc); |
| 2805 | return result; |
| 2806 | } |
| 2807 | |
| 2808 | /* Call get_code_snippet and extract inner text content. |
| 2809 | * Caller must free returned string. */ |
no outgoing calls
no test coverage detected