内容
[](https://mseep.ai/app/ixe1-code-scanner-server)
<a href="https://glama.ai/mcp/servers/@Ixe1/code-scanner-server">
<img width="380" height="200" src="https://glama.ai/mcp/servers/@Ixe1/code-scanner-server/badge" 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`文件中定义的规则。
* **灵活的过滤:** 按定义类型、修饰符(`public`,`private`)、名称模式(正则表达式)和文件路径模式过滤结果。
* **多种输出格式:** 以Markdown(默认)、XML或JSON格式生成结果。
* **可配置的详细程度:** 输出详细程度:`minimal`,`standard`(默认),`detailed`。
* **双模式操作:** 作为独立的CLI工具或集成的MCP服务器运行。
## 使用模式
### 1. 命令行界面(CLI)
直接从终端运行扫描器。此模式需要`--directory`参数指定目标代码库。
**基本用法:**
```bash
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`:显示所有选项的详细帮助信息。
**示例(扫描`src`中的TypeScript文件,输出详细的JSON):**
```bash
node build/index.js -d ./src -p "**/*.ts" -f json -l detailed
```
### 2. MCP服务器模式(`scan_code`工具)
如果在没有`--directory`参数的情况下运行,该工具将作为MCP服务器启动,通过标准输入/输出监听请求。这允许与MCP客户端(如AI助手)集成。
* **工具名称:** `scan_code`
* **描述:** 扫描指定目录中的代码文件,并根据提供的过滤器返回定义列表。
* **输入模式:** 接受与CLI选项相对应的参数。`directory`属性是必需的。
```json
{
"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. **克隆(可选):** 如果您没有代码,请克隆存储库。
```bash
# git clone <repository_url>
# cd code-scanner-server
```
3. **安装依赖项:**
```bash
npm install
```
4. **构建:** 编译TypeScript代码。
```bash
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`):**
```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、桌面应用程序),以使更改生效。*
## 开发
* **监视模式:** 在源文件更改时自动重建项目:
```bash
npm run watch
```
* **调试(MCP模式):** 通过stdio调试MCP服务器可能很复杂。使用MCP Inspector工具可以更轻松地进行调试:
```bash
npm run inspector
```
这将启动带有Node.js检查器的服务器,并提供一个URL以连接调试工具(如Chrome DevTools)。
## 致谢
本项目的开发得到了AI的重大帮助,主要使用通过Visual Studio Code的Roo Code扩展访问的Google的Gemini 2.5 Pro模型。
## 许可证
本项目根据GNU通用公共许可证v3.0进行许可 - 有关详细信息,请参阅[LICENSE](LICENSE)文件。