代码审计:基于人工智能的多语言代码智能
你的AI能够跨语言理解你的代码。 Code Auditor为你的整个代码库(TypeScript、JavaScript和Go)建立索引,并提供实时分析,像Claude这样的人工智能助手实际上可以用来帮助你编写更好的代码。
它解决的问题
AI编码助手功能强大,但它们是盲目的。他们无法搜索你的代码库,不知道你的模式,也无法错过关键的上下文。Code Auditor通过为代码中的每个函数、组件和模式创建可搜索的索引来改变这一点。
运作原理
- 索引 -自动编目函数、React组件、Go结构和依赖项
- 分析 -检测跨多种语言的SOLID违规、代码重复和安全问题
- 连接 -AI助手通过MCP(模型上下文协议)访问您的代码索引
- 迭代 -根据您的实际代码库模式获取智能建议
快速入门(2分钟)
# Add to your project with Claude Code CLI
claude mcp add code-auditor -- npx code-auditor-mcp
# That's it! Now ask Claude:
# "What authentication functions exist in my codebase?"
# "Find all API endpoints and check for rate limiting"
# "Show me Go structs that handle user data"
# "Compare TypeScript and Go implementations of the same feature"重要的核心功能
🔍 多语言代码搜索
"Find all functions that validate user input"
"Show me where we're calling the payment API"
"What Go structs implement the User interface?"
"Compare error handling patterns between TypeScript and Go"🎯 智能代码分析
- 坚实的原则 -在架构问题传播之前抓住它们
- DRY违规 -查找应重构的重复代码
- 安全模式 -验证身份验证、速率限制、SQL注入保护
- 死代码 -识别未使用的导入和功能
🤖 AI工具集成
自动生成以下配置:
- 克劳德(通过MCP)
- 光标
- 继续
- GitHub Copilot
- 10+其他AI助手
⚙️ 持久配置
设置一次分析器首选项:
You: "Set SOLID analyzer to allow 3 responsibilities for components"
Claude: Configuration saved! All future audits will use this setting.📋 项目任务队列(MCP)
使用 project_tasks 工具,用于在本地数据库中保存每个项目的任务列表(标题、状态、优先级、截止日期、阻止程序、相关文件/符号等)。 任务存活 sync_index 重置:清除分析索引会删除索引函数、缓存审计、代码映射和模式覆盖,这样就不会保留对已删除代码的“幽灵”引用——确实如此 不 删除任务列表或分析器配置。
真实案例
示例1:查找身份验证模式
You: "Show me all authentication-related functions"
Claude: Found 23 functions across 8 files:
- `validateToken()` in auth/tokens.ts:45
- `requireAuth()` in middleware/auth.ts:12
- `checkPermissions()` in auth/permissions.ts:78
...示例2:分析代码质量
You: "Audit the user service for issues"
Claude: Found 3 critical issues:
- Single Responsibility violation: UserService handles both auth and profile updates
- SQL injection risk: Raw query in getUserByEmail() at line 234
- Missing rate limiting on password reset endpoint示例3:发现模式
You: "Find React components similar to DataTable"
Claude: Found 4 similar components:
- `UserTable` - extends DataTable with user-specific columns
- `OrderGrid` - implements similar pagination pattern
- `ProductList` - uses same filtering approach安装选项
已发布的包(npm/pnpm/yarn)
npm install -g code-auditor-mcp
# or: pnpm add -g code-auditor-mcp
code-audit # Run analysis项目安装
npm install --save-dev code-auditor-mcp
npx code-audit从该存储库开发
需要 Node.js 18+.从 app/ 目录:
pnpm install
pnpm run build
pnpm test正在使用的包管理器是 pnpm (参见 packageManager 在 package.json).使用 pnpm run test:parity 仅当您需要传统与通用分析器奇偶校验套件时。
CI/CD集成
# GitHub Actions
- name: Code Audit
run: npx code-audit --fail-on-criticalMCP服务器:存储索引的位置
Loki索引文件默认为 /.code-index/index.db。如果您的MCP主机在意外或共享的情况下运行 cwd,指向专用文件夹:
- 环境变量
CODE_AUDITOR_DATA_DIR--用作存储根的目录(服务器启动时相对于进程cwd的绝对或相对目录)。数据库文件是/index.db(无额外费用.code-index分段)。 - 命令行界面 —
node dist/mcp-index.js --data-dir /path/to/data(相当于设置env变量)。已发布的二进制文件code-auditor-mcp也接受--data-dir因为它加载了相同的引导程序。
示例 光标 .cursor/mcp.json 片段:
{
"mcpServers": {
"code-auditor": {
"command": "node",
"args": ["/absolute/path/to/code-auditor/app/dist/mcp-index.js", "--stdio"],
"env": {
"CODE_AUDITOR_DATA_DIR": "/Users/you/Library/Application Support/code-auditor"
}
}
}
}关键命令
code-audit # Full analysis with HTML report
code-audit -f json # JSON output for CI/CD
code-audit -a solid,dry # Run specific analyzers
code-audit --health # Quick health score (0-100)反馈循环
- 写入代码 → 代码审计员会自动为其建立索引
- 问AI → “是否有验证电子邮件的功能?”
- 获取上下文 → AI发现
validateEmail()以及类似的模式 - 改进 → 人工智能建议使用现有的验证,而不是复制
- 重复 → 你的AI对你的代码库越来越聪明
配置
为您的架构设置自定义阈值:
// Via MCP
mcp.set_analyzer_config({
analyzerName: "solid",
config: {
maxUnrelatedResponsibilities: 4,
patternThresholds: {
"Dashboard": { maxResponsibilities: 6 }
}
}
});高级搜索运算符
| 您想要什么 | 搜索查询 |
|---|---|
| 功能复杂 | complexity:>10 |
| 仅Go功能 | lang:go |
| TypeScript组件 | lang:typescript component:functional |
| 无证出口 | exported:true jsdoc:false |
| React钩子的使用 | component:functional hook:useState |
| Go结构体方法 | lang:go entity:struct |
| 查找依赖关系 | calls:validateUser |
| 跨语言模式 | name:validateEmail |
| 未使用的进口 | unused-imports file:src |
演出
- 秒内索引10000+个函数
- 文件更改的增量更新
- LokiJS用于快速内存搜索
- FlexSearch用于智能查询
贡献
看 贡献.md 作为指导方针。
许可证
麻省理工学院-随时随地使用它。
______________________________________________________________________
准备好将你的AI x射线视觉输入到你的代码中了吗?
claude mcp add code-auditor -- npx code-auditor-mcp