analyze_structure
Analyzes project directory structures and displays them as tree diagrams to visualize file organization and hierarchy.
Instructions
프로젝트 디렉토리 구조를 분석하여 트리 형태로 보여줍니다.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | 분석할 디렉토리 경로 | |
| maxDepth | No | 최대 깊이 (1-10). 기본값: 전체 | |
| showHidden | No | 숨김 파일/폴더 표시 여부. 기본값: false |
Implementation Reference
- src/project-analyzer.ts:116-147 (handler)Core handler function implementing the directory structure analysis logic, including tree building and stats computation.export function analyzeStructure( targetPath: string, options: StructureOptions = {} ): StructureResult { const { maxDepth = Infinity, showHidden = false } = options; try { if (!fs.existsSync(targetPath)) { return { success: false, path: targetPath, error: `경로를 찾을 수 없습니다: ${targetPath}`, }; } const stats: DirectoryStats = { totalFiles: 0, totalDirs: 0 }; const tree = buildTree(targetPath, "", 0, maxDepth, showHidden, stats); return { success: true, path: targetPath, tree, stats, }; } catch (err) { return { success: false, path: targetPath, error: err instanceof Error ? err.message : String(err), }; } }
- src/index.ts:417-457 (registration)MCP server.tool registration for 'analyze_structure', defining input schema with Zod and wrapper that invokes the core handler.server.tool( "analyze_structure", "프로젝트 디렉토리 구조를 분석하여 트리 형태로 보여줍니다.", { path: z.string().describe("분석할 디렉토리 경로"), maxDepth: z .number() .int() .min(1) .max(10) .optional() .describe("최대 깊이 (1-10). 기본값: 전체"), showHidden: z .boolean() .optional() .describe("숨김 파일/폴더 표시 여부. 기본값: false"), }, async ({ path: targetPath, maxDepth, showHidden }) => { const result = analyzeStructure(targetPath, { maxDepth, showHidden }); if (!result.success) { return { content: [{ type: "text", text: `오류: ${result.error}` }], isError: true, }; } const statsText = result.stats ? `\n\n📊 통계: ${result.stats.totalFiles}개 파일, ${result.stats.totalDirs}개 폴더` : ""; return { content: [ { type: "text", text: `📁 ${result.path}\n\n${result.tree}${statsText}`, }, ], }; } );
- src/project-analyzer.ts:16-38 (schema)TypeScript interfaces defining input options (StructureOptions), stats (DirectoryStats), and output result (StructureResult) for the analyzeStructure function.export interface StructureOptions { maxDepth?: number; showHidden?: boolean; } /** * 디렉토리 통계 */ export interface DirectoryStats { totalFiles: number; totalDirs: number; } /** * 디렉토리 구조 분석 결과 */ export interface StructureResult { success: boolean; path: string; tree?: string; stats?: DirectoryStats; error?: string; }
- src/project-analyzer.ts:152-200 (helper)Recursive helper function to build the visual directory tree string representation.function buildTree( dirPath: string, prefix: string, depth: number, maxDepth: number, showHidden: boolean, stats: DirectoryStats ): string { if (depth >= maxDepth) { return ""; } let result = ""; const entries = fs.readdirSync(dirPath, { withFileTypes: true }); // 숨김 파일 필터링 및 정렬 (디렉토리 먼저) const filtered = entries .filter((e) => showHidden || !e.name.startsWith(".")) .sort((a, b) => { if (a.isDirectory() && !b.isDirectory()) return -1; if (!a.isDirectory() && b.isDirectory()) return 1; return a.name.localeCompare(b.name); }); for (let i = 0; i < filtered.length; i++) { const entry = filtered[i]; const isLast = i === filtered.length - 1; const connector = isLast ? "└── " : "├── "; const newPrefix = prefix + (isLast ? " " : "│ "); if (entry.isDirectory()) { stats.totalDirs++; result += `${prefix}${connector}${entry.name}/\n`; result += buildTree( path.join(dirPath, entry.name), newPrefix, depth + 1, maxDepth, showHidden, stats ); } else { stats.totalFiles++; result += `${prefix}${connector}${entry.name}\n`; } } return result; }