# 代码上下文MCP
## 基本信息
- Slug: `fkesheh-code-context-mcp`
- Source: modelscope
- Publisher: @fkesheh/code-context-mcp
- Categories: version-control / search / developer-tools
- Hosted: No
- License: MIT License
- Source URL: https://www.modelscope.cn/mcp/servers/@fkesheh/code-context-mcp
## 简介
一个MCP服务器，它在本地git仓库上提供语义搜索功能，使用户能够克隆仓库、处理分支并通过矢量化代码块搜索代码。
## 安装提示

```bash
# Clone the repository git clone <repository-url> cd code-context-mcp # Install dependencies npm install # Build the project npm run build
```

## MCP Server 详情

# Code Context MCP 服务器

Model Context Protocol (MCP) 服务器，用于从本地 git 仓库提供代码上下文。该服务器允许您：

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

## 特性

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

## 前提条件

- Node.js (v16+)
- Git
- Ollama 及其嵌入模型

## 安装

```bash
# 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：

```bash
# 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`) 中添加以下配置：

```json
{
  "mcpServers": {
    "code-context-mcp": {
      "command": "/path/to/your/node",
      "args": ["/path/to/code-context-mcp/dist/index.js"]
    }
  }
}
```


## 工具

服务器提供了以下工具：

### queryRepo

克隆仓库，处理代码，并执行语义搜索：

```json
{
  "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`：存储代码块及其嵌入

## 许可证

MIT

