使用说明
代码上下文mcp
用于语义代码搜索和依赖图分析的MCP服务器。使用向量嵌入(pgvector+Ollama)将代码库索引到知识图(Apache AGE)中,以实现人工智能驱动的代码理解。
支持的语言
- TypeScript (
.ts,.tsx)--函数、类、类型、接口、导入 - 锈 (
.rs)--函数、结构、枚举、特征、impl块、模块、use声明 - Solidity (
.sol)--合约、函数、事件、修饰符、结构、枚举、接口、导入 - C -函数、枚举
先决条件
- 码头工人 和 Docker Compose
- Node.js >= 18
快速开始
# Start PostgreSQL (with AGE + pgvector) and Ollama
docker-compose up -d
# Install dependencies and build
npm install
npm run build
为代码库编制索引
# Index a directory (project name auto-derived from directory basename)
node dist/cli/index.js index ./src
# Override the project name
node dist/cli/index.js index ./src --project my-project
# Index multiple codebases
node dist/cli/index.js index /path/to/frontend --project frontend
node dist/cli/index.js index /path/to/backend --project backend
索引器将:
- 使用树形图解析所有支持的源文件(TypeScript、Rust、Solidity)
- 构建一个包含函数、类、类型、契约、特征及其关系的代码图
- 通过Ollama生成语义搜索的向量嵌入
- 在索引过程中显示进度条
在同一目录上重新运行索引器会跳过未更改的文件(通过内容哈希检测到)。
Claude桌面配置
添加到您的Claude桌面配置(~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"code-context": {
"command": "node",
"args": ["dist/mcp/server.js"],
"cwd": "/path/to/code-context-mcp",
"env": {
"PGHOST": "localhost",
"PGPORT": "5433",
"PGDATABASE": "rag_db",
"PGUSER": "postgres",
"PGPASSWORD": "postgres",
"OLLAMA_URL": "http://localhost:11434"
}
}
}
}
MCP工具
list_projects
列出所有索引项目及其节点计数。
参数: 无
search_code
在索引代码库中进行语义搜索。按含义查找函数、类、类型和导入。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
query |
string | yes | 自然语言搜索查询 |
limit |
number | no | 最大结果数(默认值:10) |
project |
string | no | 项目范围搜索 |
get_call_stack
从函数中跟踪调用依赖树。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
functionName |
string | yes | 要跟踪的函数 |
depth |
number | no | 最大遍历深度,1-10(默认值:3) |
project |
string | no | 项目范围 |
get_function_context
收集函数的完整上下文:调用者、被调用者、它使用的类型。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
functionName |
string | yes | 获取上下文的函数 |
project |
string | no | 项目范围 |
get_impact_analysis
分析文件更改的影响。显示依赖于文件中定义的所有外部代码。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
filePath |
string | yes | 文件的相对路径(按索引) |
project |
string | no | 项目范围 |
环境变量
| 变量 | 默认值 | 描述 |
|---|---|---|
PGHOST |
localhost |
PostgreSQL主机 |
PGPORT |
5433 |
PostgreSQL端口 |
PGDATABASE |
rag_db |
数据库名称 |
PGUSER |
postgres |
数据库用户 |
PGPASSWORD |
postgres |
数据库密码 |
OLLAMA_URL |
http://localhost:11434 |
API终点 |
测试
单元测试
单元测试在没有Docker的情况下运行,包括提取器、扫描程序、注册表、进度条、嵌入和数据库查询。
npm test # Run all unit tests
npm run test:watch # Watch mode
集成测试
集成测试需要单独的Docker容器,以避免干扰开发数据。
# Start test infrastructure (separate ports: PG=5434, Ollama=11435)
docker compose -f docker-compose.test.yml up -d
# Run integration tests
npm run test:integration
集成测试涵盖了完整的索引管道和多语言图填充。