get_source_relation
Analyze dependencies between source files to identify relationships and manage project structure effectively.
Instructions
Analyze dependencies between source files
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Implementation Reference
- source_relation.py:75-95 (handler)The main handler function for the 'get_source_relation' tool. It is decorated with @mcp.tool() which registers it as an MCP tool. The function creates a SourceAnalyzer instance, analyzes the source file or directory for dependencies (recursively if file), and returns a JSON string with the dependencies graph.@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)Helper function used by the handler to recursively analyze dependencies starting from a given file, building a full dependency dictionary by calling analyzer.analyze_single_file and recursing on direct dependencies.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-115 (helper)Key method in SourceAnalyzer called by the recursive helper to analyze a single file's direct dependencies and compute recursive ones.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)}