dbgraph 0.1.1 → 0.1.3
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.
- package/README.ZH-cn.md +143 -288
- package/README.md +155 -296
- package/package.json +2 -2
package/README.ZH-cn.md
CHANGED
|
@@ -1,64 +1,153 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
1
3
|
# DBGraph
|
|
2
4
|
|
|
5
|
+
### 数据库知识图谱 — 将数据库 schema 提取为知识图谱,通过 MCP 供 AI 代理使用
|
|
6
|
+
|
|
7
|
+
**零猜测 SQL 生成 · 亚毫秒级 schema 查询 · 100% 本地运行**
|
|
8
|
+
|
|
3
9
|
[](https://www.npmjs.com/package/dbgraph)
|
|
4
|
-
[](LICENSE)
|
|
11
|
+
[](https://nodejs.org/)
|
|
6
12
|
|
|
7
|
-
|
|
13
|
+
[](#支持引擎)
|
|
14
|
+
[](#支持引擎)
|
|
15
|
+
[](#支持引擎)
|
|
8
16
|
|
|
9
|
-
|
|
17
|
+
</div>
|
|
10
18
|
|
|
11
|
-
|
|
19
|
+
## 快速开始
|
|
12
20
|
|
|
21
|
+
```bash
|
|
22
|
+
# 零安装(推荐)
|
|
23
|
+
npx dbgraph
|
|
24
|
+
|
|
25
|
+
# 或全局安装
|
|
26
|
+
npm i -g dbgraph
|
|
13
27
|
```
|
|
14
|
-
传统流程:
|
|
15
|
-
LLM → 猜表名列名 → 写 SQL → 执行 → 报错 → 再猜 → 循环
|
|
16
28
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
29
|
+
### 初始化项目
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
cd your-project
|
|
33
|
+
npx dbgraph init -i # 一步完成初始化 + 索引
|
|
20
34
|
```
|
|
21
35
|
|
|
22
|
-
|
|
36
|
+
<sub>`dbgraph init` 创建 `.dbgraph/` 目录和默认 `dbgraph-db.json` 配置。加上 `-i`(`--index`)会立即内省数据库。编辑 `dbgraph-db.json` 配置数据库连接。</sub>
|
|
37
|
+
|
|
38
|
+
### 启动 MCP 服务器
|
|
23
39
|
|
|
24
40
|
```bash
|
|
25
|
-
|
|
26
|
-
|
|
41
|
+
npx dbgraph serve
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
AI 代理连接 MCP 后自动发现 `dbgraph_*` 工具,用于 schema 感知的 SQL 生成。
|
|
27
45
|
|
|
28
|
-
|
|
29
|
-
|
|
46
|
+
## MCP 配置
|
|
47
|
+
|
|
48
|
+
在 AI 代理的配置文件中添加 DBGraph 作为 MCP 服务器:
|
|
49
|
+
|
|
50
|
+
**opencode**(`~/.config/opencode/opencode.json`):
|
|
51
|
+
```json
|
|
52
|
+
{
|
|
53
|
+
"mcp": {
|
|
54
|
+
"dbgraph": {
|
|
55
|
+
"type": "local",
|
|
56
|
+
"command": ["npx", "dbgraph", "serve", "--auto-refresh"],
|
|
57
|
+
"enabled": true
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
30
61
|
```
|
|
31
62
|
|
|
32
|
-
|
|
63
|
+
**Cursor** → 设置 → MCP Servers → 添加:
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
"mcpServers": {
|
|
67
|
+
"dbgraph": {
|
|
68
|
+
"command": "npx",
|
|
69
|
+
"args": ["dbgraph", "serve", "--auto-refresh"]
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
33
74
|
|
|
34
|
-
|
|
75
|
+
**Claude Code**(`~/.claude/settings.json`):
|
|
76
|
+
```json
|
|
77
|
+
{
|
|
78
|
+
"mcpServers": {
|
|
79
|
+
"dbgraph": {
|
|
80
|
+
"command": "npx",
|
|
81
|
+
"args": ["dbgraph", "serve", "--auto-refresh"]
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
35
86
|
|
|
36
|
-
|
|
87
|
+
**Codex CLI**(`~/.codexclirc.json`):
|
|
88
|
+
```json
|
|
89
|
+
{
|
|
90
|
+
"mcpServers": {
|
|
91
|
+
"dbgraph": {
|
|
92
|
+
"type": "local",
|
|
93
|
+
"command": ["npx", "dbgraph", "serve", "--auto-refresh"]
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
37
98
|
|
|
38
|
-
|
|
99
|
+
## 为什么用 DBGraph?
|
|
39
100
|
|
|
40
|
-
|
|
101
|
+
LLM 写 SQL 出错的最大原因是**不知道库表结构**——表名靠猜、列名靠蒙、JOIN 条件靠碰运气。DBGraph 把数据库 schema(表、列、类型、外键、约束、索引)提取为**可搜索的知识图谱**存在 `.dbgraph/` 中。AI 代理通过 MCP 工具直接查询,无需数据库直连。
|
|
41
102
|
|
|
42
|
-
|
|
103
|
+
```
|
|
104
|
+
传统流程:
|
|
105
|
+
LLM → 猜名列名 → 写 SQL → 报错 → 再猜 → 循环
|
|
43
106
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
107
|
+
DBGraph 流程:
|
|
108
|
+
LLM → dbgraph_context("orders") → 拿到精确 schema
|
|
109
|
+
→ 写 SQL → 成功
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## CLI 命令
|
|
113
|
+
|
|
114
|
+
| 命令 | 说明 |
|
|
115
|
+
|---------|-------------|
|
|
116
|
+
| `init` | 初始化 `.dbgraph` 项目(`-i` 立即索引)|
|
|
117
|
+
| `index` | 运行数据库内省 |
|
|
118
|
+
| `serve` | 启动 MCP 服务器(`--auto-refresh` 自动检测 schema 变更)|
|
|
119
|
+
| `query` | 搜索表、列、视图、索引 |
|
|
120
|
+
| `context` | 查看完整表结构(写 SQL 前必调)|
|
|
121
|
+
| `trace` | 追踪外键关联路径 |
|
|
122
|
+
| `explore` | 批量查看多张表 schema |
|
|
123
|
+
| `sources` | 列出已配置的数据库源 |
|
|
124
|
+
| `status` | 知识图谱统计 |
|
|
125
|
+
| `test` | 测试数据库连接 |
|
|
126
|
+
| `config` | 查看或创建配置 |
|
|
47
127
|
|
|
48
|
-
|
|
49
|
-
dbgraph init -i
|
|
128
|
+
所有命令默认使用当前目录,也可指定目录:
|
|
50
129
|
|
|
51
|
-
|
|
52
|
-
dbgraph
|
|
130
|
+
```bash
|
|
131
|
+
npx dbgraph status # 当前目录
|
|
132
|
+
npx dbgraph status ./other-project # 其他项目
|
|
53
133
|
```
|
|
54
134
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
135
|
+
## MCP 工具
|
|
136
|
+
|
|
137
|
+
启动 `dbgraph serve` 后,AI 代理可调用:
|
|
138
|
+
|
|
139
|
+
| 工具 | 用途 |
|
|
140
|
+
|------|---------|
|
|
141
|
+
| `dbgraph_search` | 按名称搜索 schema 对象 |
|
|
142
|
+
| `dbgraph_context` | **完整表结构** — 列、类型、主键、外键、索引 |
|
|
143
|
+
| `dbgraph_trace` | 追踪外键关联路径(orders → users)|
|
|
144
|
+
| `dbgraph_explore` | 批量查看多张表 |
|
|
145
|
+
| `dbgraph_sources` | 列出所有数据库源 |
|
|
146
|
+
| `dbgraph_status` | 图谱健康状态与统计 |
|
|
58
147
|
|
|
59
|
-
|
|
148
|
+
## 配置说明
|
|
60
149
|
|
|
61
|
-
|
|
150
|
+
编辑项目根目录的 `dbgraph-db.json`:
|
|
62
151
|
|
|
63
152
|
```json
|
|
64
153
|
{
|
|
@@ -81,277 +170,43 @@ dbgraph init ./demo-project
|
|
|
81
170
|
}
|
|
82
171
|
```
|
|
83
172
|
|
|
84
|
-
### 3. 提取 Schema
|
|
85
|
-
|
|
86
|
-
```bash
|
|
87
|
-
dbgraph index
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
把数据库 schema 提取到知识图谱中。首次运行会连接所有配置的数据库,提取表/列/外键/索引/视图等信息。
|
|
91
|
-
|
|
92
|
-
### 4. 查询知识图谱
|
|
93
|
-
|
|
94
|
-
```bash
|
|
95
|
-
# 搜索表/列
|
|
96
|
-
dbgraph query orders
|
|
97
|
-
dbgraph query users --kind table
|
|
98
|
-
|
|
99
|
-
# 查看完整表结构
|
|
100
|
-
dbgraph context public.orders
|
|
101
|
-
|
|
102
|
-
# 查看状态
|
|
103
|
-
dbgraph status
|
|
104
|
-
|
|
105
|
-
# 列出数据源
|
|
106
|
-
dbgraph sources
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
## 配置说明
|
|
110
|
-
|
|
111
|
-
### `dbgraph-db.json`
|
|
112
|
-
|
|
113
173
|
| 字段 | 类型 | 必填 | 说明 |
|
|
114
|
-
|
|
115
|
-
| `alias` | string | 是 |
|
|
116
|
-
| `engine` | string | 是 |
|
|
117
|
-
| `host` | string | 否 |
|
|
174
|
+
|-------|------|----------|-------------|
|
|
175
|
+
| `alias` | string | 是 | 数据库别名(图谱中以 `db://@alias` 标识)|
|
|
176
|
+
| `engine` | string | 是 | `postgresql` / `mysql` / `mariadb` / `sqlite` |
|
|
177
|
+
| `host` | string | 否 | 主机地址 |
|
|
118
178
|
| `port` | number | 否 | 端口(默认根据引擎判断)|
|
|
119
|
-
| `database` | string |
|
|
179
|
+
| `database` | string | 视情况 | PostgreSQL/MySQL 必填 |
|
|
120
180
|
| `schemas` | string[] | 否 | 要提取的 schema 列表(默认全部)|
|
|
121
|
-
| `path` | string |
|
|
122
|
-
| `auth` | string | 否 |
|
|
123
|
-
| `ssl` | boolean | 否 |
|
|
124
|
-
|
|
125
|
-
## CLI 命令参考
|
|
126
|
-
|
|
127
|
-
所有命令格式:`dbgraph <命令> [选项] [目录]`
|
|
128
|
-
|
|
129
|
-
不填目录时默认为当前目录。
|
|
130
|
-
|
|
131
|
-
| 命令 | 说明 |
|
|
132
|
-
|------|------|
|
|
133
|
-
| `init` | 初始化 .dbgraph 项目 + 配置 |
|
|
134
|
-
| `index` | 运行数据库内省,提取 schema |
|
|
135
|
-
| `serve` | 启动 MCP 服务器(供 AI Agent 使用)|
|
|
136
|
-
| `query` | 搜索表/列/视图 |
|
|
137
|
-
| `context` | 查看完整表结构 |
|
|
138
|
-
| `status` | 知识图谱状态统计 |
|
|
139
|
-
| `sources` | 列出数据源 |
|
|
140
|
-
| `test` | 测试数据库连接 |
|
|
141
|
-
| `config` | 查看/创建配置 |
|
|
142
|
-
|
|
143
|
-
### `init`
|
|
144
|
-
|
|
145
|
-
```bash
|
|
146
|
-
# 初始化当前目录
|
|
147
|
-
dbgraph init
|
|
148
|
-
|
|
149
|
-
# 初始化并立即索引
|
|
150
|
-
dbgraph init -i
|
|
151
|
-
|
|
152
|
-
# 初始化指定目录
|
|
153
|
-
dbgraph init ./my-project
|
|
154
|
-
|
|
155
|
-
# 指定配置文件路径
|
|
156
|
-
dbgraph init ./my-project -c ./my-project/custom-config.json
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
### `index`
|
|
160
|
-
|
|
161
|
-
```bash
|
|
162
|
-
# 索引当前目录配置的数据库
|
|
163
|
-
dbgraph index
|
|
164
|
-
|
|
165
|
-
# 指定配置文件
|
|
166
|
-
dbgraph index ./my-project -c ./my-project/custom-config.json
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
### `serve`(MCP 模式)
|
|
170
|
-
|
|
171
|
-
```bash
|
|
172
|
-
# 启动 MCP stdio 服务器(当前目录)
|
|
173
|
-
dbgraph serve
|
|
174
|
-
|
|
175
|
-
# 指定项目目录
|
|
176
|
-
dbgraph serve ./my-project
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
AI Agent 连接到 MCP 后自动发现 `dbgraph_*` 工具。
|
|
180
|
-
|
|
181
|
-
### `query`
|
|
182
|
-
|
|
183
|
-
```bash
|
|
184
|
-
# 搜索
|
|
185
|
-
dbgraph query orders
|
|
186
|
-
|
|
187
|
-
# 按类型过滤
|
|
188
|
-
dbgraph query orders --kind table
|
|
189
|
-
dbgraph query amount --kind column
|
|
190
|
-
|
|
191
|
-
# JSON 格式输出
|
|
192
|
-
dbgraph query orders --json
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
### `context`
|
|
196
|
-
|
|
197
|
-
```bash
|
|
198
|
-
# 查看表结构
|
|
199
|
-
dbgraph context orders
|
|
200
|
-
dbgraph context public.orders
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
输出示例:
|
|
204
|
-
```
|
|
205
|
-
## Table: public.orders
|
|
206
|
-
|
|
207
|
-
Database: prod (postgresql)
|
|
208
|
-
|
|
209
|
-
| Column | Type | Nullable | Default | PK | FK |
|
|
210
|
-
|-------------|-------------|----------|----------|----|-----------|
|
|
211
|
-
| id | bigint | NO | | PK | |
|
|
212
|
-
| user_id | integer | NO | | | users.id |
|
|
213
|
-
| status | varchar(20) | NO | pending | | |
|
|
214
|
-
| total_amount| numeric(10,2)| YES | 0.00 | | |
|
|
215
|
-
| created_at | timestamp | NO | now() | | |
|
|
216
|
-
|
|
217
|
-
Indexes:
|
|
218
|
-
- idx_orders_status on (status)
|
|
219
|
-
- pk_orders PRIMARY KEY using btree (id)
|
|
220
|
-
|
|
221
|
-
Foreign Keys:
|
|
222
|
-
- fk_orders_user → users(id) ON DELETE CASCADE
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
## MCP 工具
|
|
226
|
-
|
|
227
|
-
启动 `dbgraph serve` 后,AI Agent 可以调用以下工具:
|
|
228
|
-
|
|
229
|
-
| 工具 | 用途 | 推荐时机 |
|
|
230
|
-
|------|------|---------|
|
|
231
|
-
| `dbgraph_search` | 搜索表/列/视图/索引 | 不确定名字时 |
|
|
232
|
-
| `dbgraph_context` | **主要入口** —— 返回完整表结构 + 列 + FK + 索引 | 写 SQL 前 |
|
|
233
|
-
| `dbgraph_trace` | 追踪 FK 关联路径(orders→users) | 需要写 JOIN 时 |
|
|
234
|
-
| `dbgraph_explore` | 一次性探索多张相关表 | 复杂查询涉及多表时 |
|
|
235
|
-
| `dbgraph_sources` | 列出所有已配置的数据库源 | 了解有哪些库可用 |
|
|
236
|
-
| `dbgraph_status` | 知识图谱统计 | 检查是否已索引 |
|
|
237
|
-
|
|
238
|
-
### 推荐工具调用策略
|
|
239
|
-
|
|
240
|
-
```
|
|
241
|
-
写 SQL 前的标准流程:
|
|
242
|
-
1. dbgraph_search("order") → 找到 order 相关表
|
|
243
|
-
2. dbgraph_context("public.orders") → 获取完整 schema
|
|
244
|
-
3. dbgraph_context("public.users") → 获取关联表 schema
|
|
245
|
-
4. dbgraph_trace("orders", "users") → 验证 FK 关联
|
|
246
|
-
5. LLM 写出精确 SQL
|
|
247
|
-
```
|
|
181
|
+
| `path` | string | 视情况 | SQLite 必填 |
|
|
182
|
+
| `auth` | string | 否 | `env:VAR_NAME` 或 `~/.pgpass` |
|
|
183
|
+
| `ssl` | boolean | 否 | 启用 SSL |
|
|
248
184
|
|
|
249
185
|
## 支持引擎
|
|
250
186
|
|
|
251
|
-
| 引擎 | 状态 |
|
|
252
|
-
|
|
253
|
-
| PostgreSQL | ✅ 完整支持 |
|
|
254
|
-
| MySQL / MariaDB | ✅ 完整支持 |
|
|
255
|
-
| SQLite | ✅ 完整支持 |
|
|
256
|
-
| SQL Server | 🔜 计划中 |
|
|
257
|
-
| Oracle | 🔜 计划中 |
|
|
258
|
-
| MongoDB | 🔜 计划中 | |
|
|
259
|
-
|
|
260
|
-
## 项目结构
|
|
261
|
-
|
|
262
|
-
```
|
|
263
|
-
dbgraph/
|
|
264
|
-
├── src/
|
|
265
|
-
│ ├── index.ts # DBGraph 主类
|
|
266
|
-
│ ├── types.ts # 所有类型 (Node, Edge, TableSchema...)
|
|
267
|
-
│ ├── config.ts # dbgraph-db.json 配置管理
|
|
268
|
-
│ ├── directory.ts # .dbgraph 目录管理
|
|
269
|
-
│ ├── errors.ts # 错误类型
|
|
270
|
-
│ ├── utils.ts # 工具函数
|
|
271
|
-
│ │
|
|
272
|
-
│ ├── db/ # SQLite 存储层
|
|
273
|
-
│ │ ├── schema.sql # 表结构 (nodes/edges FTS5)
|
|
274
|
-
│ │ ├── sqlite-adapter.ts # node:sqlite 适配
|
|
275
|
-
│ │ ├── migrations.ts # 版本迁移
|
|
276
|
-
│ │ ├── queries.ts # CRUD + FTS5 搜索 + 评分 (LRU缓存)
|
|
277
|
-
│ │ └── index.ts # 连接管理
|
|
278
|
-
│ │
|
|
279
|
-
│ ├── graph/
|
|
280
|
-
│ │ └── traversal.ts # BFS/DFS/寻路/影响半径
|
|
281
|
-
│ │
|
|
282
|
-
│ ├── context/
|
|
283
|
-
│ │ ├── index.ts # 表结构组装
|
|
284
|
-
│ │ └── formatter.ts # Markdown 输出
|
|
285
|
-
│ │
|
|
286
|
-
│ ├── introspect/ # 数据库内省
|
|
287
|
-
│ │ ├── base.ts # 基类 + Node/Edge 工厂
|
|
288
|
-
│ │ ├── index.ts # 工厂方法
|
|
289
|
-
│ │ ├── connection.ts # 连接管理
|
|
290
|
-
│ │ ├── postgres.ts # PostgreSQL
|
|
291
|
-
│ │ ├── mysql.ts # MySQL
|
|
292
|
-
│ │ └── sqlite.ts # SQLite
|
|
293
|
-
│ │
|
|
294
|
-
│ ├── mcp/ # MCP 服务器
|
|
295
|
-
│ │ ├── transport.ts # JSON-RPC 传输
|
|
296
|
-
│ │ ├── session.ts # 会话管理
|
|
297
|
-
│ │ ├── engine.ts # 引擎 + 生命周期
|
|
298
|
-
│ │ ├── tools.ts # 6 个 dbgraph_* 工具
|
|
299
|
-
│ │ ├── server-instructions.ts # LLM 指引
|
|
300
|
-
│ │ └── index.ts # MCPServer
|
|
301
|
-
│ │
|
|
302
|
-
│ └── bin/
|
|
303
|
-
│ └── dbgraph.ts # CLI
|
|
304
|
-
```
|
|
187
|
+
| 引擎 | 状态 |
|
|
188
|
+
|--------|------|
|
|
189
|
+
| PostgreSQL | ✅ 完整支持 |
|
|
190
|
+
| MySQL / MariaDB | ✅ 完整支持 |
|
|
191
|
+
| SQLite | ✅ 完整支持 |
|
|
192
|
+
| SQL Server | 🔜 计划中 |
|
|
193
|
+
| Oracle | 🔜 计划中 |
|
|
305
194
|
|
|
306
195
|
## 开发
|
|
307
196
|
|
|
308
197
|
```bash
|
|
309
|
-
|
|
198
|
+
git clone https://github.com/ZhangYaoSong/dbgraph.git
|
|
199
|
+
cd dbgraph
|
|
310
200
|
npm install
|
|
311
|
-
|
|
312
|
-
# 构建
|
|
313
201
|
npm run build
|
|
314
|
-
|
|
315
|
-
#
|
|
316
|
-
npm run dev
|
|
317
|
-
|
|
318
|
-
# 运行帮助
|
|
319
|
-
npm run cli -- --help
|
|
202
|
+
npm run cli -- init -i # 初始化当前目录 + 索引
|
|
203
|
+
npm run cli -- serve # 从源码启动 MCP 服务器
|
|
320
204
|
```
|
|
321
205
|
|
|
322
|
-
### 添加新数据库引擎
|
|
323
|
-
|
|
324
|
-
在 `src/introspect/` 下创建新文件(如 `mssql.ts`),实现 `BaseIntrospector` 抽象类:
|
|
325
|
-
|
|
326
|
-
```typescript
|
|
327
|
-
import { BaseIntrospector } from './base';
|
|
328
|
-
|
|
329
|
-
export class MSSQLIntrospector extends BaseIntrospector {
|
|
330
|
-
async extractAll(): Promise<IntrospectResult> {
|
|
331
|
-
// 1. 连接数据库
|
|
332
|
-
// 2. 查询 information_schema
|
|
333
|
-
// 3. 调用 this.makeNode() / this.makeEdge()
|
|
334
|
-
// 4. 返回 IntrospectResult
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
```
|
|
338
|
-
|
|
339
|
-
然后在 `src/introspect/index.ts` 中注册:
|
|
340
|
-
|
|
341
|
-
```typescript
|
|
342
|
-
case 'mssql':
|
|
343
|
-
return new MSSQLIntrospector(config);
|
|
344
|
-
```
|
|
345
|
-
|
|
346
|
-
## 参考
|
|
347
|
-
|
|
348
|
-
- **[AGENTS.md](AGENTS.md)** — OpenCode AI Agent 的项目指引,包含开发命令速查、架构要点和 CodeGraph 使用说明。
|
|
349
|
-
- **CodeGraph** — 本项目已初始化 CodeGraph 索引(`.codegraph/`),Agent 可优先使用 `codegraph_*` 工具进行结构查询,速度远超 grep。
|
|
350
|
-
|
|
351
206
|
## 鸣谢
|
|
352
207
|
|
|
353
|
-
|
|
208
|
+
受 [CodeGraph](https://github.com/colbymchenry/codegraph) 启发——一个优秀的代码知识图谱工具,本项目将其思路应用于数据库 schema 领域。
|
|
354
209
|
|
|
355
|
-
##
|
|
210
|
+
## 许可证
|
|
356
211
|
|
|
357
212
|
MIT
|
package/README.md
CHANGED
|
@@ -1,353 +1,212 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[](LICENSE)
|
|
5
|
-
[](https://nodejs.org)
|
|
3
|
+
# DBGraph
|
|
6
4
|
|
|
7
|
-
Database
|
|
5
|
+
### Database Knowledge Graph — Introspect schemas into a searchable graph, expose over MCP for AI agents
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
**Zero-guess SQL generation · Sub-millisecond schema lookups · 100% local**
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
[](https://www.npmjs.com/package/dbgraph)
|
|
10
|
+
[](LICENSE)
|
|
11
|
+
[](https://nodejs.org/)
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
[](#supported-engines)
|
|
14
|
+
[](#supported-engines)
|
|
15
|
+
[](#supported-engines)
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
LLM → dbgraph_context("orders") → get exact schema
|
|
19
|
-
→ write SQL → dbgraph_execute → success
|
|
20
|
-
```
|
|
17
|
+
</div>
|
|
21
18
|
|
|
22
|
-
##
|
|
19
|
+
## Get Started
|
|
23
20
|
|
|
24
21
|
```bash
|
|
25
|
-
#
|
|
26
|
-
|
|
22
|
+
# Zero-install (recommended)
|
|
23
|
+
npx dbgraph
|
|
27
24
|
|
|
28
|
-
# Or
|
|
29
|
-
|
|
25
|
+
# Or install globally
|
|
26
|
+
npm i -g dbgraph
|
|
30
27
|
```
|
|
31
28
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
All commands default to the current directory. Pass a directory path to target another project.
|
|
39
|
-
|
|
40
|
-
### 1. Initialize a project
|
|
41
|
-
|
|
42
|
-
```bash
|
|
43
|
-
# Initialize current directory
|
|
44
|
-
dbgraph init
|
|
45
|
-
|
|
46
|
-
# Or initialize + index in one step
|
|
47
|
-
dbgraph init -i
|
|
48
|
-
|
|
49
|
-
# Or target a specific directory
|
|
50
|
-
dbgraph init ./demo-project
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
This creates:
|
|
54
|
-
- `.dbgraph/` — knowledge graph data directory
|
|
55
|
-
- `dbgraph-db.json` — database connection config (default template)
|
|
56
|
-
|
|
57
|
-
### 2. Configure database connections
|
|
58
|
-
|
|
59
|
-
Edit `dbgraph-db.json` with your database info:
|
|
60
|
-
|
|
61
|
-
```json
|
|
62
|
-
{
|
|
63
|
-
"databases": [
|
|
64
|
-
{
|
|
65
|
-
"alias": "prod",
|
|
66
|
-
"engine": "postgresql",
|
|
67
|
-
"host": "localhost",
|
|
68
|
-
"port": 5432,
|
|
69
|
-
"database": "ecommerce",
|
|
70
|
-
"schemas": ["public"],
|
|
71
|
-
"auth": "env:DB_PASSWORD"
|
|
72
|
-
},
|
|
73
|
-
{
|
|
74
|
-
"alias": "local",
|
|
75
|
-
"engine": "sqlite",
|
|
76
|
-
"path": "./dev.db"
|
|
77
|
-
}
|
|
78
|
-
]
|
|
79
|
-
}
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
### 3. Extract schema
|
|
83
|
-
|
|
84
|
-
```bash
|
|
85
|
-
dbgraph index
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
Introspects all configured databases and stores tables, columns, foreign keys, indexes, and views into the knowledge graph.
|
|
89
|
-
|
|
90
|
-
### 4. Query the knowledge graph
|
|
91
|
-
|
|
92
|
-
```bash
|
|
93
|
-
# Search tables/columns
|
|
94
|
-
dbgraph query orders
|
|
95
|
-
dbgraph query users --kind table
|
|
96
|
-
|
|
97
|
-
# View full table structure
|
|
98
|
-
dbgraph context public.orders
|
|
99
|
-
|
|
100
|
-
# Check status
|
|
101
|
-
dbgraph status
|
|
102
|
-
|
|
103
|
-
# List data sources
|
|
104
|
-
dbgraph sources
|
|
29
|
+
### Initialize a project
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
cd your-project
|
|
33
|
+
npx dbgraph init -i # init + index in one step
|
|
105
34
|
```
|
|
106
35
|
|
|
107
|
-
|
|
36
|
+
<sub>`dbgraph init` creates `.dbgraph/` and a default `dbgraph-db.json` config. Adding `-i` (`--index`) also introspects your databases immediately. Edit `dbgraph-db.json` first to configure your connections.</sub>
|
|
108
37
|
|
|
109
|
-
###
|
|
38
|
+
### Start the MCP server
|
|
110
39
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
| `engine` | string | yes | Database engine: `postgresql` / `mysql` / `mariadb` / `sqlite` |
|
|
115
|
-
| `host` | string | no | Host address (not needed for SQLite) |
|
|
116
|
-
| `port` | number | no | Port (default depends on engine) |
|
|
117
|
-
| `database` | string | yes (non-SQLite) | Database name |
|
|
118
|
-
| `schemas` | string[] | no | Schemas to introspect (default: all) |
|
|
119
|
-
| `path` | string | yes (SQLite) | SQLite file path |
|
|
120
|
-
| `auth` | string | no | Authentication, e.g. `env:DB_PASSWORD` or `~/.pgpass` |
|
|
121
|
-
| `ssl` | boolean | no | Enable SSL connection |
|
|
40
|
+
```bash
|
|
41
|
+
npx dbgraph serve
|
|
42
|
+
```
|
|
122
43
|
|
|
123
|
-
|
|
44
|
+
AI agents connected to MCP automatically discover `dbgraph_*` tools for schema-aware SQL generation.
|
|
124
45
|
|
|
125
|
-
|
|
46
|
+
## MCP Configuration
|
|
126
47
|
|
|
127
|
-
|
|
128
|
-
|---------|-------------|
|
|
129
|
-
| `init` | Initialize .dbgraph project + config |
|
|
130
|
-
| `index` | Run database introspection |
|
|
131
|
-
| `serve` | Start MCP server (for AI agents) |
|
|
132
|
-
| `query` | Search tables/columns/views |
|
|
133
|
-
| `context` | View full table structure |
|
|
134
|
-
| `status` | Knowledge graph statistics |
|
|
135
|
-
| `sources` | List data sources |
|
|
136
|
-
| `test` | Test database connections |
|
|
137
|
-
| `config` | View/create configuration |
|
|
138
|
-
|
|
139
|
-
### `init`
|
|
140
|
-
|
|
141
|
-
```bash
|
|
142
|
-
# Initialize current directory
|
|
143
|
-
dbgraph init
|
|
144
|
-
|
|
145
|
-
# Init and index immediately
|
|
146
|
-
dbgraph init -i
|
|
147
|
-
|
|
148
|
-
# Initialize a specific directory
|
|
149
|
-
dbgraph init ./my-project
|
|
150
|
-
|
|
151
|
-
# Specify config file path
|
|
152
|
-
dbgraph init ./my-project -c ./my-project/custom-config.json
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
### `index`
|
|
156
|
-
|
|
157
|
-
```bash
|
|
158
|
-
# Index configured databases (current dir)
|
|
159
|
-
dbgraph index
|
|
160
|
-
|
|
161
|
-
# Use a specific config file
|
|
162
|
-
dbgraph index ./my-project -c ./my-project/custom-config.json
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
### `serve` (MCP mode)
|
|
166
|
-
|
|
167
|
-
```bash
|
|
168
|
-
# Start MCP stdio server (current dir)
|
|
169
|
-
dbgraph serve
|
|
170
|
-
|
|
171
|
-
# Start for a specific project directory
|
|
172
|
-
dbgraph serve ./my-project
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
AI agents automatically discover `dbgraph_*` tools upon connecting to MCP.
|
|
48
|
+
Add DBGraph as an MCP server in your agent's config:
|
|
176
49
|
|
|
177
|
-
|
|
50
|
+
**opencode** (`~/.config/opencode/opencode.json`):
|
|
51
|
+
```json
|
|
52
|
+
{
|
|
53
|
+
"mcp": {
|
|
54
|
+
"dbgraph": {
|
|
55
|
+
"type": "local",
|
|
56
|
+
"command": ["npx", "dbgraph", "serve", "--auto-refresh"],
|
|
57
|
+
"enabled": true
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
178
62
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
63
|
+
**Cursor** → Settings → MCP Servers → Add:
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
"mcpServers": {
|
|
67
|
+
"dbgraph": {
|
|
68
|
+
"command": "npx",
|
|
69
|
+
"args": ["dbgraph", "serve", "--auto-refresh"]
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
182
74
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
75
|
+
**Claude Code** (`~/.claude/settings.json`):
|
|
76
|
+
```json
|
|
77
|
+
{
|
|
78
|
+
"mcpServers": {
|
|
79
|
+
"dbgraph": {
|
|
80
|
+
"command": "npx",
|
|
81
|
+
"args": ["dbgraph", "serve", "--auto-refresh"]
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
186
86
|
|
|
187
|
-
|
|
188
|
-
|
|
87
|
+
**Codex CLI** (`~/.codexclirc.json`):
|
|
88
|
+
```json
|
|
89
|
+
{
|
|
90
|
+
"mcpServers": {
|
|
91
|
+
"dbgraph": {
|
|
92
|
+
"type": "local",
|
|
93
|
+
"command": ["npx", "dbgraph", "serve", "--auto-refresh"]
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
189
97
|
```
|
|
190
98
|
|
|
191
|
-
|
|
99
|
+
## Why DBGraph?
|
|
100
|
+
|
|
101
|
+
LLMs write wrong SQL because they **don't know your schema** — guessing table names, column names, and JOIN conditions. DBGraph extracts your complete database schema (tables, columns, types, foreign keys, constraints, indexes) into a **searchable knowledge graph** stored in `.dbgraph/`. AI agents query it via MCP tools directly — no live database connection needed.
|
|
192
102
|
|
|
193
|
-
```bash
|
|
194
|
-
# View table structure
|
|
195
|
-
dbgraph context orders ./my-project
|
|
196
|
-
dbgraph context public.orders ./my-project
|
|
197
103
|
```
|
|
104
|
+
Without DBGraph:
|
|
105
|
+
LLM → guess names → write SQL → run → error → guess again → loop
|
|
198
106
|
|
|
199
|
-
|
|
107
|
+
With DBGraph:
|
|
108
|
+
LLM → dbgraph_context("orders") → get exact schema
|
|
109
|
+
→ write SQL → success
|
|
200
110
|
```
|
|
201
|
-
## Table: public.orders
|
|
202
111
|
|
|
203
|
-
|
|
112
|
+
## CLI Reference
|
|
204
113
|
|
|
205
|
-
|
|
|
206
|
-
|
|
207
|
-
|
|
|
208
|
-
|
|
|
209
|
-
|
|
|
210
|
-
|
|
|
211
|
-
|
|
|
114
|
+
| Command | Description |
|
|
115
|
+
|---------|-------------|
|
|
116
|
+
| `init` | Initialize `.dbgraph` project (`-i` to index immediately) |
|
|
117
|
+
| `index` | Run database introspection |
|
|
118
|
+
| `serve` | Start MCP server (use `--auto-refresh` to watch for schema changes) |
|
|
119
|
+
| `query` | Search tables, columns, views, indexes |
|
|
120
|
+
| `context` | View full table schema (call before writing SQL) |
|
|
121
|
+
| `trace` | Trace foreign key join paths between tables |
|
|
122
|
+
| `explore` | Explore multiple related tables at once |
|
|
123
|
+
| `sources` | List configured database sources |
|
|
124
|
+
| `status` | Knowledge graph statistics |
|
|
125
|
+
| `test` | Test database connections |
|
|
126
|
+
| `config` | View or create configuration |
|
|
212
127
|
|
|
213
|
-
|
|
214
|
-
- idx_orders_status on (status)
|
|
215
|
-
- pk_orders PRIMARY KEY using btree (id)
|
|
128
|
+
All commands default to the current directory. Pass a directory path to target another project:
|
|
216
129
|
|
|
217
|
-
|
|
218
|
-
|
|
130
|
+
```bash
|
|
131
|
+
npx dbgraph status # current directory
|
|
132
|
+
npx dbgraph status ./other-project # another project
|
|
219
133
|
```
|
|
220
134
|
|
|
221
135
|
## MCP Tools
|
|
222
136
|
|
|
223
|
-
After starting `dbgraph serve`, AI agents can call
|
|
137
|
+
After starting `dbgraph serve`, AI agents can call:
|
|
224
138
|
|
|
225
|
-
| Tool | Purpose |
|
|
226
|
-
|
|
227
|
-
| `dbgraph_search` | Search
|
|
228
|
-
| `dbgraph_context` | **
|
|
229
|
-
| `dbgraph_trace` | Trace FK join paths (orders→users) |
|
|
230
|
-
| `dbgraph_explore` |
|
|
231
|
-
| `dbgraph_sources` | List all
|
|
232
|
-
| `dbgraph_status` |
|
|
139
|
+
| Tool | Purpose |
|
|
140
|
+
|------|---------|
|
|
141
|
+
| `dbgraph_search` | Search schema objects by name |
|
|
142
|
+
| `dbgraph_context` | **Full table schema** — columns, types, PKs, FKs, indexes |
|
|
143
|
+
| `dbgraph_trace` | Trace FK join paths (orders → users) |
|
|
144
|
+
| `dbgraph_explore` | Batch schema for multiple tables |
|
|
145
|
+
| `dbgraph_sources` | List all database sources |
|
|
146
|
+
| `dbgraph_status` | Graph health and statistics |
|
|
233
147
|
|
|
234
|
-
|
|
148
|
+
## Configuration
|
|
235
149
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
150
|
+
Edit `dbgraph-db.json` in your project root:
|
|
151
|
+
|
|
152
|
+
```json
|
|
153
|
+
{
|
|
154
|
+
"databases": [
|
|
155
|
+
{
|
|
156
|
+
"alias": "prod",
|
|
157
|
+
"engine": "postgresql",
|
|
158
|
+
"host": "localhost",
|
|
159
|
+
"port": 5432,
|
|
160
|
+
"database": "ecommerce",
|
|
161
|
+
"schemas": ["public"],
|
|
162
|
+
"auth": "env:DB_PASSWORD"
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
"alias": "local",
|
|
166
|
+
"engine": "sqlite",
|
|
167
|
+
"path": "./dev.db"
|
|
168
|
+
}
|
|
169
|
+
]
|
|
170
|
+
}
|
|
243
171
|
```
|
|
244
172
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
|
248
|
-
|
|
249
|
-
|
|
|
250
|
-
|
|
|
251
|
-
|
|
|
252
|
-
|
|
|
253
|
-
|
|
|
254
|
-
|
|
|
173
|
+
| Field | Type | Required | Description |
|
|
174
|
+
|-------|------|----------|-------------|
|
|
175
|
+
| `alias` | string | yes | Database alias (`db://@alias` in graph) |
|
|
176
|
+
| `engine` | string | yes | `postgresql` / `mysql` / `mariadb` / `sqlite` |
|
|
177
|
+
| `host` | string | no | Host address |
|
|
178
|
+
| `port` | number | no | Port (default depends on engine) |
|
|
179
|
+
| `database` | string | depends | Required for PostgreSQL/MySQL |
|
|
180
|
+
| `schemas` | string[] | no | Schemas to introspect (default: all) |
|
|
181
|
+
| `path` | string | depends | Required for SQLite |
|
|
182
|
+
| `auth` | string | no | `env:VAR_NAME` or `~/.pgpass` |
|
|
183
|
+
| `ssl` | boolean | no | Enable SSL |
|
|
255
184
|
|
|
256
|
-
##
|
|
185
|
+
## Supported Engines
|
|
257
186
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
│ ├── errors.ts # Error types
|
|
266
|
-
│ ├── utils.ts # Utility functions
|
|
267
|
-
│ │
|
|
268
|
-
│ ├── db/ # SQLite storage layer
|
|
269
|
-
│ │ ├── schema.sql # Table schema (nodes/edges FTS5)
|
|
270
|
-
│ │ ├── sqlite-adapter.ts # node:sqlite adapter
|
|
271
|
-
│ │ ├── migrations.ts # Version migrations
|
|
272
|
-
│ │ ├── queries.ts # CRUD + FTS5 search + scoring (LRU cache)
|
|
273
|
-
│ │ └── index.ts # Connection management
|
|
274
|
-
│ │
|
|
275
|
-
│ ├── graph/
|
|
276
|
-
│ │ └── traversal.ts # BFS/DFS/pathfinding/impact radius
|
|
277
|
-
│ │
|
|
278
|
-
│ ├── context/
|
|
279
|
-
│ │ ├── index.ts # Table context assembly
|
|
280
|
-
│ │ └── formatter.ts # Markdown output
|
|
281
|
-
│ │
|
|
282
|
-
│ ├── introspect/ # Database introspection
|
|
283
|
-
│ │ ├── base.ts # Base class + Node/Edge factory
|
|
284
|
-
│ │ ├── index.ts # Factory method
|
|
285
|
-
│ │ ├── connection.ts # Connection management
|
|
286
|
-
│ │ ├── postgres.ts # PostgreSQL
|
|
287
|
-
│ │ ├── mysql.ts # MySQL
|
|
288
|
-
│ │ └── sqlite.ts # SQLite
|
|
289
|
-
│ │
|
|
290
|
-
│ ├── mcp/ # MCP server
|
|
291
|
-
│ │ ├── transport.ts # JSON-RPC transport
|
|
292
|
-
│ │ ├── session.ts # Session management
|
|
293
|
-
│ │ ├── engine.ts # Engine + lifecycle
|
|
294
|
-
│ │ ├── tools.ts # 6 dbgraph_* tools
|
|
295
|
-
│ │ ├── server-instructions.ts # LLM instructions
|
|
296
|
-
│ │ └── index.ts # MCPServer
|
|
297
|
-
│ │
|
|
298
|
-
│ └── bin/
|
|
299
|
-
│ └── dbgraph.ts # CLI
|
|
300
|
-
```
|
|
187
|
+
| Engine | Status |
|
|
188
|
+
|--------|--------|
|
|
189
|
+
| PostgreSQL | ✅ Full support |
|
|
190
|
+
| MySQL / MariaDB | ✅ Full support |
|
|
191
|
+
| SQLite | ✅ Full support |
|
|
192
|
+
| SQL Server | 🔜 Planned |
|
|
193
|
+
| Oracle | 🔜 Planned |
|
|
301
194
|
|
|
302
195
|
## Development
|
|
303
196
|
|
|
304
197
|
```bash
|
|
305
|
-
|
|
198
|
+
git clone https://github.com/ZhangYaoSong/dbgraph.git
|
|
199
|
+
cd dbgraph
|
|
306
200
|
npm install
|
|
307
|
-
|
|
308
|
-
# Build
|
|
309
201
|
npm run build
|
|
310
|
-
|
|
311
|
-
#
|
|
312
|
-
npm run dev
|
|
313
|
-
|
|
314
|
-
# Quick build + run
|
|
315
|
-
npm run cli -- status ./my-project
|
|
202
|
+
npm run cli -- init -i # init current dir + index
|
|
203
|
+
npm run cli -- serve # start MCP server from source
|
|
316
204
|
```
|
|
317
205
|
|
|
318
|
-
### Adding a new database engine
|
|
319
|
-
|
|
320
|
-
Create a new file in `src/introspect/` (e.g. `mssql.ts`) implementing `BaseIntrospector`:
|
|
321
|
-
|
|
322
|
-
```typescript
|
|
323
|
-
import { BaseIntrospector } from './base';
|
|
324
|
-
|
|
325
|
-
export class MSSQLIntrospector extends BaseIntrospector {
|
|
326
|
-
async extractAll(): Promise<IntrospectResult> {
|
|
327
|
-
// 1. Connect to database
|
|
328
|
-
// 2. Query information_schema
|
|
329
|
-
// 3. Call this.makeNode() / this.makeEdge()
|
|
330
|
-
// 4. Return IntrospectResult
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
```
|
|
334
|
-
|
|
335
|
-
Then register it in `src/introspect/index.ts`:
|
|
336
|
-
|
|
337
|
-
```typescript
|
|
338
|
-
case 'mssql':
|
|
339
|
-
return new MSSQLIntrospector(config);
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
## References
|
|
343
|
-
|
|
344
|
-
- **[AGENTS.md](AGENTS.md)** — OpenCode AI Agent project guide with dev commands and architecture overview
|
|
345
|
-
- **CodeGraph** — This project uses CodeGraph indexing (`.codegraph/`) for fast structural queries
|
|
346
|
-
|
|
347
206
|
## Acknowledgments
|
|
348
207
|
|
|
349
|
-
|
|
208
|
+
Inspired by [CodeGraph](https://github.com/colbymchenry/codegraph) — an excellent code knowledge graph tool that this project adapts to the database schema domain.
|
|
350
209
|
|
|
351
210
|
## License
|
|
352
211
|
|
|
353
|
-
MIT
|
|
212
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dbgraph",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Database knowledge graph for LLM-powered SQL generation. Introspect database schemas into a local-first knowledge graph, exposed over MCP.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"bin": {
|
|
8
|
-
"dbgraph": "
|
|
8
|
+
"dbgraph": "dist/bin/dbgraph.js"
|
|
9
9
|
},
|
|
10
10
|
"files": [
|
|
11
11
|
"dist",
|