代码上下文MCP服务器
一个模型上下文协议(MCP)服务器,用于从本地git存储库提供代码上下文。此服务器允许您:
- 在本地克隆git存储库
- 处理分支和文件
- 为代码块生成嵌入
- 对代码执行语义搜索
特性
- 使用本地git存储库而不是GitHub API
- 将数据存储在SQLite数据库中
- 将代码拆分为语义块
- 使用Ollama为代码块生成嵌入
- 提供对代码的语义搜索
先决条件
- Node.js(v16+)
- Git
- Ollama与嵌入模型
安装
# Clone the repository
git clone
cd code-context-mcp
# Install dependencies
npm install
# Build the project
npm run build配置
设置以下环境变量:
DATA_DIR:SQLite数据库的目录(默认:“~/.codeContextMcp/data”)REPO_CACHE_DIR:克隆存储库的目录(默认:“~/.codeContextMcp/repos”)
使用Olama
为了实现更快、更强大的嵌入,您可以使用Ollama:
# Install Ollama from https://ollama.ai/
# Pull an embedding model (unclemusclez/jina-embeddings-v2-base-code is recommended)
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", // Optional - defaults to repository's default branch
"query": "Your search query",
"keywords": ["keyword1", "keyword2"], // Optional - filter results by keywords
"filePatterns": ["**/*.ts", "src/*.js"], // Optional - filter files by glob patterns
"excludePatterns": ["**/node_modules/**"], // Optional - exclude files by glob patterns
"limit": 10 // Optional - number of results to return, default: 10
}这 branch 参数是可选的。如果没有提供,该工具将自动使用存储库的默认分支。
这 keywords 参数是可选的。如果提供,结果将被筛选为仅包含至少包含一个指定关键字的块(不区分大小写的匹配)。
这 filePatterns 和 excludePatterns 参数是可选的。它们允许您使用glob模式过滤哪些文件被处理和搜索(例如。, **/*.ts 对于所有TypeScript文件)。
数据库模式
服务器使用具有以下模式的SQLite:
repository:存储有关存储库的信息branch:存储有关分支机构的信息file:存储有关文件的信息branch_file_association:将文件与分支关联file_chunk:存储代码块及其嵌入
调试
MAC Mx系列-ARM架构问题
在Mac M系列芯片(ARM架构)上安装better squelite3时,如果遇到“mach-o文件,但是不兼容的架构(有'x86_64',需要'arm64e'或'arm64')”等错误,您需要确保二进制文件与您的架构匹配。以下是解决此问题的方法:
# Check your Node.js architecture
node -p "process.arch"
# If it shows 'arm64', but you're still having issues, try:
npm rebuild better-sqlite3 --build-from-source
# Or for a clean install:
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奥利玛植入物测试
卷曲http://localhost:11434/api/embed-d'{“model”:“unclemusclez/jina-embeddings-v2-base-code”,“input”:“Llamas是骆驼科的成员”} 卷曲http://127.0.01:11434/api/embed-d'{“model”:“unclemusclez/jina-embeddings-v2-base-code”,“input”:“Llamas是camelid家族的成员”} curl http://\[::1\]:11434/api/embed-d'{“模型”:“uncemusclez/jina-embeddings-v2-base-code”,“输入”:“骆驼是骆驼科的成员”}
许可证
麻省理工学院
