mcpField
C
Code Context MCP Server

Code Context MCP Server

本地服务
github.com/fkesheh开发工具2669 周前更新

关于 Code Context MCP Server

Code Context MCP Server 是一个 MCP 服务,用于从本地 git 仓库提供代码上下文。它会克隆仓库、处理分支和文件、为代码块生成嵌入向量,并支持语义搜索。适合希望基于本地仓库与 Ollama 嵌入模型进行代码检索的开发者。

主要功能

  1. 01克隆本地 git 仓库并处理分支与文件
  2. 02将代码切分为语义块
  3. 03使用 Ollama 生成代码嵌入向量
  4. 04基于语义的代码搜索
  5. 05支持 glob 过滤与关键词过滤

适用场景

  1. 01在本地仓库中按语义查找相关代码
  2. 02为 MCP 客户端提供特定分支的代码上下文

MCP 相关

该项目是 MCP 服务,通过 queryRepo 工具向 MCP 客户端返回仓库的语义搜索结果。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

暂未收录安装方式

我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。

代码仓库

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

Code Context MCP 服务

一个 Model Context Protocol (MCP) 服务,用于从本地 git 仓库提供代码上下文。该服务允许你:

  1. 在本地克隆 git 仓库
  2. 处理分支和文件
  3. 为代码块生成嵌入
  4. 对代码执行语义搜索

功能

  • 使用本地 git 仓库,而非 GitHub API
  • 将数据存储在 SQLite 数据库中
  • 将代码拆分为语义块
  • 使用 Ollama 为代码块生成嵌入
  • 提供对代码的语义搜索

先决条件

  • Node.js (v16+)
  • Git
  • 带有嵌入模型的 Ollama

安装

# Clone the repository
git clone <repository-url>
cd code-context-mcp

# Install dependencies
npm install

# Build the project
npm run build

配置

设置以下环境变量:

  • DATA_DIR:SQLite 数据库的目录(默认:'~/.codeContextMcp/data')
  • REPO_CACHE_DIR:克隆仓库的目录(默认:'~/.codeContextMcp/repos')
使用 Ollama

为了获得更快、更强大的嵌入,你可以使用 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-sqlite3 时,如果遇到诸如 "mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e' or '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

测试 Ollama 嵌入

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

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。