Skip to main content
Glama
owayo

MCP Source Relation Server

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
NameRequiredDescriptionDefault
pathYes

Implementation Reference

  • 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)
  • 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
  • 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)}
  • 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

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/owayo/mcp-source-relation'

If you have feedback or need assistance with the MCP directory API, please join our Discord server