语义代码搜索服务器

Code RAG MCP Server是一款基于语义理解的代码搜索工具,支持多语言代码搜索和本地嵌入模型,适用于开发效率提升和代码管理。

  • by Mirrdhyn
  • Go
  • 本地部署
  • 5 星标
  • 更新于
在 GitHub 查看 Mirrdhyn/code-rag-mcp

使用说明

代码RAG MCP服务器

用于语义代码搜索的MCP(模型上下文协议)服务器。用基于理解的代码搜索替换grep/ripgrep。

🚀 特性

  • ✅ 语义搜索:按概念查找代码,而不仅仅是按文本
  • ✅ 本地嵌入:使用LM Studio(不需要OpenAI)
  • ✅ 多语言:Go、Python、JS/TS、Terraform、YAML等。
  • ✅ MCP集成:与Claude Code和Zed兼容
  • ✅ HTTP API:Git钩子用于在提交时自动重新索引
  • ✅ 快速:使用Qdrant进行内存索引

📋 先决条件

  1. 转到1.22+

    brew install go
    
  2. Qdrant (矢量数据库)

    docker run -d --name qdrant \
      -p 6333:6333 -p 6334:6334 \
      -v $(pwd)/qdrant_data:/qdrant/storage \
      qdrant/qdrant
    
  3. LM 工作室 具有嵌入模型

    • 下载:https://lmstudio.ai
    • 负载型号: nomic-ai/nomic-embed-text-v1.5-GGUF
    • 在端口1234上启动本地服务器

🔧 安装

# 1. Clone the repo
git clone <your-repo>
cd code-rag-mcp

# 2. Install dependencies
go mod download

# 3. Build
go build -o code-rag-mcp

# 4. Configure
cp config.yaml ~/.config/code-rag-mcp/config.yaml
# Edit config.yaml with your code paths

# 5. Install globally (optional)
sudo cp code-rag-mcp /usr/local/bin/

⚙️ 配置

1.配置yaml

编辑 ~/.config/code-rag-mcp/config.yaml:

embedding_type: "local"
embedding_model: "nomic-ai/nomic-embed-text-v1.5-GGUF"
embedding_base_url: "http://localhost:1234/v1"
embedding_dim: 768

code_paths:
  - "/Users/you/projects"  # Your code directory

2.克劳德桌面

文件: ~/Library/Application Support/Claude/claude_desktop_config.json (macOS) 文件: ~/.config/Claude/claude_desktop_config.json (Linux)

{
  "mcpServers": {
    "code-rag": {
      "command": "/usr/local/bin/code-rag-mcp",
      "env": {
        "LM_STUDIO_URL": "http://localhost:1234/v1"
      }
    }
  }
}

修改后, 完全重新启动克劳德桌面 (退出+重新启动)。

3.克劳德密码

选项A:通过CLI(推荐) 🚀

# Add the code-rag MCP server
claude mcp add code-rag \
  --type stdio \
  --command /usr/local/bin/code-rag-mcp \
  --scope user

# Verify installation
claude mcp list
claude mcp get code-rag

选项B:手动配置

克劳德代码使用 同一文件 作为克劳德桌面:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json
{
  "mcpServers": {
    "code-rag": {
      "command": "/usr/local/bin/code-rag-mcp",
      "env": {
        "LM_STUDIO_URL": "http://localhost:1234/v1"
      }
    }
  }
}

完成复制粘贴命令 (建成后):

# 1. Install the binary
sudo cp code-rag-mcp /usr/local/bin/
sudo chmod +x /usr/local/bin/code-rag-mcp

# 2. Add to Claude Code
claude mcp add code-rag \
  --type stdio \
  --command /usr/local/bin/code-rag-mcp \
  --scope user \
  --env LM_STUDIO_URL=http://localhost:1234/v1

# 3. Verify
claude mcp list

4.Zed

文件: ~/.config/zed/settings.json

{
  "context_servers": {
    "code-rag": {
      "source": "custom",
      "command": {
        "path": "/usr/local/bin/code-rag-mcp",
        "args": [],
        "env": {
          "LM_STUDIO_URL": "http://localhost:1234/v1"
        }
      }
    }
  }
}

完成复制粘贴命令:

# 1. Install the binary (if not already done)
sudo cp code-rag-mcp /usr/local/bin/
sudo chmod +x /usr/local/bin/code-rag-mcp

# 2. Add configuration to Zed
cat << 'EOF' >> ~/.config/zed/settings.json
{
  "context_servers": {
    "code-rag": {
      "source": "custom",
      "command": {
        "path": "/usr/local/bin/code-rag-mcp",
        "args": [],
        "env": {
          "LM_STUDIO_URL": "http://localhost:1234/v1"
        }
      }
    }
  }
}
EOF

# 3. Restart Zed

备注:对于Zed,您还可以使用代理面板中的“添加自定义服务器”按钮。

🎯 用法

1.启动服务

# Terminal 1: Qdrant
docker start qdrant

# Terminal 2: LM Studio
# Open LM Studio UI and start the server

# Terminal 3: Test the server
./code-rag-mcp

2.在Claude中使用

Claude: Hi! Let me check the index status.
[Calls: get_index_stats]

You: Index my Terraform project
Claude: [Calls: index_codebase /path/to/terraform]

You: Where is the VPC configuration?
Claude: [Calls: semantic_code_search "VPC network configuration"]

3.示例查询

语义搜索:

"authentication middleware"
"database connection logic"
"error handling patterns"
"terraform AWS VPC modules"
"API rate limiting"

类似代码:

"Find code similar to: func (s *Server) HandleAuth()"

结合上下文进行解释:

"Explain handlers/auth.go with related dependencies"

🔍 可用的MCP工具

semantic_code_search

主要语义搜索。 使用而不是grep。

{
  "query": "authentication logic",
  "limit": 5,
  "min_score": 0.7,
  "language": "go"
}

find_similar_code

查找与给定代码段类似的代码。

{
  "code_snippet": "func HandleError(err error) { ... }",
  "limit": 5
}

explain_code_with_context

用上下文解释文件。

{
  "file_path": "/path/to/file.go",
  "focus": "dependencies"
}

index_codebase

为目录建立索引。 先运行这个。

{
  "path": "/Users/you/projects/myapp",
  "extensions": [".go", ".py"]
}

get_index_stats

检查索引状态。

🧪 测试

# Test embeddings
go run scripts/test_embeddings.go

# Test indexing
go run scripts/test_indexing.go /path/to/code

# Test searches
go run scripts/test_search.go "authentication"

📊 推荐的嵌入模型

型号 RAM 尺寸 质量 使用情况
nomic-embed-v1.5 (Q8) 548MB 768 ⭐⭐⭐⭐⭐ 推荐
bge-small-en-v1.5 133 MB 384 ⭐⭐⭐⭐ 重量轻
全迷你LM-L6-v2 90MB 384 ⭐⭐⭐ 快速

在LM Studio中下载

  1. 打开LM工作室
  2. 搜索:“nomic嵌入文本”
  3. 下载: nomic-ai/nomic-embed-text-v1.5-GGUF (Q8)
  4. 在“本地服务器”中加载模型

🐛 故障排除

无结果

# Check index
get_index_stats

# Re-index
index_codebase /path/to/code

# Lower minimum score
semantic_code_search "query" min_score=0.5

LM Studio连接失败

# Check if LM Studio is running
curl http://localhost:1234/v1/models

# Check logs
./code-rag-mcp --config config.yaml

Qdrant没有响应

# Restart Qdrant
docker restart qdrant

# Check logs
docker logs qdrant

📚 文档

🤝 贡献

欢迎投稿!看 贡献.md

📄 许可证

MIT许可证-请参阅 许可证

🙏 致谢