返回市场
代码扫描服务器

代码扫描服务器

作者:Ixe12 星标更新:2025-06-23

项目介绍

MseeP.ai 安全评估徽章

<a href="https://glama.ai/mcp/servers/@Ixe1/code-scanner-server"> <img width="380" height="200" src="https://gips1.baidu.com/it/u=3338298299,1021500643&fm=3081&app=3081&f=PNG?w=760&h=400" alt="代码扫描服务器 MCP 服务器" /> </a>

code-scanner-server

一个命令行工具和 MCP 服务器,用于扫描代码文件中的定义(类、函数等),尊重 .gitignore 文件,提供行号,并以适合 LLM 的格式输出(XML/Markdown)。

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

注意: 此工具正在积极开发中。虽然核心功能已经可用,但某些功能或特定语言解析器可能尚未完全测试,可能存在错误或限制。

功能

  • 代码定义提取: 识别函数、类、变量、接口、方法等。
  • 多语言支持: 使用 Tree-sitter 解析 JavaScript(.js.jsx)、TypeScript(.ts.tsx)、C#(.cs)、PHP(.php)、CSS(.css)和 Python(.py)。
  • 尊重 .gitignore 自动遵循 .gitignore 文件中的规则。
  • 灵活过滤: 根据定义类型、修饰符(如 publicprivate)、名称模式(正则表达式)和文件路径模式过滤结果。
  • 多种输出格式: 生成结果为 Markdown(默认)、XML 或 JSON。
  • 可配置详细程度: 输出详细程度:minimalstandard(默认)、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>: 输出格式(xmlmarkdownjson)。默认:markdown
  • -l, --detail <level>: 详细程度(minimalstandarddetailed)。默认:standard
  • --include-types <types...>: 只包含特定的定义类型(例如,classmethod)。
  • --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: 显示所有选项的详细帮助信息。

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

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

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

如果没有 --directory 参数,工具将以 MCP 服务器模式启动,通过标准输入/输出监听请求。这允许与 MCP 客户端(如 AI 助手)集成。

  • 工具名称: scan_code
  • 描述: 扫描指定目录中的代码文件,并根据提供的过滤器返回定义列表。
  • 输入模式: 接受对应于 CLI 选项的参数。directory 属性是必需的。
    {
      "type": "object",
      "properties": {
        "directory": { "type": "string", "description": "要扫描的目录的绝对路径。" },
        "filePatterns": { "type": "array", "items": { "type": "string" }, "description": "文件的 glob 模式。", "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": "名称的正则表达式模式。" },
        "excludeNamePattern": { "type": "string", "description": "排除名称的正则表达式模式。" },
        "includePaths": { "type": "array", "items": { "type": "string" } },
        "excludePaths": { "type": "array", "items": { "type": "string" } }
      },
      "required": ["directory"]
    }
    
  • 示例用法与 AI 助手: "使用 code-scanner-server 的 scan_code 在目录 /path/to/project 上进行扫描,输出 xml 格式。"

安装

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

配置(MCP 服务器)

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

重要: 将下面示例中的 /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" // <-- 替换此路径!(例如,在 Windows 上为 "C:\\Users\\YourUser\\Projects\\code-scanner-server\\build\\index.js")
      ],
      "env": {},
      "disabled": false,
      "autoApprove": [] // 如需自动批准,请在此处添加工具名称
    }
  }
}

修改配置后,请记得重启您的 MCP 客户端应用程序(IDE、桌面应用),以便更改生效。

开发

  • 监视模式: 当源文件发生变化时,自动重建项目:
    npm run watch
    
  • 调试(MCP 模式): 通过 stdio 调试 MCP 服务器可能很复杂。使用 MCP Inspector 工具进行更简单的调试:
    npm run inspector
    
    这会启动带有 Node.js 检查器的服务器,并提供一个连接调试工具(如 Chrome DevTools)的 URL。

致谢

本项目在很大程度上得到了 AI 的帮助,主要使用了通过 Visual Studio Code 的 Roo Code 扩展访问的 Google 的 Gemini 2.5 Pro 模型。

许可证

本项目根据 GNU 通用公共许可证 v3.0 授权 - 详情见 LICENSE 文件。