code-scanner-server一个命令行工具和 MCP 服务器,用于扫描代码文件中的定义(类、函数等),尊重
.gitignore文件,提供行号,并以适合 LLM 的格式输出(XML/Markdown)。
该项目提供了一个使用 TypeScript 和 Node.js 构建的多功能代码扫描工具。它利用 Tree-sitter 解析库来分析源代码并提取结构信息。它可以作为命令行界面(CLI)工具运行,也可以作为 MCP(模型上下文协议)服务器集成。
注意: 此工具正在积极开发中。虽然核心功能已经可用,但某些功能或特定语言解析器可能尚未完全测试,可能存在错误或限制。
.js、.jsx)、TypeScript(.ts、.tsx)、C#(.cs)、PHP(.php)、CSS(.css)和 Python(.py)。.gitignore: 自动遵循 .gitignore 文件中的规则。public、private)、名称模式(正则表达式)和文件路径模式过滤结果。minimal、standard(默认)、detailed。直接从终端运行扫描器。此模式需要指定目标代码库的 --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: 显示所有选项的详细帮助信息。示例(扫描 src 中的 TypeScript 文件,输出详细的 JSON):
node build/index.js -d ./src -p "**/*.ts" -f json -l detailed
scan_code 工具)如果没有 --directory 参数,工具将以 MCP 服务器模式启动,通过标准输入/输出监听请求。这允许与 MCP 客户端(如 AI 助手)集成。
scan_codedirectory 属性是必需的。
{
"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"]
}
# git clone <repository_url>
# cd code-scanner-server
npm install
npm run build
这将在 build/index.js 创建可执行的 JavaScript 文件。要使用 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
npm run inspector
这会启动带有 Node.js 检查器的服务器,并提供一个连接调试工具(如 Chrome DevTools)的 URL。本项目在很大程度上得到了 AI 的帮助,主要使用了通过 Visual Studio Code 的 Roo Code 扩展访问的 Google 的 Gemini 2.5 Pro 模型。
本项目根据 GNU 通用公共许可证 v3.0 授权 - 详情见 LICENSE 文件。