1337Xcode/Cortex

GitHub: 1337Xcode/Cortex

Cortex 将代码仓库索引为 SQLite 调用图并通过 MCP 协议暴露给 AI 编程助手,以极低 token 开销实现结构化代码查询与分析。

Stars: 3 | Forks: 1

# Cortex [![Release](https://img.shields.io/github/v/release/1337Xcode/cortex?style=flat-square&color=blue)](https://github.com/1337Xcode/cortex/releases) [![Build](https://img.shields.io/github/actions/workflow/status/1337Xcode/cortex/release.yml?style=flat-square&label=build)](https://github.com/1337Xcode/cortex/actions) [![License](https://img.shields.io/badge/license-MIT-green?style=flat-square)](LICENSE) [![Rust](https://img.shields.io/badge/rust-1.75%2B-orange?style=flat-square&logo=rust)](https://www.rust-lang.org/) [![MCP Tools](https://img.shields.io/badge/MCP%20tools-32-purple?style=flat-square)](docs/tools.md) [![Languages](https://img.shields.io/badge/languages-29-teal?style=flat-square)](docs/architecture.md) [![Platform](https://img.shields.io/badge/platform-linux%20%7C%20macos%20%7C%20windows-lightgrey?style=flat-square)]() [![npm](https://img.shields.io/npm/v/@1337xcode/cortex?style=flat-square&color=red&label=npm)](https://www.npmjs.com/package/@1337xcode/cortex) [![Docs](https://img.shields.io/badge/docs-1337xcode.github.io%2Fcortex-blue?style=flat-square)](https://1337xcode.github.io/cortex) 单一二进制文件。零依赖。支持 29 种语言。32 个 MCP 工具。为 AI 编码 agent 提供本地代码智能。 **[文档](https://1337xcode.github.io/cortex)** · **[npm](https://www.npmjs.com/package/@1337xcode/cortex)** · **[问题](https://github.com/1337Xcode/cortex/issues)** ## 兼容平台

Claude Code   Cursor   GitHub Copilot   Windsurf   VS Code   OpenAI Codex   Cline   Kiro   JetBrains   Neovim

任何兼容 MCP 的 AI agent、IDE 或 CLI 工具均可开箱即用。`cortex install` 会自动检测您的环境并写入配置。
全部 25 个支持的平台 | 平台 | 安装命令 | |---|---| | Claude Code (Linux/Mac) | `cortex install` | | Claude Code (Windows) | `cortex install --platform claude-code` | | Codex | `cortex install --platform codex` | | OpenCode | `cortex install --platform opencode` | | GitHub Copilot CLI | `cortex install --platform copilot` | | VS Code Copilot Chat | `cortex vscode install` | | Aider | `cortex install --platform aider` | | OpenClaw | `cortex install --platform openclaw` | | Factory Droid | `cortex install --platform droid` | | Trae | `cortex install --platform trae` | | Trae CN | `cortex install --platform trae-cn` | | Gemini CLI | `cortex install --platform gemini` | | Hermes | `cortex install --platform hermes` | | Kimi Code | `cortex install --platform kimi` | | Kiro IDE/CLI | `cortex kiro install` | | Pi coding agent | `cortex install --platform pi` | | Cursor | `cortex cursor install` | | Google Antigravity | `cortex antigravity install` | | Windsurf | `cortex install --platform windsurf` | | Zed | `cortex install --platform zed` | | JetBrains | `cortex install --platform jetbrains` | | Cline/Roo | `cortex install --platform cline` | | Continue.dev | `cortex install --platform continue` | | Supermaven | `cortex install --platform supermaven` | | Tabnine | `cortex install --platform tabnine` |
## 为什么选择 Cortex 一个 agent 询问“什么调用了 processOrder”时,会得到一个 200 token 的图结果,而不是耗费 20,000 token 去读取文件。在单个结构性问题上,这节省了 100 倍的 token。 Cortex 将您的仓库索引到 SQLite 调用图中,并通过模型上下文协议(MCP)将其暴露出来。在 535ms 内索引 127 个文件。增量重新索引仅需 13ms。能够处理 10 万到 100 万行以上的代码库。 **v1.1 版本新增了多源置信度感知智能。** Cortex 现在可以接入 SCIP 索引以实现精确的符号解析,运行特定框架的适配器(FastAPI、Express、NestJS、Spring、Django、React)来检测依赖注入和路由配置,为每条边标记置信度级别,并在无法给出确切答案时进行诚实的回退。`ask` 和 `get_task_context` 工具经过了彻底重构,引入了证据融合排序和结构化的回退建议。 ## 安装 ``` npx @1337xcode/cortex@latest install ```
其他方法 **Shell 脚本** ``` curl -fsSL https://raw.githubusercontent.com/1337Xcode/cortex/main/install.sh | sh ``` **从源码构建** ``` git clone https://github.com/1337Xcode/cortex.git && cd cortex cargo build --release cp target/release/cortex ~/.local/bin/ ```
## 快速开始 ``` cortex index # parse your repo, build the graph cortex install # detect Claude Code / Cursor, write MCP config cortex serve # start the MCP server ``` 您的 agent 将直接查询调用图,而不是读取原始文件。 ## 演示命令 ``` cortex impact UserService.getUser # blast radius: what breaks if I change this? cortex explain DatabasePool.acquire # offline function explanation, zero LLM calls cortex security report # taint flows, OWASP patterns, dependency vulns cortex diff main feature-branch # call graph diff between branches cortex viz --export graph.html # interactive 3D graph in a standalone HTML file cortex ci --fail-on-taint # CI quality gate with exit codes cortex hotspots --months 6 # high-churn risky code ranked by risk score cortex coverage --lcov coverage.lcov # populate coverage field, rank untested functions cortex modules # Leiden community detection module boundaries cortex federate add ../auth-service # query across repos with unified graph cortex benchmark # run correctness suite, report pass rate + token savings cortex status --savings # token savings dashboard with honest net-negative reporting cortex index --repair # rebuild index from scratch, preserve observations cortex semantic enable # build embedding index for semantic search ``` ## 功能特性 - **29 种语言**,通过 tree-sitter 解析 - **32 个 MCP 工具**,通过 stdio 暴露;或在智能模式下提供 5 个(`ask` 元工具进行内部路由) - **SCIP 索引入口**,用于精确的符号解析——当发生冲突时,优先采用 SCIP 边而不是 tree-sitter - **6 种框架适配器**(FastAPI、Express、NestJS、Spring、Django、React),可检测 tree-sitter 无法识别的 DI 配置、middleware 链和路由 - **置信度标记的边**——每条边都包含 `edge_source` 和 `confidence`(1.0 SCIP / 0.8 框架 / 0.5 AST / 0.3 名称匹配);查询默认筛选 confidence ≥ 0.7 的结果 - **证据融合的 `get_task_context`**——多信号排序(BM25 + SCIP 距离 + git 近期活跃度 + 边置信度 + 文件大小惩罚),提供逐文件的判断依据,并确保结果不为空 - **诚实回退**——当置信度低于 MEDIUM 时,`ask` 会返回结构化的 grep 建议;在索引状态不佳时,绝不会返回错误但语气肯定的数据 - **索引健康度门控**——当索引为空或损坏时,所有工具都会返回带有回退建议的结构化错误 - **Token 节省仪表盘**——`cortex status --savings` 显示累计净节省量、每次查询的平均节省量,以及净负增长的查询(如实报告) - **`get_repo_brief`**——无参数的冷启动摘要,控制在 400 token 以内:包括语言、框架、入口点、热点区域、安全模式和测试概况 - **工具集管理**——默认 10 个工具,7 个实验性工具(可选启用),5 个智能工具模式;`semantic_search` 仅在构建了 embedding 后才会列出 - **增量 embedding**——仅对内容哈希发生变化的函数重新生成 embedding;`cortex semantic enable` 用于构建初始索引 - **正确性基准测试**——`cortex benchmark` 根据真实标准验证工具准确性;如果通过率降至 70% 以下,CI 门控将使构建失败 - **亚秒级增量重新索引**,通过原生 OS 文件监视器实现(inotify、FSEvents、ReadDirectoryChangesW) - **可配置的模型定价**,通过 `~/.cortex/pricing.toml` 实现并支持最长前缀匹配 - **污点流分析、OWASP Top 10 检测、SBOM 生成**,全部在本地运行,无需云端 - **跨会话记忆**,当关联代码发生更改时,会将观察结果标记为过期 - **多仓库联合查询**,可通过统一的图跨仓库进行查询 - **CI 质量门控**,支持可配置的阈值和退出代码 - **3D 交互式图形可视化**,可导出为独立的 HTML - **可移植的 JSON 捆绑包** (cortex.json),方便通过 git 进行团队共享 - **支持离线工作。** 无需云平台、API 密钥、Docker 或语言运行时。
全部 29 种支持的语言 | 语言 | 语言 | 语言 | |----------|----------|----------| | Python | TypeScript | JavaScript | | Rust | Go | Java | | C | C++ | C# | | Ruby | PHP | Swift | | Kotlin | Scala | Lua | | Zig | Haskell | Elixir | | Dart | R | Julia | | OCaml | Bash | HCL/Terraform | | Perl | Objective-C | SQL | | YAML | TOML | |
## 架构 ``` graph TD A[Repository Files] --> B[File Watcher
notify crate] B --> C[Indexer Pipeline
Rayon + tree-sitter] C --> D[SQLite Graph Store
WAL + FTS5 + sqlite-vec] D --> E[MCP Server
Tokio + JSON-RPC 2.0] E --> F[AI Coding Agent] C --> G1[SCIP Ingester
Protobuf → HIGH edges] G1 --> D C --> G2[Framework Adapters
FastAPI · Express · NestJS
Spring · Django · React] G2 --> D C --> G3[Security Pass
Taint + OWASP + SBOM] G3 --> D D --> H[Bundle Exporter
cortex.json] H --> I[Git Repository
Team Sharing] D --> J[Memory Layer
Staleness-aware observations] D --> K[Evidence Fusion
BM25 + SCIP + git + embeddings] K --> E ``` ## 置信度级别 图中的每一条边都包含一个来源和置信度值。查询默认筛选置信度 ≥ 0.7 (MEDIUM) 的结果,过滤掉基于启发式名称匹配的边。 | 来源 | 置信度 | 生成方式 | |--------|-----------|--------------| | `scip` | 1.0 HIGH | SCIP 索引(精确的符号解析) | | `framework_adapter` | 0.8 MEDIUM | FastAPI/Express/NestJS/Spring/Django/React 模式匹配 | | `ast_direct` | 0.5 LOW | tree-sitter AST 提取 | | `name_match` | 0.3 VERY_LOW | 基于启发式的名称解析 | ## 基准测试结果 Cortex 内置了一个自引用的基准测试套件(`benchmark/cortex_self_benchmark.json`,24 个测试用例),涵盖了 `trace_callers`、`blast_radius`、`get_task_context` 和 `ask`。针对您的索引运行测试: ``` cortex benchmark ``` CI 流水线会在每次发布时运行此测试,如果通过率降至 70% 以下,构建将会失败。 ## 对比 | 功能 | Cortex | codebase-memory-mcp | Srclight | Serena | mcp-codebase-index | |---------|--------|---------------------|----------|--------|-------------------| | 架构 | Rust 二进制文件, SQLite | C 二进制文件, SQLite | Python, SQLite | Python, LSP | Python, SQLite | | 语言支持 | 29 (tree-sitter) | 66 (tree-sitter) | 10 (tree-sitter) | 40+ (LSP) | 4 | | MCP 工具 | 32 (或 5 个智能模式) | 14 | 42 | ~20 | 17 | | 智能工具路由 | 是 (ask 元工具) | 否 | 否 | 否 | 否 | | SCIP 精确解析 | 是 | 否 | 否 | 是 (LSP) | 否 | | 框架适配器 | 6 (DI, 路由, middleware) | 否 | 否 | 否 | 否 | | 置信度标记的边 | 是 (4 个级别) | 否 | 否 | 否 | 否 | | 诚实回退 | 是 (FallbackSuggestion) | 否 | 否 | 否 | 否 | | 索引健康度门控 | 是 | 否 | 否 | 否 | 否 | | Token 节省仪表盘 | 是 (计算透明) | 否 | 否 | 否 | 否 | | 冷启动仓库摘要 | 是 (get_repo_brief) | 否 | 否 | 否 | 否 | | 正确性基准测试 | 是 (CI 门控, 70% 阈值) | 否 | 否 | 否 | 否 | | 调用图 | 函数级别 | 完整链路 | 调用者/被调用者 | LSP 精确 | 跨文件 | | 混合搜索 | FTS5 + sqlite-vec | 仅图结构 | RRF 融合 | 关键词 | 结构化 | | Token 减少 | 结构化查询减少 100 倍 | 平均 121 倍 | 未测量 | 未测量 | 87% | | 增量更新 | 13ms (无更改时) | 毫秒级 | Git 钩子 | LSP 实时 | 基于哈希 | | 安全 (污点分析/OWASP/SBOM) | 是 | 否 | 否 | 否 | 否 | | 跨会话记忆 | 感知过期 | 否 | 否 | 否 | 否 | | 多仓库联合查询 | 是 | 否 | SQLite ATTACH | 部分 | 否 | | 社区发现 | Leiden 算法 | 否 | Leiden | 否 | 否 | | 覆盖率差距分析 | 是 (LCOV 交叉引用) | 否 | 否 | 否 | 否 | | 单一二进制,零依赖 | 是 | 是 | 否 (Python) | 否 (Python+LSP) | 否 (Python) | | 自动配置 IDE | 25 个 agent | 否 | 否 | 否 | 否 | | 兼容气隙隔离 | 是 | 是 | 是 | 是 | 是 | | 开源协议 | MIT | MIT | MIT | MIT | MIT | ## 文档 - [入门指南](docs/getting-started.md) - [IDE 配置](docs/ide-setup.md) - [CLI 参考](docs/cli-reference.md) - [MCP 工具](docs/tools.md) - [配置说明](docs/configuration.md) - [架构设计](docs/architecture.md) 完整文档站点:[1337xcode.github.io/cortex](https://1337xcode.github.io/cortex) ## 开源协议 [MIT](LICENSE) **[文档](https://1337xcode.github.io/cortex)** · **[npm](https://www.npmjs.com/package/@1337xcode/cortex)** · **[问题](https://github.com/1337Xcode/cortex/issues)**
标签:AI编程助手, LLM防护, MCP, Rust, SOC Prime, 人工智能, 代码分析, 凭证管理, 可视化界面, 开发工具, 暗色界面, 用户模式Hook绕过, 网络流量审计, 调用图, 通知系统