依赖分析-MCP添加福利群:解决AI开发者的「MCP实战痛点」一个模型上下文协议(MCP)服务器,用于分析代码库以生成依赖图和架构洞察。该服务器有助于理解多种编程语言中的代码结构、依赖关系和架构模式。
npm install
npm run build
在您的 MCP 设置文件中添加配置(通常位于 ~/.config/cline/mcp_settings.json 或等效位置):
json { mcpServers: { \DependencyMCP: { \command: \node, \args: [\path/to/dependency-mcp/dist/index.js], \env: { \MAX_LINES_TO_READ: \1000, \CACHE_DIR: \path/to/dependency-mcp/.dependency-cache, \CACHE_TTL: \3600000 } } }
环境变量:
分析代码库中的依赖关系并生成依赖图。
const result = await client.callTool("DependencyMCP", "analyze_dependencies", {
path: "/path/to/project",
excludePatterns: ["node_modules", "dist"], // optional
maxDepth: 10, // optional
fileTypes: [".ts", ".js", ".cs"] // optional
});

以 JSON 或 DOT 格式获取代码库的依赖图。
const result = await client.callTool("DependencyMCP", "get_dependency_graph", {
path: "/path/to/project",
format: "dot" // or "json" (default)
});

获取特定文件的详细元数据。
const result = await client.callTool("DependencyMCP", "get_file_metadata", {
path: "/path/to/file.ts"
});

根据架构规则和模式对代码库进行评分。
const result = await client.callTool("DependencyMCP", "get_architectural_score", {
path: "/path/to/project",
rules: [
{
pattern: "src/domain/**/*",
allowed: ["src/domain/**/*"],
forbidden: ["src/infrastructure/**/*"]
}
]
});

{
"src/index.ts": {
"path": "src/index.ts",
"imports": ["./utils", "./services/parser"],
"exports": ["analyze", "generateGraph"],
"namespaces": [],
"architecturalLayer": "Infrastructure",
"dependencies": ["src/utils.ts", "src/services/parser.ts"],
"dependents": []
}
}

{
"score": 85,
"violations": [
"src/domain/user.ts -> src/infrastructure/database.ts violates architectural rules"
],
"details": "Score starts at 100 and deducts 5 points per violation"
}

该服务器使用 TypeScript 构建,并使用以下技术:
dependency-mcp/ ├── src/ │ └── index.mts # Main server implementation ├── package.json ├── tsconfig.json └── README.md
要为新的编程语言添加支持:
fileTypes 数组中parseFileImports 和 parseFileExports 中实现特定于语言的正则表达式模式inferArchitecturalLayer 添加任何特定于语言的架构模式MIT