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

Function handle_get_code_snippet

src/mcp/mcp.c:8734–8823  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8732}
8733
8734static char *handle_get_code_snippet(cbm_mcp_server_t *srv, const char *args) {
8735 char *qn = cbm_mcp_get_string_arg(args, "qualified_name");
8736 char *project = get_project_arg(args);
8737 bool include_neighbors = cbm_mcp_get_bool_arg(args, "include_neighbors");
8738
8739 if (!qn) {
8740 free(project);
8741 return cbm_mcp_text_result("qualified_name is required", true);
8742 }
8743
8744 cbm_store_t *store = resolve_store(srv, project);
8745 if (!store) {
8746 char *_err = build_project_list_error("project not found or not indexed");
8747 char *_res = cbm_mcp_text_result(_err, true);
8748 free(_err);
8749 free(qn);
8750 free(project);
8751 return _res;
8752 }
8753
8754 char *not_indexed = verify_project_indexed(store, project);
8755 if (not_indexed) {
8756 free(qn);
8757 free(project);
8758 return not_indexed;
8759 }
8760
8761 /* Default to current project (same as all other tools) */
8762 const char *effective_project = project ? project : srv->current_project;
8763
8764 /* Tier 1: Exact QN match */
8765 cbm_node_t node = {0};
8766 int rc = cbm_store_find_node_by_qn(store, effective_project, qn, &node);
8767 if (rc == CBM_STORE_OK) {
8768 char *result = build_snippet_response(srv, &node, NULL, include_neighbors, NULL, 0);
8769 free_node_contents(&node);
8770 free(qn);
8771 free(project);
8772 return result;
8773 }
8774
8775 /* Tier 2: Suffix match — handles partial QNs ("main.HandleRequest")
8776 * and short names ("ProcessOrder") via LIKE '%.X'. */
8777 cbm_node_t *suffix_nodes = NULL;
8778 int suffix_count = 0;
8779 cbm_store_find_nodes_by_qn_suffix(store, effective_project, qn, &suffix_nodes, &suffix_count);
8780
8781 if (suffix_count == SKIP_ONE) {
8782 copy_node(&suffix_nodes[0], &node);
8783 cbm_store_free_nodes(suffix_nodes, suffix_count);
8784 char *result = build_snippet_response(srv, &node, "suffix", include_neighbors, NULL, 0);
8785 free_node_contents(&node);
8786 free(qn);
8787 free(project);
8788 return result;
8789 }
8790
8791 if (suffix_count > SKIP_ONE) {

Callers 1

dispatch_toolFunction · 0.85

Calls 15

cbm_mcp_get_string_argFunction · 0.85
get_project_argFunction · 0.85
cbm_mcp_get_bool_argFunction · 0.85
cbm_mcp_text_resultFunction · 0.85
resolve_storeFunction · 0.85
build_project_list_errorFunction · 0.85
verify_project_indexedFunction · 0.85
build_snippet_responseFunction · 0.85
free_node_contentsFunction · 0.85
copy_nodeFunction · 0.85

Tested by

no test coverage detected