使用说明
代码上下文MCP服务器
一种先进的模型上下文协议(MCP)服务器,为AI代理提供自动代码上下文和关系分析。该工具可帮助AI助手快速理解代码结构、依赖关系和关系,而无需逐一手动读取文件。
特性
- 多语言支持:TypeScript、JavaScript、TSX、JSX、Python、Java等
- 综合代码分析:
- 符号定义和签名
- 所有参考文献和用法
- 呼叫者/被呼叫者关系
- 进出口跟踪
- 呼叫链分析
- 快速性能:
- 基于树的AST解析
- Ripgrep用于闪电般快速的参考查找
- 智能缓存层
- 丰富的上下文:立即返回AI理解代码关系所需的一切
为什么是这个工具?
当AI代理处理代码库时,他们通常需要:
- 查找函数的定义位置
- 查看所有拨打电话的地方
- 了解它在内部的称呼
- 了解相关类型、参数、返回值
如果没有此工具,代理必须逐一手动读取文件。使用此工具,代理可以 即时、全面的背景 关于任何代码符号。
用例
1.功能签名更改
之前:代理更新函数签名,然后读取每个文件以更新调用站点。
使用代码上下文MCP:Agent会立即看到所有呼叫站点,并可以决定使用默认参数,而不是更新每次呼叫。
2.影响分析
之前:“如果我更改此功能,会发生什么故障?”需要手动探索文件。
使用代码上下文MCP:立即查看完整的呼叫链和所有依赖项。
3.理解模糊请求
之前:“修复身份验证错误”-代理盲目搜索。
使用代码上下文MCP:Agent立即获取文件上下文、相关函数和结构。
安装
npm install
npm run build
MCP配置
添加到您的MCP设置文件中(例如,Claude Desktop配置):
{
"mcpServers": {
"code-context": {
"command": "node",
"args": ["/path/to/code-context-mcp-server/dist/index.js"],
"env": {
"WORKSPACE_PATH": "/path/to/your/project"
}
}
}
}
或者独立运行:
WORKSPACE_PATH=/path/to/project node dist/index.js
可用工具
1. get_code_context
获取符号的全面上下文,包括定义、引用、调用者和被调用者。
输入:
{
"symbol": "functionName",
"filePath": "optional/specific/file.ts",
"options": {
"includeCallers": true,
"includeCallees": true,
"maxDepth": 3,
"contextLines": 3
}
}
输出:
{
"success": true,
"context": {
"symbol": {
"name": "functionName",
"type": "function",
"location": { ... },
"signature": "function functionName(arg1: string): void",
"language": "typescript"
},
"definition": {
"code": "...",
"location": { ... }
},
"references": [
{
"location": { ... },
"context": "line of code with reference",
"isDefinition": false,
"isWrite": false
}
],
"callers": [
{
"location": { ... },
"callerSymbol": "ClassName.methodName",
"context": "..."
}
],
"callees": [
{
"symbol": { ... },
"type": "calls",
"location": { ... }
}
],
"relatedSymbols": [ ... ],
"fileContext": {
"imports": [ ... ],
"exports": [ ... ],
"structure": { ... }
}
},
"metadata": {
"totalReferences": 15,
"totalCallers": 5,
"totalCallees": 8,
"analysisTimeMs": 45
}
}
示例使用:
Agent: "I need to change the arguments for calculateTotal function"
*Calls get_code_context with symbol="calculateTotal"*
Agent receives: definition, all 23 call sites, what it calls internally
Agent: "I see it's called in 23 places. I'll add a default parameter instead of updating all calls."
2. find_symbol
按名称或图案搜索符号。
输入:
{
"query": "calculate",
"type": "function",
"searchOptions": {
"maxResults": 50
}
}
输出:
{
"success": true,
"symbols": [
{
"name": "calculateTotal",
"type": "function",
"location": { ... },
"signature": "...",
"language": "typescript"
}
],
"metadata": {
"totalMatches": 12,
"searchTimeMs": 20
}
}
3. get_file_context
获取文件级上下文,包括导入、导出和结构。
输入:
{
"filePath": "src/utils/math.ts"
}
输出:
{
"success": true,
"context": {
"filePath": "src/utils/math.ts",
"language": "typescript",
"imports": [
{
"source": "./types",
"symbols": ["MathOptions", "Result"],
"location": { ... }
}
],
"exports": [ ... ],
"mainSymbols": [ ... ],
"structure": {
"classes": [ ... ],
"functions": [ ... ],
"variables": [ ... ],
"interfaces": [ ... ]
}
}
}
4. analyze_call_chain
分析调用链以查看函数之间的关系。
输入:
{
"symbol": "processData",
"direction": "both",
"maxDepth": 3
}
输出:
{
"success": true,
"chain": {
"symbol": { ... },
"depth": 0,
"children": [
{
"symbol": { ... },
"depth": 1,
"callSite": { ... },
"children": [ ... ]
}
]
},
"metadata": {
"totalNodes": 47,
"maxDepth": 3,
"analysisTimeMs": 120
}
}
5. reindex_workspace
在发生重大更改后强制重新索引工作区。
输入: {}
6. get_index_stats
获取索引代码库的统计信息。
输出:
{
"success": true,
"index": {
"totalSymbols": 1523,
"totalFiles": 243,
"symbolsByType": {
"function": 456,
"class": 123,
"method": 678,
"variable": 266
}
},
"cache": { ... },
"supportedLanguages": ["typescript", "javascript", "python", "java"],
"workspacePath": "/path/to/workspace"
}
示例场景
场景1:修改公共函数
用户请求: “更改 authenticateUser 也接受可选 role 参数“
没有代码上下文MCP:
- 查找函数定义
- 修改签名
- 搜索所有用法(手动文件读取)
- 逐一更新每个呼叫站点
使用代码上下文MCP:
- 呼叫
get_code_context随着symbol="authenticateUser" - 立即查看47个呼叫站点
- 分析大多数人不需要角色
- 添加默认参数
role?: string - 只更新需要它的3个呼叫
场景2:理解复杂的依赖关系
用户请求: “当 saveData 被称为?"
代理人:
1. get_code_context(symbol="saveData", options={ includeCallees: true, maxDepth: 2 })
2. Receives complete tree: saveData → validateData → checkSchema → ...
3. Provides clear explanation of data flow
情景3:影响分析
用户请求: “重命名安全吗 formatCurrency?"
代理人:
1. get_code_context(symbol="formatCurrency")
2. See all 8 files that reference it
3. Check if it's exported (yes)
4. Check external dependencies
5. Inform user: "It's exported in 2 places, affecting 8 files. I can safely rename with proper imports."
演出
- 初始指数:典型项目约2-5秒(取决于规模)
- 查询时间:大多数操作为10-100ms
- 缓存命中:对于重复查询,<5ms
- 增量更新:文件可以单独重新索引
建筑
┌─────────────────────────────────────────┐
│ MCP Server (index.ts) │
│ (Exposes tools via MCP protocol) │
└────────────────┬────────────────────────┘
│
┌────────────┼────────────┐
│ │ │
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌──────────┐
│ Indexer │ │Analyzer │ │ Tools │
│ │ │ │ │ │
│ Symbol │ │ Call │ │ Context │
│ Index │ │ Chain │ │ Response │
└────┬────┘ └────┬────┘ └────┬─────┘
│ │ │
└────────────┼────────────┘
│
┌────────┴────────┐
│ │
▼ ▼
┌─────────┐ ┌──────────┐
│ Parsers │ │ Ripgrep │
│ (Tree- │ │ (Fast │
│ sitter) │ │ Search) │
└─────────┘ └──────────┘
支持的语言
- TypeScript(.ts)
- JavaScript(.js、.mjs、.cjs)
- TSX(.TSX)-React TypeScript
- JSX(.JSX)-React JavaScript
- Python(.py)
- Java(.Java)
通过使用树型语法实现解析器,可以添加更多的语言。
需求
- Node.js 18+
- 可选但推荐:
ripgrep(rg)更快的搜索- 安装:
brew install ripgrep(macOS)或apt install ripgrep(Linux) - 如果不可用,则返回grep
- 安装:
发展
# Install dependencies
npm install
# Build
npm run build
# Watch mode
npm run watch
# Test with MCP inspector
npm run inspector
故障排除
初始索引速度慢
- 大型代码库(10k+文件)正常
- 索引一次构建并缓存
- 使用
reindex_workspace仅在需要时
缺少引用
- 确保安装了ripgrep以获得最佳效果
- 检查是否支持文件类型
- 尝试
reindex_workspace如果代码发生了显著变化
语言不受支持
- 检查
get_index_stats查看支持的语言 - 通过扩展实现新的解析器
BaseParser
贡献
要添加对新语言的支持,请执行以下操作:
- 安装树保姆语法:
npm install tree-sitter-<language> - 在中创建解析器
src/parsers/<Language>Parser.ts - 扩展
BaseParser类 - 实施
walkTree方法 - 增添
ParserFactory
许可证
麻省理工学院
鸣谢
内置:
