代码扫描工具

一个CLI工具和MCP服务器,用于扫描代码文件中的定义(类、函数等),支持.gitignore,提供行号,并输出LLM友好的格式(XML/Markdown)。

  • by Ixe1
  • JavaScript
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 Ixe1/code-scanner-server

使用说明

code-scanner-server

一个CLI工具和MCP服务器,用于扫描代码文件以查找定义(类、函数等)、尊重.gitignore、提供行号并输出LLM友好格式(XML/Dmarkdown)。

这个项目提供了一个用TypeScript和Node.js构建的多功能代码扫描工具。它利用Tree sitter解析库来分析源代码并提取结构信息。它既可以作为命令行界面(CLI)工具运行,也可以作为MCP(模型上下文协议)服务器运行。

注: 该工具正在积极开发中。虽然核心功能是可操作的,但某些功能或特定语言解析器可能没有经过充分测试,可能包含错误或限制。

特性

  • 代码定义提取: 标识函数、类、变量、接口、方法等。
  • 多语言支持: 解析JavaScript(.js, .jsx),TypeScript(.ts, .tsxC.cs),PHP(.php),CSS(.css)和Python(.py)通过树保姆。
  • .gitignore意识到: 自动遵守中定义的规则 .gitignore 文件夹。
  • 灵活过滤: 按定义类型、修饰符筛选结果(public, private)、名称模式(正则表达式)和文件路径模式。
  • 多种输出格式: 以Markdown(默认)、XML或JSON格式生成结果。
  • 可配置的细节级别: 输出冗长: minimal, standard (默认), detailed.
  • 双模式操作: 作为独立的CLI工具或集成的MCP服务器运行。

使用模式

1.命令行界面(CLI)

直接从终端运行扫描仪。此模式需要 --directory 指定目标代码库的参数。

基本用法:

node build/index.js --directory /path/to/your/codebase

常见选项:

  • -d, --directory <path>:(必需)要扫描的目录的绝对或相对路径。
  • -p, --patterns <patterns...>:文件扩展名的Glob模式(例如。, "**/*.ts" "**/*.js").默认为JS、TSX、CS、PHP、CSS、PY文件。
  • -f, --format <format>:输出格式(xml, markdown, json).违约: markdown.
  • -l, --detail <level>:详细程度(minimal, standard, detailed).违约: standard.
  • --include-types <types...>:仅包括特定的定义类型(例如。, class, method).
  • --exclude-types <types...>:排除特定定义类型。
  • --include-modifiers <modifiers...>:仅包括具有特定修饰符的定义(例如。, public).
  • --exclude-modifiers <modifiers...>:排除具有特定修饰符的定义。
  • --name-pattern <regex>:包括与JavaScript正则表达式模式匹配的定义。
  • --exclude-name-pattern <regex>:排除与JavaScript正则表达式模式匹配的定义。
  • --include-paths <paths...>:要包含的其他文件路径模式(glob)。
  • --exclude-paths <paths...>:要排除的文件路径模式(glob)。
  • -h, --help:显示所有选项的详细帮助信息。

示例(扫描TypeScript文件 src,输出详细的JSON):

node build/index.js -d ./src -p "**/*.ts" -f json -l detailed

2.MCP服务器模式(scan_code 工具)

如果没有运行 --directory 参数,该工具作为MCP服务器启动,通过标准输入/输出监听请求。这允许与人工智能助手等MCP客户端集成。

  • 工具名称: scan_code
  • 说明: 扫描指定目录中的代码文件,并根据提供的筛选器返回定义列表。
  • 输入架构: 接受与CLI选项对应的参数。这 directory 财产是必需的。
    {
      "type": "object",
      "properties": {
        "directory": { "type": "string", "description": "Absolute path to the directory to scan." },
        "filePatterns": { "type": "array", "items": { "type": "string" }, "description": "Glob patterns for files.", "default": ["**/*.js", ..., "**/*.py"] },
        "outputFormat": { "type": "string", "enum": ["xml", "markdown", "json"], "default": "markdown" },
        "detailLevel": { "type": "string", "enum": ["minimal", "standard", "detailed"], "default": "standard" },
        "includeTypes": { "type": "array", "items": { "type": "string" } },
        "excludeTypes": { "type": "array", "items": { "type": "string" } },
        "includeModifiers": { "type": "array", "items": { "type": "string" } },
        "excludeModifiers": { "type": "array", "items": { "type": "string" } },
        "namePattern": { "type": "string", "description": "Regex pattern for names." },
        "excludeNamePattern": { "type": "string", "description": "Regex pattern to exclude names." },
        "includePaths": { "type": "array", "items": { "type": "string" } },
        "excludePaths": { "type": "array", "items": { "type": "string" } }
      },
      "required": ["directory"]
    }
    
  • AI助手使用示例: “在输出xml格式的目录/path/to/project上使用代码扫描服务器scan_code。”

安装

  1. 先决条件: 确保你已经安装了Node.js和npm。
  2. 克隆(可选): 如果您没有代码,请克隆存储库。
    # git clone <repository_url>
    # cd code-scanner-server
    
  3. 安装依赖关系:
    npm install
    
  4. 构建: 编译TypeScript代码。
    npm run build
    
    这将在以下位置创建可执行JavaScript文件 build/index.js.

配置(MCP服务器)

要使用MCP服务器模式,请将其添加到MCP客户端的配置文件中(例如。, claude_desktop_config.json 对于桌面应用程序或 cline_mcp_settings.json 用于VS代码扩展)。

重要提示: 替换 /path/to/code-scanner-server 在下面的例子中 绝对路径 转到系统上此项目的目录。

示例(claude_desktop_config.json / cline_mcp_settings.json):

{
  "mcpServers": {
    "code-scanner-server": {
      "command": "node",
      "args": [
        "/absolute/path/to/your/code-scanner-server/build/index.js" // <-- Replace this path! (e.g., "C:\\Users\\YourUser\\Projects\\code-scanner-server\\build\\index.js" on Windows)
      ],
      "env": {},
      "disabled": false,
      "autoApprove": [] // Add tool names here for auto-approval if desired
    }
  }
}

修改配置后,请记住重新启动MCP客户端应用程序(IDE、桌面应用程序),以使更改生效。

发展

  • 观看模式: 源文件更改时自动重建项目:
    npm run watch
    
  • 调试(MCP模式): 通过stdio调试MCP服务器可能很复杂。使用MCP检查器工具更容易调试:
    npm run inspector
    
    这将启动附加了Node.js检查器的服务器,并提供一个URL来连接调试工具(如Chrome DevTools)。

致谢

该项目在人工智能的帮助下得到了显著发展,主要使用谷歌的Gemini 2.5 Pro模型,该模型通过Visual Studio Code的Roo Code扩展进行访问。

许可证

此项目根据GNU通用公共许可证v3.0获得许可-请参阅 许可证 文件以获取详细信息。