get_source_relation
Analyze source code dependencies to identify relationships between files in a specified directory, aiding in dependency management and project structure understanding.
Instructions
Analyze dependencies between source files
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Implementation Reference
- source_relation.py:75-96 (handler)The main handler function for the 'get_source_relation' tool. Decorated with @mcp.tool(), it processes the input path, initializes SourceAnalyzer, computes dependencies recursively for files or directory-wide, and returns formatted JSON.@mcp.tool() def get_source_relation(path: str) -> str: """Analyze dependencies between source files""" path_obj = Path(path) base_dir = str(path_obj.parent if path_obj.is_file() else path_obj) analyzer = SourceAnalyzer(base_dir) # ソースコードを解析 if path_obj.is_file(): analyzed_files: Set[str] = set() file_path = str(path_obj.absolute()) dependencies = analyze_dependencies_recursively( analyzer, file_path, analyzed_files ) else: dependencies = analyzer.analyze_directory() # 結果をまとめる result = {"dependencies": dependencies} return json.dumps(result, indent=2, ensure_ascii=False)
- source_relation.py:14-49 (helper)Recursive helper function used by the handler to compute full dependency tree for a starting file by calling analyzer.analyze_single_file and recursing.def analyze_dependencies_recursively( analyzer: SourceAnalyzer, file_path: str, analyzed_files: Set[str] ) -> Dict[str, List[str]]: """ファイルの依存関係を再帰的に解析する Args: analyzer (SourceAnalyzer): 解析を行うアナライザーインスタンス file_path (str): 解析対象のファイルパス analyzed_files (Set[str]): 解析済みファイルのセット Returns: Dict[str, List[str]]: 解析されたすべての依存関係の辞書 """ if file_path in analyzed_files: return {} analyzed_files.add(file_path) dependencies: Dict[str, List[str]] = {} # ファイルを解析 try: current_deps = analyzer.analyze_single_file(Path(file_path)) direct_dependencies = current_deps.get(file_path, []) dependencies[file_path] = direct_dependencies # 各依存ファイルについても再帰的に解析 for dependency in direct_dependencies: if dependency not in analyzed_files: nested_deps = analyze_dependencies_recursively( analyzer, dependency, analyzed_files ) dependencies.update(nested_deps) except Exception: pass return dependencies
- src/source_analyzer.py:104-116 (helper)Key method in SourceAnalyzer called by the recursive helper to analyze a single file and get its recursive dependencies.def analyze_single_file(self, file_path: Path) -> Dict[str, List[str]]: """単一のファイルを解析する Args: file_path (Path): 解析対象のファイルパス Returns: Dict[str, List[str]]: ファイルの完全な依存関係(直接および間接的な依存関係を含む) """ self.analyze_file(file_path) normalized_path = self.normalize_path(file_path) return {normalized_path: self.get_recursive_dependencies(normalized_path)}
- src/source_analyzer.py:117-144 (helper)Key method in SourceAnalyzer called directly by the handler to analyze all source files in a directory and compute their recursive dependencies.def analyze_directory(self) -> Dict[str, List[str]]: """ディレクトリ全体を解析する Returns: Dict[str, List[str]]: ディレクトリ内のファイルの完全な依存関係 """ # すべてのアナライザーの対象拡張子を収集 all_extensions = [] for analyzer in self.analyzers: all_extensions.extend(analyzer.file_extensions) # ファイルを収集(srcディレクトリが存在する場合はそこから、存在しない場合はbase_dirから) target_files = [] for ext in all_extensions: target_files.extend(self.src_dir.rglob(f"*{ext}")) # ファイルを解析 for file_path in target_files: self.analyze_file(file_path) # 各ファイルの再帰的な依存関係を取得 complete_dependencies = {} for file_path in self.dependencies.keys(): complete_dependencies[file_path] = self.get_recursive_dependencies( file_path ) return complete_dependencies