MCPcopy
hub / github.com/HKUDS/DeepCode / search_code_references

Function search_code_references

tools/code_reference_indexer.py:335–408  ·  view source on GitHub ↗

**UNIFIED TOOL**: Search relevant reference code from index files for target file implementation. This tool combines directory setup, index loading, and searching in a single call. Args: indexes_path: Path to the indexes directory containing JSON index files target_file

(
    indexes_path: str, target_file: str, keywords: str = "", max_results: int = 10
)

Source from the content-addressed store, hash-verified

333
334@mcp.tool()
335async def search_code_references(
336 indexes_path: str, target_file: str, keywords: str = "", max_results: int = 10
337) -> str:
338 """
339 **UNIFIED TOOL**: Search relevant reference code from index files for target file implementation.
340 This tool combines directory setup, index loading, and searching in a single call.
341
342 Args:
343 indexes_path: Path to the indexes directory containing JSON index files
344 target_file: Target file path (file to be implemented)
345 keywords: Search keywords, comma-separated
346 max_results: Maximum number of results to return
347
348 Returns:
349 Formatted reference code information JSON string
350 """
351 try:
352 # Step 1: Load index files from specified directory
353 logger.info(f"Loading index files from: {indexes_path}")
354 index_cache = load_index_files_from_directory(indexes_path)
355
356 if not index_cache:
357 result = {
358 "status": "error",
359 "message": f"No index files found or failed to load from: {indexes_path}",
360 "target_file": target_file,
361 "indexes_path": indexes_path,
362 }
363 return json.dumps(result, ensure_ascii=False, indent=2)
364
365 # Step 2: Parse keywords
366 keyword_list = (
367 [kw.strip() for kw in keywords.split(",") if kw.strip()] if keywords else []
368 )
369
370 # Step 3: Find relevant reference code
371 relevant_refs = find_relevant_references_in_cache(
372 target_file, index_cache, keyword_list, max_results
373 )
374
375 # Step 4: Find direct relationships
376 relationships = find_direct_relationships_in_cache(target_file, index_cache)
377
378 # Step 5: Format output
379 formatted_output = format_reference_output(
380 target_file, relevant_refs, relationships
381 )
382
383 result = {
384 "status": "success",
385 "target_file": target_file,
386 "indexes_path": indexes_path,
387 "keywords_used": keyword_list,
388 "total_references_found": len(relevant_refs),
389 "total_relationships_found": len(relationships),
390 "formatted_content": formatted_output,
391 "indexes_loaded": list(index_cache.keys()),
392 "total_indexes_loaded": len(index_cache),

Callers

nothing calls this directly

Tested by

no test coverage detected