关于 Octocode
Octocode 是面向代码库 AI Agent 的 MCP 服务和代码索引器,结合语义搜索与基于 tree-sitter 的符号及依赖图,并可选提供索引增强和 LSP 工具。
主要功能
- 01语义与 BM25 代码搜索
- 02基于 AST 的文件和符号图
- 03查看签名与代码结构
- 04AST 模式搜索
- 05可选 LSP 导航工具
- 06可选索引式 GraphRAG 增强
适用场景
- 01按概念或行为查找代码
- 02追踪导入、调用和依赖
- 03无需通读文件即可查看结构
- 04在工作区定位代码模式
MCP 相关
Octocode 通过 MCP 工具提供代码搜索、签名查看、图导航和结构搜索。可选 LSP 工具支持定义跳转、引用查找、悬停信息和符号操作。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
面向 AI Agent 的结构化代码智能 — MCP 服务 + 知识图谱 + 语义搜索
为你的代码库赋予 AI 助手一个大脑。 Octocode 会将你的项目转换为可导航的知识图谱,让 Claude、Cursor 和其他 AI Agent 能够搜索、理解并浏览其中的内容。
🚀 快速开始 • 🤖 MCP 集成 • 📖 文档 • 🌐 网站
🤖 为 AI Agent 打造
问题: AI 助手无法洞察你的代码库。它们不能搜索文件、理解依赖关系,也无法在不同会话间记住上下文。
解决方案: Octocode 的 MCP 服务为 AI Agent 提供:
- 🔍 语义搜索 — 按含义而非关键词查找代码
- 🕸️ 知识图谱 — 浏览导入、调用和依赖关系
- 📝 代码签名 — 无需读取完整文件即可查看其结构
- 🧭 LSP 精确定位 — 通过语言服务实现转到定义、查找引用和悬停文档
适用于: Claude Desktop • Cursor • Windsurf • 任何兼容 MCP 的 AI
现在,你的 AI 助手可以:
You: "Where is authentication handled?"
AI: *searches your codebase* "Authentication is in src/middleware/auth.rs,
which imports jwt.rs for token validation and calls user_store.rs for lookup."
You: "What files depend on the payment module?"
AI: *queries knowledge graph* "src/api/handlers/payment.rs imports payment/mod.rs,
which is also used by src/workers/refund.rs and src/cron/billing.rs"
You: "Find every call site of this function"
AI: *uses LSP find-references* "process_payment() is called from 4 places:
checkout.rs:87, refund.rs:134, billing.rs:56, and tests/payment_test.rs:23"
🤔 为什么选择 Octocode?
标准 RAG 会将代码视为扁平的文本片段。 它能找到相似的片段,却不知道 auth_middleware.rs 导入了 jwt.rs、调用了 user_store.rs,并接入了 router.rs。Octocode 理解结构。
# Semantic search finds the right code
octocode search "authentication middleware"
→ src/middleware/auth.rs — Similarity: 0.9234
# The GraphRAG CLI queries the optional persisted graph
octocode config --graphrag-enabled true
octocode index
octocode graphrag get-relationships --node-id src/middleware/auth.rs
Outgoing:
imports → jwt (src/auth/jwt.rs): token validation logic
calls → user_store (src/db/user_store.rs): user lookup by token
Incoming:
imports ← router (src/router.rs): wires auth into the request pipeline
Octocode 使用 tree-sitter AST 解析,构建包含文件、符号、导入、调用、继承和实现关系的实时图谱。MCP graphrag 工具无需索引、嵌入或大模型,即可根据当前源代码树按需构建此图谱。可选的索引式 GraphRAG 还能提供语义文件发现、描述和更广泛的架构关系。
🔬 工作原理
Current Source → Tree-sitter AST → Live Symbol Graph ──────────────→ MCP `graphrag`
↑ ↑
Indexed Code → Embeddings + Optional LLM → Persisted File Enrichment ─────┘
- 实时 AST 图谱 — tree-sitter 直接从当前源代码中提取文件和符号节点,以及确定性的
contains、imports、calls、extends和implements关系 - 始终可用的图谱导航 — 即使设置
[graphrag].enabled = false,也可以通过 MCP 查询图谱、遍历关系、查找路径和查看概览 - 可选增强 — 启用索引式 GraphRAG 后,可叠加语义文件匹配、大模型生成的描述,以及更广泛的文件级架构关系;符号不会被嵌入,也不会由大模型生成
- 混合搜索 — 语义相似度 + BM25 全文搜索 + 重排序,分别处理基于含义的代码检索
- MCP 服务 — 向任何兼容 MCP 的客户端提供
semantic_search、view_signatures、graphrag和structural_search
✨ 与众不同之处
| 标准 RAG | 文档查找工具 | Octocode | |
|---|---|---|---|
| 索引内容 | 文本块 | 外部库文档 | 你的代码库结构(AST) |
| 理解内容 | 相似文本 | API 规范和用法 | 函数、导入、依赖关系 |
| 跨文件 | 否 | 否 | 是 — 沿依赖关系图导航 |
| 关系 | 否 | 否 | imports、calls、implements、extends…… |
| AI 集成 | 因情况而异 | MCP | 原生 MCP 服务 + LSP |
文档工具为 AI 提供你所用库的使用手册。Octocode 则为 AI 提供你如何组合这些库的蓝图。
使用 Rust 构建,性能出色。本地优先,保护隐私。开源(Apache 2.0),透明可信。
📊 检索质量
Octocode 提供了一套可复现的检索基准测试(benchmark/):包含 127 条精心整理的代码搜索查询及其行范围标准答案,并针对 octocode 自身源码运行(固定在 b1771ba,确保标注不会偏移)。下方数据使用完全本地、无需 API 密钥的技术栈 — 通过 fastembed 使用 jina-embeddings-v2-base-code,不使用重排器 — 因此这些数据代表基线,而非上限:
| 配置 | Hit@5 | Hit@10 | MRR | NDCG@10 | Recall@10 |
|---|---|---|---|---|---|
| 仅密集向量 | 0.598 | 0.717 | 0.485 | 0.528 | 0.671 |
| 混合检索,默认 RRF 权重(0.7/0.3) | 0.598 | 0.717 | 0.485 | 0.528 | 0.671 |
| 混合检索,针对关键词调优(0.3/0.7) | 0.732 | 0.835 | 0.572 | 0.620 | 0.807 |
提高 RRF 融合中 BM25/关键词信号的权重 — 代码中的精确标识符对此尤为重要 — 可在零额外成本下将 Hit@5 提升 +22%,并将 Recall@10 提升 +20%。
基准测试还指出了哪些方法在这里_不起作用_(benchmark/RESULTS.md 中提供完整的 6 种变体对照):通用的本地交叉编码器重排器(bge-reranker-base)实际上会导致结果变差(Hit@5 从 0.732 降至 0.598)——代码检索需要_了解代码的_重排器(例如 voyage:rerank-2.5),而非现成的通用重排器。
git clone https://github.com/Muvon/octocode && cd octocode
git worktree add /tmp/corpus b1771ba # pin the corpus to the ground-truth commit
CORPUS=/tmp/corpus python3 benchmark/run_matrix.py # set OCTO_BIN to use a custom binary
有关方法和指标定义,请参阅 benchmark/README.md。
🚀 快速开始
1. 安装
# Universal installer (Linux, macOS, Windows)
curl -fsSL https://raw.githubusercontent.com/Muvon/octocode/master/install.sh | sh
# macOS with Homebrew
brew install muvon/tap/octocode
其他安装方式
# From crates.io
cargo install octocode
# Or from source (latest)
cargo install --git https://github.com/Muvon/octocode
# Download binary from releases
# https://github.com/Muvon/octocode/releases
平台特定的说明请参阅安装指南。
2. 设置 API 密钥
# Optional: embedding provider (defaults to local FastEmbed — no key needed)
export VOYAGE_API_KEY="your-voyage-api-key"
# Optional: LLM for commit messages, code review
export OPENROUTER_API_KEY="your-openrouter-api-key"
获取你的 Voyage API 密钥: voyageai.com(提供免费套餐)
其他嵌入提供方
Octocode 支持多种嵌入提供方:
# OpenAI
export OPENAI_API_KEY="your-key"
octocode config --code-embedding-model "openai:text-embedding-3-small"
# Jina AI
export JINA_API_KEY="your-key"
octocode config --code-embedding-model "jina:jina-embeddings-v3"
# Google
export GOOGLE_API_KEY="your-key"
octocode config --code-embedding-model "google:text-embedding-005"
所有受支持提供方请参阅 API Keys 指南。
3. 为代码库建立索引
cd /your/project
octocode index
# → ✓ Indexing complete! 342 of 342 files processed (342 new, 0 unchanged)
4. 搜索代码
# Natural language search
octocode search "authentication middleware"
# Multi-query for broader results
octocode search "auth" "middleware" "session"
# Filter by language
octocode search "database connection pool" --language rust
# Search commit history
octocode search "authentication refactor" --mode commits
5. 连接 AI 助手
将以下内容添加到 MCP 客户端配置中(Claude Desktop、Cursor、Windsurf):
{
"mcpServers": {
"octocode": {
"command": "octocode",
"args": ["mcp", "--path", "/your/project"]
}
}
}
完成!现在,你的 AI 助手已能理解代码库结构。
🔌 MCP 服务集成
Octocode 内置了一个 MCP 服务,可将你的代码库作为工具提供给 AI 助手。这是使用 Octocode 的主要方式 — 让 AI 助手直接访问代码搜索和导航功能。
可用工具
| 工具 | 功能说明 |
|---|---|
semantic_search | 按含义查找代码——“身份验证流程”“错误处理”“数据库查询” |
view_signatures | 查看文件结构——函数签名、类定义、导入 |
graphrag | 常驻文件/符号图——搜索节点、检查关系,并在不建立索引的情况下查找路径 |
structural_search | AST 模式匹配——查找 .unwrap() 调用、new 实例化及特定模式 |
lsp_goto_definition | 跳转到符号定义(需要 --with-lsp) |
lsp_find_references | 查找工作区中某个符号的所有用法(需要 --with-lsp) |
lsp_hover | 查看符号的类型信息和文档(需要 --with-lsp) |
lsp_document_symbols / lsp_workspace_symbols / lsp_completion | 文件符号、全工作区符号搜索、代码补全(需要 --with-lsp) |
使用你的语言服务器启动服务,即可启用 LSP 工具:
octocode mcp --path /your/project --with-lsp="rust-analyzer"
对话式 AI 示例
连接后,你的 AI 助手可以回答有关代码库的问题:
You: "Where is user authentication implemented?"
AI: *uses semantic_search* "Found in src/auth/login.rs. The authenticate() function
validates credentials against the database, generates a JWT token, and stores
the session in Redis."
You: "What files depend on the payment module?"
AI: *uses graphrag* "src/api/handlers/payment.rs imports payment/mod.rs, which is also
used by src/workers/refund.rs and src/cron/billing.rs. The payment module exports
process_payment() and validate_transaction() functions."
You: "Show me all error handling in the API layer"
AI: *uses structural_search* "Found 23 error handling patterns in src/api/:
- 15 use Result<T, ApiError> with explicit error types
- 8 use .unwrap() (potential panics in handlers/user.rs:42, handlers/auth.rs:87)
- 3 use .expect() with custom messages"
快速设置
Octomind(推荐)——零配置,Octocode 已预先配置:
curl -fsSL https://raw.githubusercontent.com/muvon/octomind/master/install.sh | bash
octomind run developer:rust
Claude Code(CLI)——命令行设置:
claude mcp add octocode -- octocode mcp --path /path/to/your/project
Claude Desktop / Cursor / Windsurf——添加到配置中:
{
"mcpServers": {
"octocode": {
"command": "octocode",
"args": ["mcp", "--path", "/path/to/your/project"]
}
}
}
配置位置:
- Claude Desktop:
~/Library/Application Support/Claude/claude_desktop_config.json(macOS) - Cursor:
~/.cursor/mcp.json或“设置” → “MCP Servers” - Windsurf:“设置” → “MCP”
📖 完整的 MCP 客户端设置指南——包含 VS Code(Cline/Continue)、Zed、Replit 等 15 多种客户端的详细说明。
🌐 支持的语言
支持 17 种语言,并提供完整的 tree-sitter AST 解析:
| 语言 | 扩展名 | 功能 |
|---|---|---|
| Rust | .rs | 完整的 AST 解析、pub/use 检测、模块结构 |
| Python | .py | 导入/类/函数提取、文档字符串解析 |
| TypeScript/JavaScript | .ts、.tsx、.js、.jsx | ES6 导入/导出、类型定义 |
| Go | .go | 包/导入分析、结构体/接口解析 |
| PHP | .php | 类/函数提取、命名空间支持 |
| C++ | .cpp、.cc、.cxx、.c++、.c、.h、.hpp、.hxx、.cppm、.ixx、.mxx、.ccm、.cxxm | include 分析、类/函数提取、C++20 模块支持 |
| Ruby | .rb | 类/模块提取、方法定义 |
| Elixir | .ex、.exs | 模块/协议提取、函数和宏定义 |
| Java | .java | 导入分析、类/方法提取 |
| Swift | .swift | 类/结构体/协议提取、导入分析 |
| Svelte | .svelte | 组件结构、script/style 块提取 |
| Lua | .lua | 函数和表提取 |
| CSS | .css、.scss、.sass | 规则和选择器提取 |
| JSON | .json | 结构分析、键提取 |
| Bash | .sh、.bash | 函数和变量提取 |
| Markdown | .md、.markdown | 文档章节索引、标题提取 |
📚 文档
- 开始使用——入门步骤和基本工作流
- 安装指南——详细的安装方式及从源代码构建
- MCP 客户端设置——连接到 Claude、Cursor、Windsurf 等 15 多种客户端
- MCP 集成——MCP 服务详情和高级配置
- 命令参考——完整的 CLI 参考
- 配置——模板和自定义设置
- API 密钥——提供方设置指南
- 架构——深入了解其工作原理
- 参与贡献——开发环境设置
🔒 隐私与安全
- 🏠 本地优先 — 通过 fastembed 完全在本地生成嵌入(无需 API 密钥);云端提供商为可选项
- 🔐 安全 — API 密钥存储在本地,支持环境变量
- 🚫 遵循 .gitignore — 从不索引敏感文件
- 🛡️ MCP 安全 — 仅限本地的服务,搜索不使用外部网络
- 📤 云端安全 — 云端提供商只会接收要生成嵌入的代码片段;如需完全离线索引,请使用本地模型
📊 检索质量基准测试
我们使用手工标注的基准数据集来衡量语义搜索质量。该数据集包含 254 个查询(127 个代码查询 + 127 个文档查询),并标注了精确的行范围。每个查询有 1–3 个预期结果,并按相关性评分。
以下数据使用完整的云端技术栈——上下文检索、Voyage 重排序器、RaBitQ 量化——基于提交 b1771ba 和基准测试配置得出。如需了解完全本地的基线和完整的变体矩阵,请参阅上文的检索质量和 benchmark/RESULTS.md。
文档搜索(--mode docs)— Hit@10:0.953,MRR:0.776
| 指标 | 得分 |
|---|---|
| Hit@5 | 0.929 (118/127) |
| Hit@10 | 0.953 (121/127) |
| MRR | 0.776 |
| NDCG@10 | 0.801 |
| Recall@5 | 0.902 |
| Recall@10 | 0.921 |
未命中查询(127 个中有 6 个):
| # | 查询 | 预期结果 | 实际结果(排名第 1) |
|---|---|---|---|
| 43 | 如何设置 MCP 代理来管理多个代码库 | doc/MCP_INTEGRATION.md:286-311 | doc/MCP_INTEGRATION.md:286-4 |
| 51 | 使用 octocode 前有哪些先决条件 | doc/GETTING_STARTED.md:6-12 | doc/CONTRIBUTING.md:7-33 |
| 59 | 遇到 API 速率限制时该怎么办 | doc/GETTING_STARTED.md:209-216 | doc/PERFORMANCE.md:304-356 |
| 75 | 小型、中型和大型项目的典型性能指标 | doc/PERFORMANCE.md:4-13 | doc/PERFORMANCE.md:414-14 |
| 112 | 如何在不同操作系统上安装 octocode | INSTALL.md:4-14 | INSTALL.md:49-70 |
| 115 | 如何解决 macOS Gatekeeper 阻止二进制文件运行的问题 | INSTALL.md:199-206 | INSTALL.md:198-119 |
代码搜索(--mode code)— Hit@10:0.992,MRR:0.895
| 指标 | 得分 |
|---|---|
| Hit@5 | 0.992 (126/127) |
| Hit@10 | 0.992 (126/127) |
| MRR | 0.895 |
| NDCG@10 | 0.906 |
| Recall@5 | 0.962 |
| Recall@10 | 0.974 |
未命中查询(127 个中有 1 个):
| # | 查询 | 预期结果 | 实际结果(排名第 1) |
|---|---|---|---|
| 105 | 系统如何确保两位开发者获得相同的数据库路径 | src/storage.rs:60-83 | src/mcp/proxy.rs:631-644 |
指标说明:Hit@k(是否出现答案?)、MRR(排名有多高?)、NDCG@10(最佳结果是否排在前面?)、Recall@k(找到了多少?)。有关方法、评分脚本和完整数据集,请参阅 benchmark/。
🤝 社区与支持
- ⭐ 在 GitHub 上给我们点星 — 这对我们帮助很大!
- 🐛 报告问题
- 💬 参与讨论
- 📧 [email protected]
- 🌐 muvon.io
⚖️ 许可证
Apache License 2.0 — 详情请参阅 LICENSE。
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。