MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_jsonrpc_parse

Function cbm_jsonrpc_parse

src/mcp/mcp.c:154–200  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

152 * ══════════════════════════════════════════════════════════════════ */
153
154int cbm_jsonrpc_parse(const char *line, cbm_jsonrpc_request_t *out) {
155 memset(out, 0, sizeof(*out));
156 out->id = CBM_NOT_FOUND;
157
158 yyjson_doc *doc = yyjson_read(line, strlen(line), 0);
159 if (!doc) {
160 return CBM_NOT_FOUND;
161 }
162
163 yyjson_val *root = yyjson_doc_get_root(doc);
164 if (!yyjson_is_obj(root)) {
165 yyjson_doc_free(doc);
166 return CBM_NOT_FOUND;
167 }
168
169 yyjson_val *v_jsonrpc = yyjson_obj_get(root, "jsonrpc");
170 yyjson_val *v_method = yyjson_obj_get(root, "method");
171 yyjson_val *v_id = yyjson_obj_get(root, "id");
172 yyjson_val *v_params = yyjson_obj_get(root, "params");
173
174 if (!v_method || !yyjson_is_str(v_method)) {
175 yyjson_doc_free(doc);
176 return CBM_NOT_FOUND;
177 }
178
179 out->jsonrpc =
180 heap_strdup(v_jsonrpc && yyjson_is_str(v_jsonrpc) ? yyjson_get_str(v_jsonrpc) : "2.0");
181 out->method = heap_strdup(yyjson_get_str(v_method));
182
183 if (v_id) {
184 out->has_id = true;
185 if (yyjson_is_int(v_id)) {
186 out->id = yyjson_get_int(v_id);
187 } else if (yyjson_is_str(v_id)) {
188 /* JSON-RPC 2.0 §4 permits string ids (Claude Desktop uses them).
189 * Preserve verbatim instead of coercing via strtol (issue #253). */
190 out->id_str = heap_strdup(yyjson_get_str(v_id));
191 }
192 }
193
194 if (v_params) {
195 out->params_raw = yyjson_val_write(v_params, 0, NULL);
196 }
197
198 yyjson_doc_free(doc);
199 return 0;
200}
201
202void cbm_jsonrpc_request_free(cbm_jsonrpc_request_t *r) {
203 if (!r) {

Callers 5

cbm_mcp_server_handleFunction · 0.85
application_mcp_requestFunction · 0.85
frontend_enqueueFunction · 0.85
test_mcp.cFile · 0.85

Calls 1

heap_strdupFunction · 0.70

Tested by

no test coverage detected