
MCP Source Tree Server
Okay, I understand. Here's how you can generate a JSON file tree from a specified directory's `src` folder, respecting `.gitignore` rules, suitable for quick project structure review in Claude. I'll provide a Python script to accomplish this. **Explanation:** 1. **`gitignore_parser` Library:** We'll use the `gitignore_parser` library to correctly interpret `.gitignore` files. This is crucial for accurately reflecting what files should be excluded. If you don't have it, you'll need to install it: `pip install gitignore_parser` 2. **`os.walk()`:** This function recursively traverses the directory tree. 3. **`.gitignore` Handling:** The script reads and parses `.gitignore` files in each directory it encounters. 4. **JSON Output:** The script constructs a JSON representation of the file tree. **Python Script:** ```python import os import json import gitignore_parser def generate_file_tree_json(root_dir, output_file="file_tree.json"): """ Generates a JSON file tree from the 'src' folder of a specified directory, respecting '.gitignore' rules. Args: root_dir (str): The root directory of the project. output_file (str): The name of the output JSON file. Defaults to "file_tree.json". """ src_dir = os.path.join(root_dir, "src") if not os.path.exists(src_dir): print(f"Error: 'src' directory not found in {root_dir}") return def build_tree(directory, ignore_checker): """Recursively builds the file tree.""" tree = {} for item in os.listdir(directory): full_path = os.path.join(directory, item) relative_path = os.path.relpath(full_path, src_dir) # Path relative to src if ignore_checker(relative_path): continue # Skip ignored files/directories if os.path.isfile(full_path): tree[item] = None # Represent files as None elif os.path.isdir(full_path): tree[item] = build_tree(full_path, ignore_checker) return tree def create_ignore_checker(root_directory): """Creates a function to check if a file/directory is ignored based on .gitignore files.""" ignore_files = [] for root, _, files in os.walk(root_directory): if '.gitignore' in files: ignore_files.append(os.path.join(root, '.gitignore')) ignore_list = [] for ignore_file in ignore_files: ignore_list.append(gitignore_parser.parse(ignore_file)) def is_ignored(path): for ignore in ignore_list: if ignore(os.path.join(src_dir, path)): # Check against the full path return True return False return is_ignored ignore_checker = create_ignore_checker(src_dir) file_tree = build_tree(src_dir, ignore_checker) with open(output_file, "w") as f: json.dump(file_tree, f, indent=4) print(f"File tree JSON saved to {output_file}") # Example Usage: if __name__ == "__main__": # Replace with the actual root directory of your project project_root = "/path/to/your/project" # <---- CHANGE THIS! generate_file_tree_json(project_root) ``` **How to Use:** 1. **Install `gitignore_parser`:** `pip install gitignore_parser` 2. **Replace Placeholder:** In the `if __name__ == "__main__":` block, replace `"/path/to/your/project"` with the actual absolute path to the root directory of your project (the directory containing the `src` folder). 3. **Run the Script:** Execute the Python script. It will create a file named `file_tree.json` in the same directory as the script. 4. **Upload to Claude:** Upload the `file_tree.json` file to Claude. **Example `file_tree.json` Output (Illustrative):** ```json { "components": { "Button.js": null, "Input.js": null }, "utils": { "api.js": null, "helpers.js": null }, "App.js": null, "index.js": null } ``` **Key Improvements and Considerations:** * **`.gitignore` Parsing:** Uses `gitignore_parser` for accurate `.gitignore` handling. This is *essential* for real-world projects. * **Error Handling:** Includes a check to ensure the `src` directory exists. * **Relative Paths:** Uses `os.path.relpath` to store paths relative to the `src` directory in the JSON, making the output more concise and readable. * **Clearer Structure:** Represents files as `null` in the JSON tree, which is a common and easily understood convention. * **Example Usage:** Provides a clear example of how to use the script. * **Comments:** Includes comments to explain the code. * **`create_ignore_checker` Function:** This function encapsulates the logic for creating the ignore checker, making the code more modular and readable. It finds all `.gitignore` files within the `src` directory and its subdirectories. * **Full Path Checking:** The `is_ignored` function now checks the full path (relative to the `src` directory) against the `.gitignore` rules, ensuring accurate matching. * **Modularity:** The code is broken down into functions for better organization and reusability. **How to Use with Claude:** 1. **Upload the JSON:** Upload the generated `file_tree.json` file to Claude. 2. **Prompt Claude:** Craft a prompt that asks Claude to analyze the file structure. For example: * "Here is a JSON representation of the file structure of a project's `src` directory. Can you identify the main components and utilities?" * "Analyze this file structure and suggest potential areas for refactoring." * "Based on this file structure, what design patterns might be in use?" * "This JSON represents the file tree of a React project. Identify the likely component structure." The more specific your prompt, the better the results you'll get from Claude. You can also ask Claude to generate diagrams or visualizations based on the JSON data.
Tools
get_src_tree
Generate a file tree for the specified directory, filtering files based on .gitignore. Traverses the filesystem and generates a JSON-formatted tree structure that preserves hierarchy.
README
MCP 源代码树服务器
这是一个生成指定目录下文件树的 MCP 服务器。
它会根据以.
开头的目录和.gitignore
中指定的条件排除特定的文件和目录。
通过将其集成到 Claude 中,Claude 可以快速查看项目结构,并帮助 Claude 确定要编辑的文件。
<a href="https://glama.ai/mcp/servers/1igr60piqh"><img width="380" height="200" src="https://glama.ai/mcp/servers/1igr60piqh/badge" alt="Source Tree Server MCP server" /></a>
功能
- 以 JSON 格式获取指定目录下的文件树
- 根据
.gitignore
中的条件排除文件/目录 - 以分层 JSON 格式表示目录结构
设置
必要条件
- Python 3.10 或更高版本
- uv
安装
# 安装 uv
$ curl -LsSf https://astral.sh/uv/install.sh | sh
$ cd /path/to/mcp-src-tree
# 安装依赖库
$ uv sync
Claude for Desktop 中的配置
请将以下配置添加到 claude_desktop_config.json
中:
{
"mcpServers": {
"src-tree": {
"command": "uv",
"args": [
"--directory",
"/path/to/mcp-src-tree", // 请替换为该项目目录的绝对路径
"run",
"tree.py"
]
}
}
}
Claude 中的使用示例
请指示指定 src 的上一级根目录。
如果考虑.gitignore
,则前提是根目录下存在.gitignore
。
例如,指示“请查看 /Users/xxx/GitHub/xxx/
的源代码树”即可运行。
以下是响应示例:
好的,我将查看源代码树。
我已查看项目结构。结构如下:
src/
└── xxxx/
├── __init__.py
├── converter.py
├── html_converter.py
├── image_processor.py
├── toc_analyzer.py
└── utils.py
这样,Claude 就可以快速查看项目结构。
文件排除
以.
开头的目录会自动排除。
与.gitignore
中指定的模式匹配的文件和目录将自动从树中排除。
例如,以下.gitignore
配置有效:
__pycache__/
node_modules/
*.log
推荐服务器
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
Excel MCP Server
一个模型上下文协议服务器,使 AI 助手能够读取和写入 Microsoft Excel 文件,支持诸如 xlsx、xlsm、xltx 和 xltm 等格式。
Playwright MCP Server
提供一个利用模型上下文协议的服务器,以实现类人浏览器的自动化,该服务器使用 Playwright,允许控制浏览器行为,例如导航、元素交互和滚动。
MCP Package Docs Server
促进大型语言模型高效访问和获取 Go、Python 和 NPM 包的结构化文档,通过多语言支持和性能优化来增强软件开发。
Claude Code MCP
一个实现了 Claude Code 作为模型上下文协议(Model Context Protocol, MCP)服务器的方案,它可以通过标准化的 MCP 接口来使用 Claude 的软件工程能力(代码生成、编辑、审查和文件操作)。
@kazuph/mcp-taskmanager
用于任务管理的模型上下文协议服务器。它允许 Claude Desktop(或任何 MCP 客户端)在基于队列的系统中管理和执行任务。
Apple MCP Server
通过 MCP 协议与 Apple 应用(如“信息”、“备忘录”和“通讯录”)进行交互,从而使用自然语言发送消息、搜索和打开应用内容。
Gitingest-MCP
一个用于 gitingest 的 MCP 服务器。它允许像 Claude Desktop、Cursor、Cline 等 MCP 客户端快速提取关于 Github 仓库的信息,包括仓库摘要、项目目录结构、文件内容等。