关于 Codemesh
Codemesh 是面向 AI 编码 Agent 的 MCP 服务,可将代码仓库索引为持久、可查询的代码知识图谱。Agent 可浏览符号与调用关系,并保存摘要和工作流供后续会话使用。
主要功能
- 01仓库映射与符号导航
- 02源码检查与调用追踪
- 03搜索与排序后的上下文组装
- 04持久化 Agent 笔记与工作流
- 05基于 SQLite 索引,无需嵌入
适用场景
- 01按目录和符号探索陌生代码库
- 02追踪调用以理解代码行为
- 03保存调查结果供后续会话使用
MCP 相关
向 MCP 客户端提供仓库映射、源码检查、搜索、调用追踪、回忆、状态检查和 Agent 写回工具。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
Codemesh
面向 AI 编码 Agent 的智能代码知识图谱
与基准 Grep+Read 相比,成本降低 71%,速度提升 72%,工具调用减少 82%
在 6 个真实代码仓库上测试(Sonnet 4.6)——只需运行一次 codemesh index。
问题所在
AI 编码 Agent 有 40–80% 的 token 浪费在探索上——在文件中搜索、读取无关代码,以及重新构建之前会话中已经了解的上下文。
在包含 600 个文件的代码库中,Agent 执行一项典型的探索任务时,往往要先读取 10 多个文件,才能知道哪些内容相关。
Before: Agent → Grep → 50 matches → Read 10 files → Understand → Work
After: Agent → codemesh_map → expand chosen branches → read exact symbols → Work
Codemesh 是一个 MCP 服务,为 Agent 提供持久、可查询的知识图谱。图谱会随着时间推移变得更智能:Agent 将学到的内容写回图谱,让下一个会话一开始就掌握相关信息。
基准测试
使用 Claude Sonnet 4.6,在 6 个真实代码库(Alamofire、Excalidraw、VS Code、Swift Compiler、pydantic-validators、pydantic-basemodel)上进行基准测试,并与基准方案和基于图谱的上下文方案进行比较。
完整的方法说明、各仓库细分结果和两两比较:docs/benchmark-results.md | 早期 pydantic 评估
这些历史测试使用的是早期的答案优先工作流。下文所述的地图优先导航工作流在一项探索性 Pydantic 试点测试中进行了评估;本节中的数字 不应被视为该工作流的实测表现。
成本
| 模式 | Alamofire | Excalidraw | VS Code | Swift Compiler1 | pydantic-validators | pydantic-basemodel | 平均 |
|---|---|---|---|---|---|---|---|
| 基准方案 | $0.54 | $0.89 | $0.21 | $0.83 | $1.32 | $0.78 | $0.76 |
| Codemesh MCP | $0.25 | $0.21 | $0.16 | $0.23 | $0.33 | $0.13 | $0.22 |
| Codemesh CLI | $0.67 | $0.51 | $0.16 | $0.83 | $1.00 | $0.18 | $0.56 |
| Codegraph | $0.37 | $0.56 | $0.57 | $0.74 | $0.29 | $0.19 | $0.45 |
耗时
| 模式 | Alamofire | Excalidraw | VS Code | Swift1 | pydantic-v | pydantic-b | 平均 |
|---|---|---|---|---|---|---|---|
| 基准方案 | 180s | 191s | 87s | 199s | 352s | 232s | 207s |
| Codemesh MCP | 78s | 45s | 35s | 87s | 72s | 32s | 58s |
| Codemesh CLI | 226s | 177s | 62s | 227s | 235s | 51s | 163s |
| Codegraph | 134s | 180s | 192s | 199s | 75s | 60s | 140s |
工具调用(Agent 轮次)
| 模式 | Alamofire | Excalidraw | VS Code | Swift1 | pydantic-v | pydantic-b | 平均 |
|---|---|---|---|---|---|---|---|
| 基准方案 | 31 | 48 | 12 | 29 | 84 | 65 | 45 |
| Codemesh MCP | 9 | 5 | 3 | 14 | 14 | 3 | 8 |
| Codemesh CLI | 30 | 32 | 12 | 56 | 64 | 9 | 34 |
| Codegraph | 31 | 35 | 44 | 44 | 20 | 12 | 31 |
质量(1–10 分,由大模型评判)
| 模式 | Alamofire2 | Excalidraw | VS Code | Swift Compiler | pydantic-validators | pydantic-basemodel | 平均 |
|---|---|---|---|---|---|---|---|
| 基准方案 | 不适用 | 9 | 8 | 7 | 2 | 9 | 7.0 |
| Codemesh MCP | 9 | 9 | 7 | 8 | 7 | 7.8 | 7.9 |
| Codemesh CLI | 9 | 7 | 7 | 9 | 1 | 8.4 | 6.9 |
| Codegraph | 8 | 9 | 8.7 | 8 | 8 | 9 | 8.4 |
成本节省:Codemesh MCP 与基线的对比
| 仓库 | 基线 | Codemesh MCP | 节省成本 | 节省时间 |
|---|---|---|---|---|
| Alamofire | $0.54 | $0.25 | −54% | −57%(180s → 78s) |
| Excalidraw | $0.89 | $0.21 | −76% | −76%(191s → 45s) |
| VS Code | $0.21 | $0.16 | −24% | −60%(87s → 35s) |
| Swift Compiler1 | $0.83 | $0.23 | −72% | −56%(199s → 87s) |
| pydantic-validators | $1.32 | $0.33 | −75% | −79%(352s → 72s) |
| pydantic-basemodel | $0.78 | $0.13 | −83% | −86%(232s → 32s) |
| 平均值 | $0.76 | $0.22 | −71% | −72% |
说明
Codemesh MCP 在所有受测模式中成本最低、速度最快——在 6 个仓库中平均比基线便宜 71%、快 72%,且工具调用次数减少 82%(8 次对比 45 次)。质量与基线相当(7.9 对比 7.0);Codegraph 的质量略胜 Codemesh(8.4),但成本约为其两倍($0.45 对比 $0.22)。所有仓库都实现了成本和时间节省——包括理解类查询(Excalidraw、pydantic-basemodel),这些查询在 codemesh 的早期构建版本中表现退步。
快速开始
1. 安装
npm install -g @pyalwin/codemesh
也可以直接运行,无需安装:
npx -y @pyalwin/codemesh --help
从源代码构建
git clone https://github.com/pyalwin/codemesh.git
cd codemesh
npm install && npm run build
npm link
验证安装:
codemesh --version应输出软件包版本。
2. 为项目建立索引
cd /your/project
codemesh index
默认的索引和检索路径会在 .codemesh/ 中使用一个 SQLite 数据库。
它不会生成嵌入向量,也不需要向量数据库。较早运行生成的现有向量
工件会保留,但此路径不会使用它们。
Indexed 656 files
Symbols found: 16733
Edges created: 33266
Duration: 10009ms
PageRank: 13843 nodes scored
3. 选择模式
Codemesh 提供两种与 AI Agent 集成的方式:
选项 A:MCP 服务(结构化工具调用)
将以下内容添加到 Claude Code MCP 配置中(~/.claude/mcp-servers.json 或项目的 .mcp.json):
{
"mcpServers": {
"codemesh": {
"command": "npx",
"args": ["-y", "@pyalwin/codemesh"],
"env": {
"CODEMESH_PROJECT_ROOT": "/path/to/your/project"
}
}
}
}
Agent 会获得原生 MCP 工具:
codemesh_map— 浏览索引仓库的一个层级,或沿链接继续浏览codemesh_source— 按符号 ID 读取经过修订版本校验的源代码视图;ifHash可避免重复传输codemesh_inspect— 检查确切的符号及其直接调用,并获取范围受限、内容精确的源代码块codemesh_hydrate— 验证已保存的符号引用,并在交接后重新打开确切的源代码codemesh_answer— 可选的排序上下文组装codemesh_explore— 搜索、上下文(多个目标)、影响分析codemesh_trace— 跟踪调用链codemesh_enrich/codemesh_workflow— 写回codemesh_recall— 检索笔记和工作流,并显示新鲜度状态codemesh_status— 健康检查
最适合: Opus、结构化工作流、丰富内容/写回
选项 B:CLI 模式(通过 Bash 使用,零 MCP 开销)
无需配置 MCP。Agent 通过 Bash 直接调用 codemesh:
export CODEMESH_PROJECT_ROOT=/path/to/your/project
# Navigate one level at a time, then read a selected symbol:
codemesh explore map
codemesh explore map --node 'dir:src'
codemesh explore map --node 'file:src/server.ts'
codemesh explore source 'symbol:src/server.ts:createServer'
codemesh explore source 'symbol:src/server.ts:createServer' --mode preview
codemesh explore inspect 'symbol:src/server.ts:createServer' --depth 1 --max-symbols 4 --compact
codemesh explore hydrate --references '[{"id":"symbol:src/server.ts:createServer","bodyHash":"HASH"}]' --compact
# Other exploration commands:
codemesh explore search "request flow"
codemesh explore context Source/Core/Session.swift Source/Core/Request.swift
codemesh explore trace Session.request --depth 5
codemesh explore recall --path src/server.ts
codemesh remember --path src/server.ts --summary "Server tool registration" --session my-task
探索和写入命令会将 JSON 输出到 stdout。无需运行 MCP 服务进程。
最适合: Sonnet/Haiku、注重速度的工作流、更简单的设置
| MCP 服务 | 命令行模式 | |
|---|---|---|
| 设置 | MCP 配置文件 | 只需 export CODEMESH_PROJECT_ROOT |
| 调用 | 持久运行的 MCP 进程 | 命令行子进程 |
| 增强 | 原生 codemesh_enrich 工具 | codemesh remember ... |
| 检索 | 共用由 SQLite 支持的处理程序 | 相同的处理程序和 JSON 数据 |
| 推荐场景 | 能可靠使用 MCP 工具的宿主 | 确定性的 Shell 工作流 |
4. 使用
Agent 可以先沿着持久化图谱选择路径,再阅读代码:
You: "Find how pydantic handles validation"
Agent calls: codemesh_map({})
gets: immediate repository directories and files, each with an ID
Agent calls: codemesh_map({ node: "dir:pydantic" })
gets: immediate files and subdirectories
Agent calls: codemesh_map({ node: "file:pydantic/main.py" })
gets: top-level symbols, including BaseModel
Agent calls: codemesh_map({ node: "symbol:pydantic/main.py:BaseModel" })
gets: methods, including model_validate
Agent calls: codemesh_source({ id: "symbol:pydantic/main.py:BaseModel.model_validate" })
reads: only the selected symbol's source
Agent calls: codemesh_enrich({ path: "pydantic/functional_validators.py",
summary: "Primary V2 validator API..." })
saves: summary for next session
客户端集成
Codemesh 支持 Model Context Protocol,因此任何兼容 MCP 的客户端都可以使用它。粘贴下面的某个代码片段,然后重启客户端,codemesh_* 工具就会出现在 Agent 的工具箱中。
Claude Code(命令行)
添加到 ~/.claude/mcp-servers.json(用户级)或 .mcp.json(项目级):
{
"mcpServers": {
"codemesh": {
"command": "npx",
"args": ["-y", "@pyalwin/codemesh"],
"env": {
"CODEMESH_PROJECT_ROOT": "/absolute/path/to/your/project"
}
}
}
}
Claude Desktop(macOS / Windows 应用)
编辑 claude_desktop_config.json:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"codemesh": {
"command": "npx",
"args": ["-y", "@pyalwin/codemesh"],
"env": {
"CODEMESH_PROJECT_ROOT": "/absolute/path/to/your/project"
}
}
}
}
重启 Claude Desktop。Codemesh 的工具会显示在工具选择器中(锤子图标)。
Cursor — 防止 Agent 在你的代码库中漫无目的地搜索
Cursor 会读取每个项目的 .cursor/mcp.json(或供所有项目使用的 ~/.cursor/mcp.json):
{
"mcpServers": {
"codemesh": {
"command": "npx",
"args": ["-y", "@pyalwin/codemesh"],
"env": {
"CODEMESH_PROJECT_ROOT": "${workspaceFolder}"
}
}
}
}
打开 Settings → MCP,确认 codemesh 显示为绿色,然后在提示词中提及它(@codemesh how does auth work?),引导 Agent 使用图谱查询,而不是递归执行 Grep。
Windsurf / VS Code (Continue)
在 ~/.continue/config.json 的 experimental.modelContextProtocolServers 下添加:
{
"experimental": {
"modelContextProtocolServers": [
{
"transport": {
"type": "stdio",
"command": "npx",
"args": ["-y", "@pyalwin/codemesh"],
"env": {
"CODEMESH_PROJECT_ROOT": "/absolute/path/to/your/project"
}
}
}
]
}
}
Agent 写回:让图谱不断变得更智能
其他代码智能工具通常只会为你的代码库建立一次索引,然后向 Agent 提供只读视图。Codemesh 让 Agent 在工作过程中为图谱提供知识——摘要、工作流和跨概念链接会在不同会话间持久保存,并在重新索引后保留。
// Session 1 — agent reads unfamiliar code, then writes back what it learned.
codemesh_enrich({
path: "pydantic/functional_validators.py",
summary: "Primary V2 validator API. `@field_validator` wraps "
+ "`_decorators.FieldValidatorDecoratorInfo`; `mode='before'|'after'` "
+ "toggles pre/post-coercion execution. Extends BaseValidator.",
sessionId: "validation-investigation-1"
})
// Session 1 — agent traces a multi-file flow, records the path.
codemesh_workflow({
name: "pydantic field validation",
description: "Request → BaseModel.__init__ → SchemaValidator → field_validator",
files: [
"pydantic/main.py",
"pydantic/_internal/_model_construction.py",
"pydantic/functional_validators.py"
]
})
// Session 2 (days later) — another agent navigates the same indexed code.
codemesh_map({ node: "file:pydantic/functional_validators.py" })
// → returns stable symbol IDs without reading their bodies.
codemesh_explore({ action: "context", path: "pydantic/functional_validators.py" })
// → includes the earlier agent's concept alongside indexed relationships.
现在,图谱了解了静态分析器无法推断的信息:文件为何重要、哪些文件会一起变动,以及维护者如何称呼某个概念。重新索引会重建结构层(文件、符号、导入、调用),但会保留所有增强信息——只有引用的文件发生变化时,条目才会过时。
参见 MCP 工具中的 codemesh_enrich 和 codemesh_workflow。
工作原理
┌──────────────────────────────────┐
│ Knowledge Graph │
│ │
│ ┌──────────┐ ┌───────────────┐ │
│ │Structural│ │ Semantic │ │
│ │ (auto) │ │ (agents) │ │
│ │ │ │ │ │
│ │ files │ │ summaries │ │
│ │ symbols │ │ workflows │ │
│ │ imports │ │ concepts │ │
│ │ calls │ │ enrichments │ │
│ └──────────┘ └───────────────┘ │
│ │
│ ┌──────────┐ ┌───────────────┐ │
│ │ Git │ │ Search │ │
│ │ Intel │ │ │ │
│ │ │ │ FTS5 (exact) │ │
│ │ hotspots │ │ Trigram (fuzzy)│ │
│ │ co-change│ │ notes + cache │ │
│ │ churn │ │ PageRank │ │
│ └──────────┘ └───────────────┘ │
│ │
│ SQLite only │
└────────────┬──────────────────────┘
│
┌────────────┴──────────────────────┐
│ MCP Server / CLI (shared core) │
│ │
│ map · source · answer · explore │
│ trace · recall · enrich · workflow │
│ status │
└────────────────────────────────────┘
结构层(自动构建)——Tree-sitter 会将你的代码解析为文件、符号(函数、类、方法)和关系(导入、调用、继承)。每次建立索引时都会重建。
语义层(由 Agent 构建)——Agent 在处理你的代码时,会写回摘要和工作流路径。这些内容会在重新索引后保留,并在不同会话间累积。引用的文件发生变化时,相关内容会失效。
MCP 工具
| 工具 | 用途 | 示例 |
|---|---|---|
codemesh_map | 在目录、文件、符号和图链接之间进行有界的单层导航 | codemesh_map({ node: "dir:src", limit: 30 }) |
codemesh_source | 获取所选符号 ID 对应的精确源代码 | codemesh_source({ id: "symbol:src/auth.ts:login" }) |
codemesh_inspect | 在字节和符号预算范围内,获取符号的精确源代码及直接调用证据 | codemesh_inspect({ id: "symbol:src/auth.ts:login", depth: 1 }) |
codemesh_hydrate | 验证已保存的符号引用,并在上下文丢失后重新打开精确源代码 | codemesh_hydrate({ references: [{ id: "symbol:src/auth.ts:login", bodyHash: "..." }] }) |
codemesh_answer | 可选的上下文排序组装,并附带源代码片段 | codemesh_answer({ question: "How does auth work?" }) |
codemesh_explore | 搜索、上下文(多目标)、影响分析 | codemesh_explore({ action: "search", query: "auth" }) |
codemesh_trace | 跟踪调用链并显示源代码 | codemesh_trace({ symbol: "login", depth: 5 }) |
codemesh_enrich | 写回所学内容,供后续会话使用 | codemesh_enrich({ path: "src/auth.py", summary: "..." }) |
codemesh_workflow | 记录多文件工作流路径 | codemesh_workflow({ name: "login flow", files: [...] }) |
codemesh_status | 检查图的健康状况 | codemesh_status() |
命令行
codemesh index # structural + git intel + pagerank
codemesh rebuild # force re-index; preserves saved knowledge
codemesh status # graph statistics
codemesh explore map # repository root
codemesh explore map --node 'dir:src' # expand one branch
codemesh explore map --node 'symbol:src/auth.ts:login' --relation calls
codemesh explore source 'symbol:src/auth.ts:login'
codemesh explore source 'symbol:src/auth.ts:login' --if-hash HASH # unchanged reference if bytes match
codemesh explore source 'symbol:src/auth.ts:login' --force # rehydrate exact source
codemesh explore inspect 'symbol:src/auth.ts:login' --depth 1 --compact
codemesh explore hydrate --references '[{"id":"symbol:src/auth.ts:login","bodyHash":"HASH"}]' --compact
codemesh explore answer "question" # optional ranked context assembly
codemesh explore search "query" # SQLite FTS5 + trigram
codemesh explore search get_default --path pydantic --scope symbols --max-bytes 4096
codemesh explore inspect 'symbol:src/auth.py:login' --direction both --max-candidates 16
codemesh explore recall --path file # prior notes with freshness
codemesh explore context file1 file2 # multi-target context
codemesh explore trace symbol --depth 5 # follow call chains
codemesh explore impact file # reverse dependencies
codemesh remember --path file --summary "note" --session task-id
codemesh workflow --name flow --description "flow description" --file first.ts --file second.ts
codemesh explore search "query" --compact # 8 candidates with IDs and match reasons
codemesh explore source 'symbol:src/auth.ts:login' --mode full --compact
codemesh explore source 'symbol:src/auth.ts:login' --mode auto --compact
rg -n 'login' src | codemesh adapt search # reversible fold when smaller
cat src/auth.ts | codemesh adapt read --path src/auth.ts --intent navigate
--compact 可用于 map、search、source、recall、inspect 和 hydrate。它会从命令行 JSON 中移除重复的元数据;source 仍会包含请求的精确代码和内容哈希。导航映射、搜索、inspect 和 hydrate 会输出有界 JSON,不进行美化排版;其精简形式会保留精确源代码和新鲜度哈希。hydrate 还接受 --references-file PATH,用于指定保存在磁盘上的 JSON 数组。
有界响应会省略额外的项目根目录包装,以确保最终 JSON 不超出请求的预算。其他命令仍沿用现有响应格式。
交接时,请保存新鲜 source 结果中的符号精确 ID 和 bodyHash。codemesh_hydrate 会在返回完整代码前,将该哈希与实时源代码进行校验。如果已保存的 hash 来自 fold 或 preview,请包含其 mode;该哈希只校验渲染后的视图,因此还需包含 bodyHash 才能验证整个符号。达到预算限制时,Inspect 和 hydrate 会明确报告省略的代码块。两者默认最多返回 4 个源代码块,JSON 响应预算为 24 KiB。inspect 最多跟踪一跳,并保守地解析本地 Python 函数和导入。使用 --direction incoming 或 both 也可检查候选调用方;--max-candidates 限制调用方验证数量(默认值为 32,最大值为 128)。索引边仅供参考:每个返回的调用方都会根据其当前 Python 绑定进行检查。如果缺少边,传入方向的发现结果会明确标注为不完整;结果为空并不能证明某个符号没有调用方。动态接收方、名称歧义和不确定的绑定会明确标记为未解析;其他语言则会提供未经验证的图候选项。这些是静态绑定,并不保证运行时行为。Hydration 返回经过实时校验的代码;客户端决定在交接后何时调用它,以及在其模型上下文中保留哪些证据。
--mode auto 是一种显式导航视图:长度不超过 120 行且大小不超过 12KiB 的符号会完整返回;更大的符号会返回有界预览,标出省略的范围,并提示映射其成员。显式指定 --mode full 时始终返回精确源码。
搜索会优先排列精确标识符以及精确限定名称/叶名称,其次考虑查询词覆盖率和词法相关性,仅在平局时使用热门度作为决胜依据。
若要选择性启用结构提示,--exact-tie-break path-depth 会在排名相同的精确名称/叶名称匹配项中,优先选择路径较浅的项,再以热门度作为决胜依据。MCP 在 codemesh_explore 上提供相同的 exactTieBreak 选项。
显式 ID 和路径过滤器仍可选择更深层的实现;深度并不能决定哪个实现是规范实现。默认值仍为 pagerank。
--path 会在 SQLite 结果上限生效前,将候选项限制在某个文件或目录内;src 不会匹配 src-other。精简搜索包含签名和范围。
搜索和导航的 JSON 预算默认为 8192 字节;--max-bytes 接受 512..65536。省略的结果会明确标出。导航游标始终指向第一个未返回的条目;如果仅该条目就超出预算,请增大 --max-bytes 并重用该游标。源码仍需通过 source/inspect 获取。
确定性 Graphify 后续方案介绍了单 Agent 设计和聚焦上下文策略。上下文替换由框架负责:MCP/CLI 工具无法移除客户端先前发送的消息。受控的下一轮迭代介绍了精简的评估指令、可选的搜索排序,以及两个多文件交接测试样例。
源码检索也接受唯一的符号名称,例如 MyType.myMethod。
遇到名称不明确时,会返回精确的候选 ID 和范围;绝不会根据大小猜测重载。跨会话引用时,仍应优先使用精确 ID。
adapt 从标准输入读取原生工具观测结果。搜索折叠会保留每个匹配项,并在不支持的格式下回退。读取大纲要求内容与新近索引的文件完全按字节匹配,且明确指定导航意图;编辑读取和部分读取则保持原样传递。--json 会报告路由和原因,而不是只输出转换后的观测结果。
可选:Hooks 和技能
技能 — 教会 Agent 以图谱优先的方式工作
将 skills/codemesh.md 复制到 ~/.claude/skills/ 或项目的 .claude/skills/。
# Install the skill so Claude Code loads the workflow automatically
cp /path/to/codemesh/skills/codemesh.md /your/project/.claude/skills/
该技能会指导 Agent 在使用 Grep/Read 前先查询图谱,并在阅读代码后通过 codemesh_enrich 写回信息。
Hooks — 自动注入读取前上下文
添加到 .claude/settings.json:
{
"hooks": {
"PreToolUse": [{
"matcher": "Read",
"hooks": [{
"type": "command",
"command": "/path/to/codemesh/hooks/pre-read.sh"
}]
}],
"PostToolUse": [{
"matcher": "Read",
"hooks": [{
"type": "command",
"command": "/path/to/codemesh/hooks/post-read.sh"
}]
}]
}
}
- 读取前 — 在待处理的 Read 结果旁添加标记为非过时的缓存摘要
- 读取后 — 提醒 Agent 在阅读不熟悉的文件后进行信息补充
支持的语言
| TypeScript | JavaScript | Python | Go | Rust | Java | C# |
| Ruby | PHP | C | C++ | Swift | Kotlin | Dart |
任何具有 tree-sitter 语法的语言都可以添加。
图数据模型
节点
| 类型 | 来源 | 关键字段 |
|---|---|---|
file | 静态(tree-sitter) | path、hash、last_indexed_at |
symbol | 静态(tree-sitter) | name、kind、file_path、line_start、line_end、signature |
concept | Agent 编写 | summary、last_updated_by、stale |
workflow | Agent 编写 | description、file_sequence、last_walked_at |
边
| 类型 | 方向 | 来源 |
|---|---|---|
contains | 文件 → 符号 | 静态 |
imports | 文件 → 文件 | 静态 |
calls | 符号 → 符号 | 静态 |
extends | 符号 → 符号 | 静态 |
describes | 概念 → 文件/符号 | Agent |
related_to | 概念 → 概念 | Agent |
traverses | 工作流 → 文件 | Agent |
架构
codemesh/
├── src/
│ ├── index.ts # MCP server entry (stdio transport)
│ ├── server.ts # Tool registration (zod schemas)
│ ├── graph/
│ │ ├── types.ts # Node/edge type definitions
│ │ ├── storage.ts # StorageBackend interface (swappable)
│ │ └── sqlite.ts # SQLite + FTS5 implementation
│ ├── indexer/
│ │ ├── indexer.ts # File walking, hashing, incremental indexing
│ │ ├── parser.ts # Tree-sitter AST extraction
│ │ └── languages.ts # Language registry (ext → grammar)
│ ├── tools/ # 6 MCP tool handlers
│ └── cli.ts # CLI entry point
├── skills/codemesh.md # Agent education skill
├── hooks/ # Pre/post read hooks
└── eval/ # Eval framework (5 tasks, 3 models)
存储与后端无关。StorageBackend 接口抽象了所有持久化操作。v1 使用 SQLite 和 FTS5,实现零依赖的本地运行。该接口支持切换到 Memgraph、Neo4j 或其他图数据库。
评测框架
可复现的评测工具,使用 LLM-as-judge 评分:
# Setup
npm install -g @pyalwin/codemesh
git clone --depth 1 https://github.com/Alamofire/Alamofire.git /tmp/alamofire
# ... clone other repos ...
# Index
CODEMESH_PROJECT_ROOT=/tmp/alamofire codemesh index
# Run benchmarks
python3 eval/head_to_head.py --model sonnet alamofire excalidraw vscode swift-compiler
完整的方法和结果请参见 docs/benchmark-results.md。早期的 pydantic 评测已归档在 docs/experiments/。
仅使用 SQLite 的 CLI 还有一套独立的 vNext 契约与 Agent 工作流评测,基于固定版本的 Pydantic 代码库。其正确性检查及模型成本/质量对比与历史基准测试分开报告。 vNext Pydantic 结果记录了首轮完整配对运行。
与现有工具对比
| 功能 | CodeGraph | Graphify | Axon | Codemesh |
|---|---|---|---|---|
| 结构索引 | 是 | 是 | 是 | 是 |
| FTS 搜索 | 是 | — | 是 | 是 |
| Agent 回写 | — | — | — | 是 |
| 工作流记忆 | — | — | — | 是 |
| 钩子拦截 | — | — | — | 是 |
| 可切换后端 | — | — | — | 是 |
| 评测框架 | — | — | — | 是 |
| 已发布的基准测试 | — | — | — | 是 |
开发
bun install # Install dependencies
bun run build # Compile TypeScript
bun run test # Run 102 tests
bun run dev # Watch mode
bun run lint # Type check
贡献
欢迎贡献。可改进的方面:
- 更多语言 — 添加 tree-sitter 语法和特定语言的提取器
- AST 差异失效 — 按函数而非按文件检测过时内容
- 图后端 — 为
StorageBackend添加 Memgraph/Neo4j 适配器 - 语义搜索 — 在 FTS5 之外添加嵌入列
- Agent 采用 — 改进模式,让 Agent 自然地优先使用图工具
许可证
MIT
Footnotes
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。