这是一个用于从本地Git仓库提供代码上下文的模型上下文协议(MCP)服务器。此服务器允许您:
# 克隆仓库
git clone <repository-url>
cd code-context-mcp
# 安装依赖
npm install
# 构建项目
npm run build
设置以下环境变量:
DATA_DIR:SQLite数据库目录(默认值:'~/.codeContextMcp/data')REPO_CACHE_DIR:克隆的仓库目录(默认值:'~/.codeContextMcp/repos')为了获得更快和更强大的嵌入表示,您可以使用Ollama:
# 从https://ollama.ai/安装Ollama
# 拉取一个嵌入模型(推荐使用unclemusclez/jina-embeddings-v2-base-code)
ollama pull unclemusclez/jina-embeddings-v2-base-code
在您的Claude Desktop配置文件(claude_desktop_config.json)中添加以下配置:
{
"mcpServers": {
"code-context-mcp": {
"command": "/path/to/your/node",
"args": ["/path/to/code-context-mcp/dist/index.js"]
}
}
}
该服务器提供了以下工具:
克隆仓库、处理代码并执行语义搜索:
{
"repoUrl": "https://github.com/username/repo.git",
"branch": "main", // 可选 - 默认为仓库的默认分支
"query": "您的搜索查询",
"keywords": ["关键词1", "关键词2"], // 可选 - 根据关键词过滤结果
"filePatterns": ["**/*.ts", "src/*.js"], // 可选 - 根据glob模式过滤文件
"excludePatterns": ["**/node_modules/**"], // 可选 - 根据glob模式排除文件
"limit": 1 0 // 可选 - 返回的结果数量,默认值:10
}
branch参数是可选的。如果没有提供,工具将自动使用仓库的默认分支。
keywords参数是可选的。如果提供,结果将被过滤以仅包含至少包含指定关键词之一的片段(不区分大小写匹配)。
filePatterns和excludePatterns参数是可选的。它们允许您根据glob模式过滤要处理和搜索的文件(例如,**/*.ts用于所有TypeScript文件)。
服务器使用具有以下模式的SQLite:
repository:存储关于仓库的信息branch:存储关于分支的信息file:存储关于文件的信息branch_file_association:关联文件与分支file_chunk:存储代码片段及其嵌入表示当在Mac M系列芯片(ARM架构)上安装better-sqlite3时,如果您遇到错误如“mach-o文件,但具有不兼容的架构(需要'arm64e'或'arm64',但已有的是'x86_64')”,您需要确保二进制文件与您的架构相匹配。以下是解决此问题的方法:
# 检查您的Node.js架构
node -p "process.arch"
# 如果显示'arm64',但您仍然存在问题,请尝试:
npm rebuild better-sqlite3 --build-from-source
# 或者进行干净安装:
npm uninstall better-sqlite3
export npm_config_arch=arm64
export npm_config_target_arch=arm64
npm install better-sqlite3 --build-from-source
如果您正在使用Rosetta,请确保您的整个环境是一致的。您的错误显示构建了x86_64二进制文件,但您的系统需要arm64。 对于持久配置,在您的.zshrc或.bashrc中添加:
export npm_config_arch=arm64
export npm_config_target_arch=arm64
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.01: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