article
README
🚀 代码上下文MCP服务器
代码上下文MCP服务器是一个提供来自本地Git仓库代码上下文的模型上下文协议(MCP)服务器。它能让您在本地克隆Git仓库、处理分支和文件、为代码片段生成嵌入,还能在代码上执行语义搜索,极大提升代码处理和搜索效率。
🚀 快速开始
本服务器允许您完成以下操作:
- 本地克隆Git仓库
- 处理分支和文件
- 为代码片段生成嵌入
- 在代码上执行语义搜索
✨ 主要特性
- 使用本地Git仓库而不是GitHub API
- 将数据存储在SQLite数据库中
- 将代码分割为语义块
- 使用Ollama生成代码片段的嵌入
- 提供对代码的语义搜索
📦 安装指南
先决条件
- Node.js (v16+)
- Git
- 带有嵌入式模型的Ollama
安装步骤
# 克隆仓库
git clone <repository-url>
cd code-context-mcp
# 安装依赖项
npm install
# 构建项目
npm run build
📚 详细文档
配置
设置以下环境变量:
DATA_DIR:SQLite数据库目录(默认:~/.codeContextMcp/data)REPO_CACHE_DIR:克隆的仓库目录(默认:~/.codeContextMcp/repos)
使用Ollama
为了更快且更强大的嵌入,您可以使用Ollama:
# 从https://ollama.ai/安装Ollama
# 拉取一个嵌入模型(unclemusclez/jina-embeddings-v2-base-code推荐)
ollama pull unclemusclez/jina-embeddings-v2-base-code
使用方法
与Claude Desktop一起使用
将以下配置添加到您的Claude Desktop配置文件(claude_desktop_config.json)中:
{
"mcpServers": {
"code-context-mcp": {
"command": "/path/to/your/node",
"args": ["/path/to/code-context-mcp/dist/index.js"]
}
}
}
工具
服务器提供以下工具:
queryRepo
克隆仓库,处理代码,并执行语义搜索:
{
"repoUrl": "https://github.com/username/repo.git",
"branch": "main", // 可选 - 默认为仓库的默认分支
"query": "您的搜索查询",
"keywords": ["keyword1", "keyword2"], // 可选 - 按关键字过滤结果
"filePatterns": ["**/*.ts", "src/*.js"], // 可选 - 使用glob模式过滤文件
"excludePatterns": ["**/node_modules/**"], // 可选 - 排除文件的glob模式
"limit": 10 // 可选 - 结果数量,默认为10
}
调试
针对Mac M系列芯片的ARM架构问题:
# 尝试以下命令:
curl http://localhost:11434/api/embed -d '{"model":"unclemusclez/jina-embeddings-v2-base-code","input":"Llamas are members of the camelid family"}'
curl http://127.0.0.1:11434/api/embed -d '{"model":"unclemusclez/jina-embeddings-v2-base-code","input":"Llamas are members of the camelid family"}'
curl http://[::1]:11434/api/embed -d '{"model":"unclemusclez/jina-embeddings-v2-base-code","input":"Llamas are members of the camelid family"}'
📄 许可证
本项目采用MIT许可证。
Scan to join WeChat group