本地语义代码搜索工具

一个100%本地的语义代码搜索工具,支持多语言代码搜索,无需云端API,保护代码隐私且完全免费。

  • by ahmedibrahim085
  • Shell
  • 本地部署
  • 更新于
在 GitHub 查看 ahmedibrahim085/farhan-code-search-template

使用说明

FarhanAliRaza代码搜索-项目模板

此模板存储库提供 100%本地语义代码搜索 使用 FarhanAliRaza/claude当地背景 对于任何项目。

✨ 所得

  • 🔍 语义代码搜索:按含义查找代码,而不仅仅是关键字
  • 🔒 100%私人:没有云API,你的代码永远不会离开你的机器
  • 💰 零成本:不收取API费用,完全免费
  • ⚡ 快速:本地FAISS矢量搜索
  • 🎯 每个项目:每个项目的独立索引
  • 🌐 多语言:Python、JS/TS、Go、Rust、Java、C/C++、C#、Markdown

🚀 快速开始

对于新项目

  1. 将此模板用于新项目

    git clone <your-template-url> your-project
    cd your-project
    
  2. 在全球范围内安装FarhanAliRaza (一次性,约1分钟)

    curl -fsSL https://raw.githubusercontent.com/FarhanAliRaza/claude-context-local/main/scripts/install.sh | bash
    
  3. 启用克劳德代码MCP批准 (关键-MCP加载所需)

    创建或编辑 .claude/settings.local.json 在您的项目中:

    mkdir -p .claude
    cat > .claude/settings.local.json << 'EOF'
    {
      "enableAllProjectMcpServers": true
    }
    EOF
    

    为什么需要:Claude Code需要明确批准才能从以下位置加载MCP服务器 .mcp.json。如果没有此设置,服务器即使在重新启动后也不会激活。

    替代 (选择性批准):

    {
      "enabledMcpjsonServers": ["code-search"]
    }
    
  4. 重新启动Claude代码

    完全关闭并重新打开Claude Code

  5. 验证MCP服务器是否已加载

    询问Claude:“列出可用的MCP工具”

    你应该看看 mcp__code-search__* 列出的工具

  6. 索引你的代码库

    在Claude Code中:“索引此代码库”

  7. 开始搜索!

    示例:

    • “查找身份验证码”
    • “显示API客户端类”

对于现有项目

  1. 复制配置文件 到您的项目根目录:

    cp .mcp.json your-project/
    cp .mcp-server-wrapper.sh your-project/
    # Merge with existing .gitignore if it exists
    cat .gitignore >> your-project/.gitignore
    
  2. 使包装器可执行

    chmod +x your-project/.mcp-server-wrapper.sh
    
  3. 启用克劳德代码MCP批准 (关键)

    创建或编辑 .claude/settings.local.json 在您的项目中:

    cd your-project
    mkdir -p .claude
    cat > .claude/settings.local.json << 'EOF'
    {
      "enableAllProjectMcpServers": true
    }
    EOF
    
  4. 在全球范围内安装FarhanAliRaza (如果尚未安装)

    curl -fsSL https://raw.githubusercontent.com/FarhanAliRaza/claude-context-local/main/scripts/install.sh | bash
    
  5. 重新启动Claude代码

    完全关闭并重新打开

  6. 验证和索引

    询问Claude:“列出可用的MCP工具”(应参见 mcp__code-search__*)

    然后:“索引此代码库”

📋 先决条件

只需要安装 每台机器一次 (不是每个项目):

# Check if already installed:
ls ~/.local/share/claude-context-local/

# If not found, install:
curl -fsSL https://raw.githubusercontent.com/FarhanAliRaza/claude-context-local/main/scripts/install.sh | bash

系统要求:

  • Python 3.12+
  • 1-2GB磁盘空间(适用于型号+索引)
  • 可选:GPU用于更快的索引(在CPU上工作)

📁 包含内容

.
├── .mcp.json                         ✅ Commit - MCP server config
├── .mcp-server-wrapper.sh            ✅ Commit - Server wrapper script
├── .gitignore                        ✅ Commit - Excludes index
├── .claude/
│   └── settings.local.json.template  ✅ Commit - Template for MCP approval
├── README.md                         ✅ Commit - This file
├── SETUP_CHECKLIST.md                ✅ Commit - Verification guide
└── .code-search-index/               ❌ DON'T commit - Local index (auto-generated)

🎮 使用示例

索引您的代码库

In Claude Code:
> Index this codebase

Expected output:
✅ Indexing /path/to/project...
✅ Found 121 files
✅ Created 1804 code chunks
✅ Indexed in 23 seconds

语义搜索

> Find all HTTP request handling code

Returns code chunks that handle HTTP requests,
even if they don't contain the exact words "HTTP request"!

类别/功能搜索

> Show me the DatabaseConnection class

Returns:
1. database/connection.py:15-45 (class DatabaseConnection) ⭐ EXACT
2. database/pool.py:20-40 (ConnectionPool)
3. database/manager.py:10-30 (DBConnectionManager)

特征搜索

> Find error handling patterns

Returns all error handling code across your codebase,
understanding the semantic meaning of "error handling"

检查状态

> What's the indexing status?

Returns:
{
  "total_chunks": 1804,
  "files_indexed": 121,
  "top_folders": { "src": 450, "tests": 610 }
}

🔧 运作原理

┌─────────────────────────────────────────────────┐
│ Your Project                                    │
│ ├── .mcp.json            ← Claude Code reads   │
│ ├── .mcp-server-wrapper.sh ← Launches server   │
│ ├── .claude/                                    │
│ │   └── settings.local.json ← Enables MCP      │
│ └── .code-search-index/  ← Local FAISS index   │
└─────────────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────────────┐
│ ~/.local/share/claude-context-local/            │
│ ├── mcp_server/          ← MCP server code      │
│ ├── embeddings/          ← EmbeddingGemma model │
│ └── chunking/            ← AST-based parser     │
└─────────────────────────────────────────────────┘

要点:

  • FarhanAliRaza在全球范围内安装过一次 ~/.local/share/
  • 每个项目都有自己的独立索引 .code-search-index/
  • Claude代码批准通过 .claude/settings.local.json 必需的
  • 项目之间没有冲突
  • 可以随时安全地删除和重新生成索引

🛠️ 故障排除

重新启动后MCP服务器未加载

症状:没有 mcp__code-search__* 重启后可用的工具

原因:缺少克劳德代码审批设置

修复:

  1. 创建或编辑 .claude/settings.local.json 在项目根目录中:

    mkdir -p .claude
    cat > .claude/settings.local.json << 'EOF'
    {
      "enableAllProjectMcpServers": true
    }
    EOF
    
  2. 重新启动Claude代码

验证修复:

# Check if approval setting exists
cat .claude/settings.local.json | grep enableAllProjectMcpServers
# Should output: "enableAllProjectMcpServers": true

附加检查表:

  1. ✅ 是 .mcp.json 在项目根中? ls .mcp.json
  2. ✅ 包装器是否可执行? ls -la .mcp-server-wrapper.sh (应显示 rwx)
  3. ✅ 是否存在全局安装? ls ~/.local/share/claude-context-local/
  4. ✅ 是否启用审批? grep enableAllProjectMcpServers .claude/settings.local.json

“未找到claude上下文本地”

修复:

curl -fsSL https://raw.githubusercontent.com/FarhanAliRaza/claude-context-local/main/scripts/install.sh | bash

“未找到uv命令”

修复:

curl -LsSf https://astral.sh/uv/install.sh | sh
# Restart terminal

包装脚本权限被拒绝

修复:

chmod +x .mcp-server-wrapper.sh

从头开始重新索引

# Delete index:
rm -rf .code-search-index/

# Re-index in Claude Code:
"Index this codebase"

索引未建立/搜索未返回任何结果

清单:

  1. ✅ 索引是否成功完成?
  2. ✅ 查看索引统计数据:“索引状态如何?”
  3. ✅ 验证中的文件 .code-search-index/projects/*/index/

修复:

# Re-index from scratch
rm -rf .code-search-index/
# Then in Claude Code: "Index this codebase"

📊 演出

项目大小 文件 索引时间 存储
小 <100 10-20秒 ~5 MB
中等 1000 1-2分钟 ~50 MB
大 10000 10-15分钟 ~500 MB

搜索速度: 每次查询50-200ms

🌟 为什么选择FarhanAliRaza?

我们进行了测试 3个本地代码搜索实现只有FarhanAliRaza工作可靠:

实施 状态 问题
丹尼尔/克劳德语境mcp ❌ 状态跟踪错误(GitHub#226)
MikeO AI/claude上下文本地mcp ❌ 破碎 100块硬限制
FarhanAliRaza/claude当地背景 ✅ 工作 未找到

测试方法: 121个文件,1804个块,生产就绪

📚 建筑

  • 嵌入: 谷歌嵌入Gemma(768维)
  • 矢量数据库: FAISS(IndexFlatIP)
  • Chunking: 基于AST(尊重代码结构)
  • 语言: Python、JS/TS、Go、Rust、Java、C/C++、C#、Markdown
  • 储存: 项目本地 .code-search-index/

🤝 贡献

发现此模板有用吗?考虑:

📄 许可证

此模板配置按原样提供。

FarhanAliRaza/claude context local有自己的许可证-请参阅他们的存储库。

🔗 资源


最后更新时间: 2025-11-27 状态: ✅ 生产准备就绪 测试时间: lmstudio桥接增强(1804块,121个文件)