代码智能搜索服务

一个为Python、SQL和Markdown代码库提供智能搜索和导航功能的模型上下文协议(MCP)服务器,旨在帮助LLM和AI助手高效理解和探索大型代码库。

  • by upandacross
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 upandacross/codebase-mcp-server

使用说明

代码库MCP服务器

一个模型上下文协议(MCP)服务器,为Python、SQL和Markdown代码库提供智能搜索和导航功能。旨在帮助LLM和AI助手高效地理解和探索大型代码库。

特性

  • 快速索引:基于AST的Python文件解析、SQL模式匹配和Markdown内容提取
  • 语义搜索:在代码库中查找函数、类、路由、模型、表和文档
  • Flask路线探索:查找web端点及其处理程序
  • ORM模型导航:了解数据库模型(SQLAlchemy、Flask SQLAlchemi)
  • SQL架构探索:查看表定义和迁移历史记录
  • 组件列表:获取路线、模型、表格和关键类的概述
  • 文件说明:文件用途和依赖关系的详细摘要

建筑

  • 索引器:预先构建代码库的JSON索引(3000多个组件约1秒)
  • 服务器:提供7个MCP工具用于代码探索
  • 存储:4-5MB磁盘占用空间,每个实例约64MB RAM
  • 演出:具有高效搜索评分的延迟加载索引

安装

先决条件

  • Python 3.11+
  • 紫外线 包管理器(或pip)

设置

# Clone the repository
git clone https://github.com/yourusername/codebase-mcp-server.git
cd codebase-mcp-server

# Install dependencies
uv pip install -e .
# Or with pip:
pip install -e .

配置

VS代码集成

创建或编辑 .vscode/mcp.json 在您的工作空间中:

{
  "mcpServers": {
    "codebase-search": {
      "command": "/absolute/path/to/your/project/.venv/bin/python",
      "args": [
        "/absolute/path/to/codebase-mcp-server/server.py",
        "--codebase",
        "${workspaceFolder}"
      ]
    }
  }
}

Claude桌面集成

添加到您的 claude_desktop_config.json:

{
  "mcpServers": {
    "codebase-search": {
      "command": "/absolute/path/to/your/project/.venv/bin/python",
      "args": [
        "/absolute/path/to/codebase-mcp-server/server.py",
        "--codebase",
        "/absolute/path/to/your/project"
      ]
    }
  }
}

可用工具

1. search_code

搜索函数、类、路线、模型、表格或文档

  • 参数: query (字符串), type (可选:函数、类、路由、模型、表、文件), limit (默认值:10)
  • 退货:按文件路径和位置对搜索结果进行排序

2. find_route

按路径或处理程序名称查找Flask路由

  • 参数: route (路径状 /users 或函数名称)
  • 退货:使用装饰器和处理程序代码定义路由

3. find_model

查找数据库模型类

  • 参数: model_name (例如。, User, Product)
  • 退货:具有字段和关系的模型类定义

4. find_table

查找数据库表并查看SQL定义

  • 参数: table_name (例如。, users, products)
  • 退货:SQL CREATE TABLE/VIEW语句

5. list_components

列出特定类型的所有组件

  • 参数: component_type (路线、型号、表格、类别)
  • 退货:所有匹配组件的概述

6. explain_file

获取特定文件的详细信息

  • 参数: filepath (相对路径)
  • 退货:目的、主要函数/类、依赖关系

7. rebuild_index

从头开始重建代码库索引

  • 参数:无
  • 退货:确认组件数量

使用示例

使用GitHub Copilot聊天的VS代码

重要:使用 @workspace 前缀用于激活MCP工具。没有它,Copilot就不会使用你的索引代码库。

@workspace find the user authentication route
@workspace list all database models
@workspace explain the database helper module
@workspace search for email validation functions

在克劳德桌面

Claude Desktop会自动使用可用的MCP工具,无需特殊前缀:

Find all functions related to email validation
Show me the User model definition
List all API routes in the codebase

索引详细信息

索引器处理:

  • Python文件:函数、类、装饰器、文档字符串
  • SQL文件:CREATE TABLE/VIEW语句、存储过程
  • Markdown文件:文档搜索的标题和内容

排除的目录(默认)

  • .venv, venv, .git, __pycache__
  • node_modules, .pytest_cache, .mypy_cache

定制

编辑 DEFAULT_EXCLUDE_PATTERNS 在 indexer.py 更改索引内容。

Git集成

子区存储库使用预提交钩子自动重建索引:

#!/bin/bash
# .git/hooks/pre-commit
if git diff --cached --name-only | grep -qE '\.(py|sql|md)$'; then
    python mcp_servers/precinct_codebase/indexer.py
    git add mcp_servers/precinct_codebase/codebase_index.json
fi

测试

pytest test_indexer.py -v

测试覆盖范围包括:

  • Python/SQL/Markdown索引
  • 带评分的搜索功能
  • 排除模式验证
  • 生产指标验证

演出

  • 索引:3000多个组件约1秒
  • 磁盘:4-5MB用于预建索引
  • 记忆:每个服务器实例约64MB RAM
  • 搜索:亚毫秒查询响应

许可证

MIT许可证-有关详细信息,请参阅许可证文件

贡献

欢迎投稿!请在GitHub上提交问题和拉取请求。

支持

对于问题、疑问或功能请求,请打开GitHub问题。