@sdsrs/code-graph 0.163.0 → 0.165.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,172 +1,61 @@
1
1
  ---
2
- name: code-graph-mcp 插件契约
3
- description: code-graph-mcp 工具调度规则 — 何时用 MCP/CLI 替代 Grep/Read,invited-memory 模式
2
+ name: code-graph-mcp plugin contract
3
+ description: When to use the code-graph CLI / MCP tools instead of rounds of Grep + Read, and what the graph cannot see
4
4
  type: reference
5
5
  ---
6
- # code-graph-mcp 插件契约
7
-
8
- > 本文件是 code-graph 工具调度细则。项目根 `CLAUDE.md` 里有一个精炼的 managed
9
- > 块(sentinel 包裹)做路由摘要 + 指针;本完整决策表按需打开,不每会话常驻。
10
- >
11
- > **v0.74 起**:插件(`/plugin install`)模式下首次 SessionStart 自动安装——
12
- > 在项目根 `CLAUDE.md` 注入/创建 managed 块,并把本文件写到 `.claude/plugin_code_graph_mcp.md`。
13
- > 旧版(≤v0.73)写到 `~/.claude/projects/<slug>/memory/`(MEMORY.md sentinel + 本文件),
14
- > 升级后首次 SessionStart 自动清理那些遗留制品(你的其它 memory 不受影响)。
15
- > 退出:`CODE_GRAPH_NO_AUTO_ADOPT=1` 阻止,`code-graph-mcp unadopt` 回退。
16
- >
17
- > 已安装的项目在下次 SessionStart 会自动对齐到插件 shipped 的最新决策表
18
- > (本文件 / CLAUDE.md 块与 shipped 差异时覆盖)。手动编辑会被覆盖——
19
- > 要锁定自己的版本,设 `CODE_GRAPH_NO_TEMPLATE_REFRESH=1`(不影响首次安装)。
20
- >
21
- > **Hook 默认值(两个 hook,默认不同 —— 故意的)**:
22
- > - **SessionStart `project_map` 注入:默认 OFF**(v0.17.0 起)。本文件 + 7 个
23
- > 工具描述已经覆盖路由所需决策信息,每次会话再 dump ≈2.3 KB 项目地图是冗余的
24
- > 常驻上下文。显式启用:`CODE_GRAPH_VERBOSE_HOOKS=1`;或按需 `code-graph-mcp map --compact`。
25
- > - **UserPromptSubmit context push:默认 ON**。基于用户消息 intent 推 impact /
26
- > overview / callgraph / search 结果(per-type cooldown 30s–5min)。routing-bench
27
- > P@1=100% 测的是分诊准确率(已决定查工具时选哪个),不等于触发率(是否
28
- > 决定查工具)—— 真实 baseline 是 raw-grep ≈13× 偏向于内置 Grep。Push 是
29
- > pre-training bias 的矫正。Escape hatch:`CODE_GRAPH_QUIET_HOOKS=1`。
30
- > - 优先级:`CODE_GRAPH_QUIET_HOOKS=1` (escape) > 其他 env > 默认。
31
- >
32
- > **v0.18.4 起**:原"进阶 5"(impact / similar / deps / dead-code / trace)已折叠
33
- > 进核心 7 的 flag —— Claude Code 现在能直接通过 MCP 调用,不必落到 CLI:
34
- > - `get_ast_node include_impact=true` / `include_similar=true`
35
- > - `module_overview include_deps=true` / `include_dead=true`
36
- > - `get_call_graph route_path="GET /api/x"`
37
- >
38
- > `impact_analysis` **已删除**——dispatcher 里没有这个 arm,按名调用返回
39
- > `Unknown tool`。改用 `get_ast_node include_impact=true`,或 CLI `impact`。
40
- > 其余旧名(`read_snippet` / `dependency_graph` / `find_similar_code` /
41
- > `find_dead_code` / `trace_http_chain` + alias `find_http_route`)仍是向后兼容
42
- > dispatcher 别名(raw JSON-RPC / SDK 脚本场景),但 Claude Code 内一律用上面的
43
- > 新 flag 形式。CLI 子命令
44
- > (`code-graph-mcp impact|similar|deps|dead-code|trace`)保持不变,给 Bash 工作流。
45
-
46
- ## 何时调用 MCP/CLI(替代多步 Grep/Read)
47
-
48
- > **v0.49 起 CLI 优先**:Claude Code 里 MCP 工具是 deferred(首次调用前要
49
- > ToolSearch 加载),而 Bash 永远在线——真实编程夜(2026-06-12)观测到的全部
50
- > 转化都是 CLI 调用。结构化查询的最快路径是 Bash 直呼
51
- > `code-graph-mcp callgraph X / show X / overview <dir> / grep "pat" / impact X`。
52
- >
53
- > **裸名字不在 PATH 上时**(纯 `/plugin install`,没跑过 `npm i -g`):插件自己
54
- > 下载的那份在 `~/.cache/code-graph/bin/code-graph-mcp`,本文下面所有查询类子命令
55
- > (callgraph / impact / overview / show / search / grep / refs / similar / deps /
56
- > dead-code / trace)都能直接用它跑;`npm i -g @sdsrs/code-graph` 则把裸名字放上
57
- > PATH(issue #41)。
58
- >
59
- > **三个例外**:`doctor` / `adopt` / `unadopt` 是 JS 派发的——二进制会去自己旁边
60
- > 找对应脚本(前者 `doctor.js`,后两者 `adopt.js`),而缓存目录里没有那些邻居,
61
- > 所以从缓存路径调它们会报 `<脚本名> not found`。直接跑脚本即可:
62
- > `node <插件目录>/claude-plugin/scripts/adopt.js unadopt`(SessionStart 的提示行
63
- > 会印出这台机器上的完整路径)、`node <插件目录>/claude-plugin/scripts/doctor.js`。
64
- >
65
- > `grep` 是 drop-in 替代:`-F` 字面 / `-i` / `-w` / `-l` / `-c` 计数 / `-t <lang>` 按语言筛 /
66
- > `-g <glob>` 路径过滤 / `-A/-B/-C N` 上下文 / `-M N` 行宽上限(默认 512,防长行刷屏)/
67
- > 多路径 / `-m 0` 取消每文件上限,退出码兼容 grep(0/1/2),召回达 git-grep 级
68
- > (tracked-but-gitignored 也能搜到),每条命中标注所属 fn/class。
69
- >
70
- > v0.10.0 起:tools/list 默认只暴露 7 个核心工具;下表"进阶 5"中的工具
71
- > 已从 tools/list 隐藏以节省 session 启动 tokens。**Claude Code 里请走 CLI
72
- > 子命令**(MCP schema 不在 list,Claude Code 的 ToolSearch 不会加载,直接
73
- > 调用会得到 `No such tool available`——实测验证见下方"进阶 5")。写
74
- > MCP SDK / 原生 `tools/call` JSON-RPC 的脚本场景仍可按名调用。
75
-
76
- ### 核心 7(tools/list 默认暴露)
77
-
78
- | 意图 | 工具 | 关键参数 / 例子 |
79
- |------|------|----------------|
80
- | "谁调用 X?" / "X 调了啥?" | `get_call_graph` / `callgraph X` | 替代 `grep "X("` |
81
- | "Y 模块长啥样?" | `module_overview` / `overview Y/` | 替代逐文件 Read |
82
- | "找做 Z 的代码"(概念) | MCP `semantic_code_search`(RRF 混合);CLI `search`(纯 FTS5) | 不知道精确名;要向量召回走 MCP |
83
- | "返回 T 类型的函数" | `ast_search --returns T` | 结构化筛选 |
84
- | "X 在哪被引用?" | `find_references` / `refs X` | 含 callers/importers;`refs X --min-confidence extracted` 滤掉跨文件裸名低置信边(inferred/ambiguous) |
85
- | "看 X 的源码 / 签名" | `get_ast_node` / `show X` | `include_impact=true` 影响面 / `include_similar=true` 嵌入近邻 |
86
- | "项目结构总览" | `project_map` / `map` | 起手势用 `--compact` |
87
- | "X 文件依赖谁?" / "Y 模块下的死代码" | `module_overview path=Y include_deps=true` / `include_dead=true` | 文件路径走 deps;目录/文件走 dead |
88
- | HTTP 路由 → handler 链 | `get_call_graph route_path="GET /api/x"` | 取代 trace_http_chain |
89
-
90
- ### 旧名兼容 + CLI 速查(v0.18.4 fold 后)
91
-
92
- v0.18.4 把原"进阶 5"折叠进核心 7 的 flag。**Claude Code 内**首选上表的新 flag 形式。
93
-
94
- 可按名调用的只有这 7 个:`project_map` / `semantic_code_search` / `get_call_graph` /
95
- `get_ast_node` / `module_overview` / `ast_search` / `find_references`。
96
- `impact_analysis` **不在其列,已从 dispatcher 删除**——按名调用返回 `Unknown tool`。
97
- 隐藏旧名 `read_snippet` / `dependency_graph` / `find_similar_code` / `find_dead_code` /
98
- `trace_http_chain`(+ alias `find_http_route`)仍有 dispatcher arm,raw JSON-RPC / MCP
99
- SDK / 既有脚本不破;但它们不在 `tools/list`,Claude Code 的 ToolSearch 不为其加载
100
- schema —— 实操中一律走新 flag。CLI 子命令保持原样:
101
-
102
- | 意图 | CLI(Bash 工作流) | 等价 MCP 新形式 |
103
- |------|--------------------|------------------|
104
- | "改 X 会炸啥?" | `code-graph-mcp impact X` | `get_ast_node symbol_name=X include_impact=true` |
105
- | HTTP 路由 → handler 链路 | `code-graph-mcp trace /api/x` | `get_call_graph route_path="GET /api/x"` |
106
- | "X 文件依赖谁?" | `code-graph-mcp deps src/x.rs` | `module_overview path="src/x.rs" include_deps=true` |
107
- | "相似/重复函数"(需 embedding) | `code-graph-mcp similar X` | `get_ast_node symbol_name=X include_similar=true` |
108
- | "未使用的代码" | `code-graph-mcp dead-code [path]` | `module_overview path=<path> include_dead=true` |
109
- | "架构咽喉/桥节点是谁?" | `code-graph-mcp centrality` | —(CLI-only;betweenness 中心性,补 `map` 的 caller_count 度中心性) |
110
- | "循环导入依赖(哪些文件互相 import)?" | `code-graph-mcp cycles` | —(CLI-only;文件级 import 环 = SCC;JS/TS/Py/Go 是坏味,Rust 内部环常良性) |
111
- | "可疑/意外的跨模块耦合?" | `code-graph-mcp surprising` | —(CLI-only;跨文件 calls/refs 按 低置信(ambiguous>inferred)+跨模块+sole-bridge 打分) |
112
- | "代码健康总览(想要一份报告)?" | `code-graph-mcp report` | —(CLI-only;汇总 summary+置信度 / hot / chokepoints / cycles / surprising / dead-code) |
113
-
114
- **dead-code 的 `ignore_paths`**:CLI 默认豁免 `["claude-plugin/", "benches/"]`
115
- (macro/shell 入口点);`--no-ignore` 关闭。MCP 端也接同名参数。
116
-
117
- ## 不要替代
118
-
119
- - 非代码文件(README/JSON/log) → 用内置 `Grep`
120
- - 代码里查常量/函数名/字符串首选 `code-graph-mcp grep "pattern" [path]`(每个命中带 containing function/module 上下文,结构化);只做纯文本匹配且不关心上下文时用内置 `Grep`
121
- - 即将编辑的具体文件 → 用 `Read`(`overview <file>` 看概览,`show SYMBOL` 看某符号)
122
-
123
- ## 工作流惯例
124
-
125
- 1. 起手 `project_map`(或 Bash 调 `code-graph-mcp map --compact`)看架构
126
- 2. `semantic_code_search` 默认带 `compact=true`,省 token
127
- 3. 展开节点:`get_ast_node node_id=N compact=true` 看签名 / 不带 compact 看全文
128
- 4. 改前评估影响:`get_ast_node symbol_name=X include_impact=true`(核心 7 内,首选)
129
- 或 Bash 调 `code-graph-mcp impact X`(独立进程;输出更细:风险等级 + 路由 + 文件计数)
130
- 5. 搜不到结果 → `code-graph-mcp health-check` 检查索引与 embedding 覆盖率
131
-
132
- 可用 prompts:`impact-analysis`、`understand-module`、`trace-request`
133
-
134
- ## CLI 速查(替 Bash)
135
-
136
- ```
137
- code-graph-mcp grep "pattern" [path] # ripgrep + AST 上下文(-t lang / -g glob / -c 计数 / -M 行宽)
138
- code-graph-mcp search "concept" # 纯 FTS5(要混合检索走 MCP semantic_code_search)
139
- code-graph-mcp ast-search "q" --type fn # 结构化筛选
140
- code-graph-mcp map # 项目架构
141
- code-graph-mcp overview src/mcp/ # 模块总览
142
- code-graph-mcp callgraph SYMBOL # 调用图
143
- code-graph-mcp impact SYMBOL # 影响面(--change-type ∈ signature|behavior|remove,默认 behavior)
144
- code-graph-mcp show SYMBOL # 节点详情
145
- code-graph-mcp refs SYMBOL --relation calls # --relation ∈ calls|imports|inherits|implements|references|exports|routes_to|all
146
- code-graph-mcp refs SYMBOL --min-confidence extracted # ∈ extracted|inferred|ambiguous;extracted=只看精确边(callgraph/impact/trace 同款)
147
- code-graph-mcp centrality # 架构咽喉(betweenness 桥节点;补 map 的 caller_count)
148
- code-graph-mcp cycles # 循环导入依赖(文件级 import 环 / SCC)
149
- code-graph-mcp surprising # 可疑跨模块耦合(低置信 + 跨模块 + sole-bridge 打分)
150
- code-graph-mcp report # 代码健康总览(汇总 hot/chokepoints/cycles/surprising/dead-code)
151
- code-graph-mcp dead-code [path] # 未使用代码(默认豁免 claude-plugin/)
152
- code-graph-mcp dead-code --ignore tmp/ --ignore scripts/bin/ # 自定义豁免前缀
153
- code-graph-mcp dead-code --no-ignore # 关掉默认豁免,看完整列表
154
- code-graph-mcp health-check # 索引健康
155
- ```
156
-
157
- 完整列表:`code-graph-mcp --help`。
158
-
159
- ## 质量门槛
160
-
161
- - `compact=true` 一般够用;要看完整代码再去掉
162
- - `impact` 在 `--change-type signature` 时返回最严格的破坏面
163
- - 索引陈旧 → SessionStart 自带 `ensureIndexFresh`;手动跑 `incremental-index`
164
-
165
- ## 卸载 / 回退
166
-
167
- - `code-graph-mcp unadopt` — 精确移除 CLAUDE.md 里的 managed 块 + `.claude/plugin_code_graph_mcp.md`(块移光后若 CLAUDE.md 只剩我们的内容则连文件一起删,否则保留你的正文);并清理任何遗留 memory-dir 制品。
168
- - `CODE_GRAPH_NO_AUTO_ADOPT=1`(`~/.claude/settings.json` env) — 阻止未来自动安装,不影响已安装状态。
169
- - `CODE_GRAPH_NO_TEMPLATE_REFRESH=1` — 锁定 CLAUDE.md 块 + 本文件不随插件升级刷新;允许手动编辑长久保留。
170
- - `CODE_GRAPH_VERBOSE_HOOKS=1`(v0.17.0+) — opt in 到 SessionStart `project_map` 注入(默认 OFF)。
171
- - `CODE_GRAPH_QUIET_HOOKS=1` — UserPromptSubmit context push 的 escape hatch(默认 ON);同时强制 SessionStart `project_map` quiet。
172
- - `CODE_GRAPH_QUIET_HOOKS=0` — 强制恢复 SessionStart `project_map` 注入(向后兼容路径)。
6
+ # code-graph-mcp: when to reach for it
7
+
8
+ Written by `code-graph-mcp adopt` to `.claude/plugin_code_graph_mcp.md`, next to the
9
+ CLAUDE.md block; `code-graph-mcp unadopt` removes both. The CLI runs from Bash with no
10
+ tool loading; the MCP tools return the same data but are deferred behind ToolSearch in
11
+ Claude Code.
12
+
13
+ ## Pick the command by the question
14
+
15
+ | Question | CLI | MCP tool |
16
+ |---|---|---|
17
+ | Who calls X / what does X call? | `code-graph-mcp callgraph X` | `get_call_graph` |
18
+ | What breaks if X's signature changes? | `code-graph-mcp impact X --change-type signature` | `get_ast_node include_impact=true` |
19
+ | Every use of X (rename / remove) | `code-graph-mcp refs X`, then `grep -w X` | `find_references` |
20
+ | Source and signature of X | `code-graph-mcp show X` | `get_ast_node` |
21
+ | What is in this dir or file? | `code-graph-mcp overview <path>` | `module_overview` |
22
+ | Find code by what it does | `code-graph-mcp search "words"` (full-text) | `semantic_code_search` (adds vector ranking once embeddings exist) |
23
+ | Text search, each hit with its enclosing fn | `code-graph-mcp grep "pat" [path]` | — |
24
+ | Tests that load these files | `code-graph-mcp affected <files>` | — |
25
+ | HTTP route to its handler chain | `code-graph-mcp trace "GET /api/x"` | `get_call_graph route_path="GET /api/x"` |
26
+ | Project layout | `code-graph-mcp map --compact` | `project_map` |
27
+ | Symbols by kind, return type or params | `code-graph-mcp ast-search "q" --type fn --returns T` | `ast_search` |
28
+
29
+ `grep` takes `-F -i -w -l -c`, `-t <lang>`, `-g <glob>`, `-A/-B/-C N` and exits like grep
30
+ (0 hits, 1 none, 2 error). Large answers: `callgraph` and `overview` accept `--budget N`.
31
+
32
+ ## What the graph cannot see
33
+
34
+ - A call it could not resolve leaves no edge: dynamic dispatch, reflection, callbacks
35
+ passed as values, unresolved imports. An empty caller list is not proof that nothing
36
+ calls X — check with grep before deleting or renaming. `impact` reports such a
37
+ function as `Risk: UNKNOWN`.
38
+ - `callgraph` and `impact` hide by-name `ambiguous` edges by default
39
+ (`--min-confidence ambiguous` shows them); `refs` shows every tier. Never narrow a
40
+ rename audit with `--min-confidence extracted`: that tier is same-file only and drops
41
+ every cross-file caller.
42
+ - A JS function held only by a variable (`const f = function () {}`) or a computed
43
+ member (`obj[k] = …`) is not a node. A call on a receiver with no written type
44
+ (hono's `c.json()`) binds by name and is often `ambiguous`, hidden by default.
45
+ - `affected` follows resolved imports; with unresolved ones (a tsconfig path alias, a
46
+ computed `require(name)`) its test list is a lower bound.
47
+ - `dead-code` is dependable for Rust. Elsewhere read it as candidates: interface
48
+ methods, re-exports and framework entry points show up in it.
49
+
50
+ ## Still use the plain tools
51
+
52
+ - Grep for literal strings, config and non-code files; Read the file you are about to edit.
53
+ - Answers look empty or stale: `code-graph-mcp health-check`, then
54
+ `code-graph-mcp incremental-index`.
55
+
56
+ ## Switches (`env` in `~/.claude/settings.json`)
57
+
58
+ - `CODE_GRAPH_QUIET_HOOKS=1` — hooks stop injecting context.
59
+ - `CODE_GRAPH_NO_TEMPLATE_REFRESH=1` — no notice when this file drifts from the shipped one.
60
+
61
+ All commands: `code-graph-mcp --help`. All switches: README, "Environment variables".
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdsrs/code-graph",
3
- "version": "0.163.0",
3
+ "version": "0.165.0",
4
4
  "description": "MCP server that indexes codebases into an AST knowledge graph with semantic search, call graph traversal, and HTTP route tracing",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -35,10 +35,10 @@
35
35
  "node": ">=16"
36
36
  },
37
37
  "optionalDependencies": {
38
- "@sdsrs/code-graph-linux-x64": "0.163.0",
39
- "@sdsrs/code-graph-linux-arm64": "0.163.0",
40
- "@sdsrs/code-graph-darwin-x64": "0.163.0",
41
- "@sdsrs/code-graph-darwin-arm64": "0.163.0",
42
- "@sdsrs/code-graph-win32-x64": "0.163.0"
38
+ "@sdsrs/code-graph-linux-x64": "0.165.0",
39
+ "@sdsrs/code-graph-linux-arm64": "0.165.0",
40
+ "@sdsrs/code-graph-darwin-x64": "0.165.0",
41
+ "@sdsrs/code-graph-darwin-arm64": "0.165.0",
42
+ "@sdsrs/code-graph-win32-x64": "0.165.0"
43
43
  }
44
44
  }