get_file_summary
Generate a concise summary of code files including line count, function definitions, imports, and complexity metrics to analyze code structure.
Instructions
Get a summary of a specific file, including:
- Line count
- Function/class definitions (for supported languages)
- Import statements
- Basic complexity metrics
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes |
Implementation Reference
- src/code_index_mcp/server.py:235-246 (handler)MCP tool registration, handler, and schema (via signature and docstring) for 'get_file_summary'. Delegates execution to CodeIntelligenceService.analyze_file.@mcp.tool() @handle_mcp_tool_errors(return_type='dict') def get_file_summary(file_path: str, ctx: Context) -> Dict[str, Any]: """ Get a summary of a specific file, including: - Line count - Function/class definitions (for supported languages) - Import statements - Basic complexity metrics """ return CodeIntelligenceService(ctx).analyze_file(file_path)
- Business logic helper: analyze_file method in CodeIntelligenceService, called by handler. Validates input, fetches summary from index_manager.get_file_summary, handles missing index case.def analyze_file(self, file_path: str) -> Dict[str, Any]: """ Analyze a file and return comprehensive intelligence. This is the main business method that orchestrates the file analysis workflow, choosing the best analysis strategy and providing rich insights about the code. Args: file_path: Path to the file to analyze (relative to project root) Returns: Dictionary with comprehensive file analysis Raises: ValueError: If file path is invalid or analysis fails """ # Business validation self._validate_analysis_request(file_path) # Use the global index manager index_manager = get_index_manager() # Debug logging logger.info(f"Getting file summary for: {file_path}") logger.info(f"Index manager state - Project path: {index_manager.project_path}") logger.info(f"Index manager state - Has builder: {index_manager.index_builder is not None}") if index_manager.index_builder: logger.info(f"Index manager state - Has index: {index_manager.index_builder.in_memory_index is not None}") # Get file summary from JSON index summary = index_manager.get_file_summary(file_path) logger.info(f"Summary result: {summary is not None}") # If deep index isn't available yet, return a helpful hint instead of error if not summary: return { "status": "needs_deep_index", "message": "Deep index not available. Please run build_deep_index before calling get_file_summary.", "file_path": file_path } return summary
- Core implementation helper: get_file_summary in SQLiteIndexManager. Queries DB for file metadata and symbols, categorizes into functions/classes/methods, constructs full summary dict.def get_file_summary(self, file_path: str) -> Optional[Dict[str, Any]]: """Return summary information for a file from SQLite storage.""" with self._lock: if not isinstance(file_path, str): logger.error("File path must be a string, got %s", type(file_path)) return None if not self.store or not self._is_loaded: if not self.load_index(): return None normalized = _normalize_path(file_path) with self.store.connect() as conn: row = conn.execute( """ SELECT id, language, line_count, imports, exports, docstring FROM files WHERE path = ? """, (normalized,), ).fetchone() if not row: logger.warning("File not found in index: %s", normalized) return None symbol_rows = conn.execute( """ SELECT type, line, signature, docstring, called_by, short_name FROM symbols WHERE file_id = ? ORDER BY line ASC """, (row["id"],), ).fetchall() imports = _safe_json_loads(row["imports"]) exports = _safe_json_loads(row["exports"]) categorized = _categorize_symbols(symbol_rows) return { "file_path": normalized, "language": row["language"], "line_count": row["line_count"], "symbol_count": len(symbol_rows), "functions": categorized["functions"], "classes": categorized["classes"], "methods": categorized["methods"], "imports": imports, "exports": exports, "docstring": row["docstring"], }