universal-db-mcp 0.1.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.
package/EXAMPLES.md ADDED
@@ -0,0 +1,362 @@
1
+ # 使用示例
2
+
3
+ 本文档提供 MCP 数据库万能连接器的详细使用示例。
4
+
5
+ ## 📋 目录
6
+
7
+ - [MySQL 使用示例](#mysql-使用示例)
8
+ - [PostgreSQL 使用示例](#postgresql-使用示例)
9
+ - [Redis 使用示例](#redis-使用示例)
10
+ - [Claude Desktop 配置示例](#claude-desktop-配置示例)
11
+ - [常见使用场景](#常见使用场景)
12
+
13
+ ---
14
+
15
+ ## MySQL 使用示例
16
+
17
+ ### 基础配置(只读模式)
18
+
19
+ ```json
20
+ {
21
+ "mcpServers": {
22
+ "mysql-db": {
23
+ "command": "npx",
24
+ "args": [
25
+ "universal-db-mcp",
26
+ "--type", "mysql",
27
+ "--host", "localhost",
28
+ "--port", "3306",
29
+ "--user", "root",
30
+ "--password", "your_password",
31
+ "--database", "myapp_db"
32
+ ]
33
+ }
34
+ }
35
+ }
36
+ ```
37
+
38
+ ### 启用写入模式(谨慎使用)
39
+
40
+ ```json
41
+ {
42
+ "mcpServers": {
43
+ "mysql-dev": {
44
+ "command": "npx",
45
+ "args": [
46
+ "universal-db-mcp",
47
+ "--type", "mysql",
48
+ "--host", "localhost",
49
+ "--port", "3306",
50
+ "--user", "dev_user",
51
+ "--password", "dev_password",
52
+ "--database", "dev_database",
53
+ "--danger-allow-write"
54
+ ]
55
+ }
56
+ }
57
+ }
58
+ ```
59
+
60
+ ### 与 Claude 对话示例
61
+
62
+ **用户**: 帮我查看 users 表的结构
63
+
64
+ **Claude 会自动**:
65
+ 1. 调用 `get_table_info` 工具
66
+ 2. 返回表的列信息、主键、索引等
67
+
68
+ **用户**: 统计最近 7 天注册的用户数量
69
+
70
+ **Claude 会自动**:
71
+ 1. 理解需求
72
+ 2. 生成 SQL: `SELECT COUNT(*) FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)`
73
+ 3. 调用 `execute_query` 工具执行
74
+ 4. 返回结果
75
+
76
+ ---
77
+
78
+ ## PostgreSQL 使用示例
79
+
80
+ ### 基础配置
81
+
82
+ ```json
83
+ {
84
+ "mcpServers": {
85
+ "postgres-db": {
86
+ "command": "npx",
87
+ "args": [
88
+ "universal-db-mcp",
89
+ "--type", "postgres",
90
+ "--host", "localhost",
91
+ "--port", "5432",
92
+ "--user", "postgres",
93
+ "--password", "your_password",
94
+ "--database", "myapp"
95
+ ]
96
+ }
97
+ }
98
+ }
99
+ ```
100
+
101
+ ### 连接远程数据库
102
+
103
+ ```json
104
+ {
105
+ "mcpServers": {
106
+ "postgres-prod": {
107
+ "command": "npx",
108
+ "args": [
109
+ "universal-db-mcp",
110
+ "--type", "postgres",
111
+ "--host", "db.example.com",
112
+ "--port", "5432",
113
+ "--user", "readonly_user",
114
+ "--password", "secure_password",
115
+ "--database", "production"
116
+ ]
117
+ }
118
+ }
119
+ }
120
+ ```
121
+
122
+ ### 与 Claude 对话示例
123
+
124
+ **用户**: 找出订单金额最高的 10 个客户
125
+
126
+ **Claude 会自动**:
127
+ 1. 调用 `get_schema` 了解表结构
128
+ 2. 生成复杂的 JOIN 查询
129
+ 3. 执行并返回结果
130
+
131
+ ---
132
+
133
+ ## Redis 使用示例
134
+
135
+ ### 基础配置(无密码)
136
+
137
+ ```json
138
+ {
139
+ "mcpServers": {
140
+ "redis-cache": {
141
+ "command": "npx",
142
+ "args": [
143
+ "universal-db-mcp",
144
+ "--type", "redis",
145
+ "--host", "localhost",
146
+ "--port", "6379"
147
+ ]
148
+ }
149
+ }
150
+ }
151
+ ```
152
+
153
+ ### 带密码和数据库选择
154
+
155
+ ```json
156
+ {
157
+ "mcpServers": {
158
+ "redis-session": {
159
+ "command": "npx",
160
+ "args": [
161
+ "universal-db-mcp",
162
+ "--type", "redis",
163
+ "--host", "localhost",
164
+ "--port", "6379",
165
+ "--password", "redis_password",
166
+ "--database", "1"
167
+ ]
168
+ }
169
+ }
170
+ }
171
+ ```
172
+
173
+ ### 与 Claude 对话示例
174
+
175
+ **用户**: 查看所有以 "user:" 开头的键
176
+
177
+ **Claude 会执行**: `KEYS user:*`
178
+
179
+ **用户**: 获取 user:1001 的信息
180
+
181
+ **Claude 会执行**: `GET user:1001` 或 `HGETALL user:1001`(根据数据类型)
182
+
183
+ ---
184
+
185
+ ## Claude Desktop 配置示例
186
+
187
+ ### 同时连接多个数据库
188
+
189
+ 你可以在 Claude Desktop 中同时配置多个数据库连接:
190
+
191
+ ```json
192
+ {
193
+ "mcpServers": {
194
+ "mysql-prod": {
195
+ "command": "npx",
196
+ "args": [
197
+ "universal-db-mcp",
198
+ "--type", "mysql",
199
+ "--host", "prod-db.example.com",
200
+ "--port", "3306",
201
+ "--user", "readonly",
202
+ "--password", "prod_password",
203
+ "--database", "production"
204
+ ]
205
+ },
206
+ "postgres-analytics": {
207
+ "command": "npx",
208
+ "args": [
209
+ "universal-db-mcp",
210
+ "--type", "postgres",
211
+ "--host", "analytics.example.com",
212
+ "--port", "5432",
213
+ "--user", "analyst",
214
+ "--password", "analytics_password",
215
+ "--database", "warehouse"
216
+ ]
217
+ },
218
+ "redis-cache": {
219
+ "command": "npx",
220
+ "args": [
221
+ "universal-db-mcp",
222
+ "--type", "redis",
223
+ "--host", "cache.example.com",
224
+ "--port", "6379",
225
+ "--password", "cache_password"
226
+ ]
227
+ }
228
+ }
229
+ }
230
+ ```
231
+
232
+ 重启 Claude Desktop 后,你可以在对话中指定使用哪个数据库:
233
+
234
+ - "在 MySQL 生产库中查询..."
235
+ - "从 PostgreSQL 分析库获取..."
236
+ - "检查 Redis 缓存中的..."
237
+
238
+ ---
239
+
240
+ ## 常见使用场景
241
+
242
+ ### 1. 数据分析
243
+
244
+ **场景**: 快速分析业务数据
245
+
246
+ ```
247
+ 用户: 帮我分析最近一个月的销售趋势
248
+
249
+ Claude 会:
250
+ 1. 查看 orders 表结构
251
+ 2. 按日期分组统计订单金额
252
+ 3. 生成趋势分析报告
253
+ ```
254
+
255
+ ### 2. 问题排查
256
+
257
+ **场景**: 排查生产问题
258
+
259
+ ```
260
+ 用户: 为什么用户 ID 12345 无法登录?
261
+
262
+ Claude 会:
263
+ 1. 查询 users 表找到该用户
264
+ 2. 检查 login_logs 表的最近记录
265
+ 3. 分析可能的原因(账号状态、密码错误次数等)
266
+ ```
267
+
268
+ ### 3. 数据迁移准备
269
+
270
+ **场景**: 了解数据库结构以准备迁移
271
+
272
+ ```
273
+ 用户: 帮我生成所有表的结构文档
274
+
275
+ Claude 会:
276
+ 1. 调用 get_schema 获取完整结构
277
+ 2. 整理成 Markdown 格式的文档
278
+ 3. 包含表名、列定义、索引、外键等信息
279
+ ```
280
+
281
+ ### 4. 性能优化建议
282
+
283
+ **场景**: 优化慢查询
284
+
285
+ ```
286
+ 用户: 这个查询很慢,帮我优化:SELECT * FROM orders WHERE user_id = 123
287
+
288
+ Claude 会:
289
+ 1. 查看 orders 表的索引情况
290
+ 2. 建议添加索引或修改查询
291
+ 3. 解释优化原理
292
+ ```
293
+
294
+ ### 5. Redis 缓存管理
295
+
296
+ **场景**: 管理缓存数据
297
+
298
+ ```
299
+ 用户: 清理所有过期的会话缓存
300
+
301
+ Claude 会:
302
+ 1. 查找所有 session: 开头的键
303
+ 2. 检查 TTL
304
+ 3. 在写入模式下执行清理(需要 --danger-allow-write)
305
+ ```
306
+
307
+ ---
308
+
309
+ ## 安全提示
310
+
311
+ ### ✅ 推荐做法
312
+
313
+ 1. **生产环境只读**: 生产数据库永远不要启用 `--danger-allow-write`
314
+ 2. **使用专用账号**: 为 MCP 创建权限受限的数据库账号
315
+ 3. **网络隔离**: 通过 VPN 或跳板机访问生产数据库
316
+ 4. **审计日志**: 定期检查 Claude Desktop 的操作日志
317
+
318
+ ### ❌ 避免做法
319
+
320
+ 1. 不要在生产环境启用写入模式
321
+ 2. 不要使用 root 或 admin 账号
322
+ 3. 不要在公共网络直接连接数据库
323
+ 4. 不要在配置文件中明文存储密码(考虑使用环境变量)
324
+
325
+ ---
326
+
327
+ ## 故障排查
328
+
329
+ ### 连接失败
330
+
331
+ **错误**: `数据库连接失败`
332
+
333
+ **解决方案**:
334
+ 1. 检查数据库服务是否运行
335
+ 2. 验证主机地址和端口
336
+ 3. 确认用户名和密码正确
337
+ 4. 检查防火墙规则
338
+
339
+ ### 权限不足
340
+
341
+ **错误**: `Access denied` 或 `permission denied`
342
+
343
+ **解决方案**:
344
+ 1. 确认数据库用户有足够权限
345
+ 2. MySQL: `GRANT SELECT ON database.* TO 'user'@'host';`
346
+ 3. PostgreSQL: `GRANT SELECT ON ALL TABLES IN SCHEMA public TO user;`
347
+
348
+ ### 写操作被拒绝
349
+
350
+ **错误**: `操作被拒绝:当前处于只读安全模式`
351
+
352
+ **解决方案**:
353
+ - 这是安全特性,如需写入,添加 `--danger-allow-write` 参数
354
+ - 仅在开发环境使用!
355
+
356
+ ---
357
+
358
+ ## 更多帮助
359
+
360
+ - 查看 [README.md](./README.md) 了解项目概述
361
+ - 查看 [CONTRIBUTING.md](./CONTRIBUTING.md) 了解如何贡献
362
+ - 提交 Issue: https://github.com/yourusername/universal-db-mcp/issues
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Universal DB MCP Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,167 @@
1
+ # 🔌 MCP 数据库万能连接器
2
+
3
+ > 让 Claude Desktop 直接连接你的数据库,用自然语言查询和分析数据
4
+
5
+ [![npm version](https://img.shields.io/npm/v/universal-db-mcp.svg)](https://www.npmjs.com/package/universal-db-mcp)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ ## 🎯 为什么使用本项目
9
+
10
+ 作为开发者,你是否遇到过这些场景:
11
+
12
+ - 📊 **临时数据分析**:想快速查看生产数据库的某些指标,但不想写 SQL?
13
+ - 🔍 **问题排查**:需要跨多个表关联查询,但记不清表结构?
14
+ - 🤖 **AI 辅助开发**:希望 Claude 能直接理解你的数据库结构,生成准确的查询?
15
+
16
+ **MCP 数据库万能连接器** 通过 Model Context Protocol (MCP) 协议,让 Claude Desktop 成为你的数据库助手:
17
+
18
+ ✅ **自然语言查询** - 用中文描述需求,Claude 自动生成并执行 SQL
19
+ ✅ **智能表结构理解** - 自动获取数据库 Schema,提供精准建议
20
+ ✅ **多数据库支持** - MySQL、PostgreSQL、Redis 一键切换
21
+ ✅ **安全第一** - 默认只读模式,防止误操作删库
22
+ ✅ **开箱即用** - 无需复杂配置,一行命令启动
23
+
24
+ ## 🚀 快速开始
25
+
26
+ ### 前置要求
27
+
28
+ - Node.js >= 20
29
+ - Claude Desktop 应用
30
+ - 至少一个数据库实例(MySQL/PostgreSQL/Redis)
31
+
32
+ ### 安装
33
+
34
+ ```bash
35
+ npm install -g universal-db-mcp
36
+ ```
37
+
38
+ 或使用 npx 直接运行(无需安装):
39
+
40
+ ```bash
41
+ npx universal-db-mcp
42
+ ```
43
+
44
+ ### 配置 Claude Desktop
45
+
46
+ 编辑 Claude Desktop 配置文件:
47
+
48
+ **macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
49
+ **Windows**: `%APPDATA%\Claude\claude_desktop_config.json`
50
+
51
+ 添加以下配置:
52
+
53
+ ```json
54
+ {
55
+ "mcpServers": {
56
+ "universal-db": {
57
+ "command": "npx",
58
+ "args": [
59
+ "universal-db-mcp",
60
+ "--type", "mysql",
61
+ "--host", "localhost",
62
+ "--port", "3306",
63
+ "--user", "root",
64
+ "--password", "your_password",
65
+ "--database", "your_database"
66
+ ]
67
+ }
68
+ }
69
+ }
70
+ ```
71
+
72
+ ### 启动使用
73
+
74
+ 1. 重启 Claude Desktop
75
+ 2. 在对话中直接询问:
76
+ - "帮我查看 users 表的结构"
77
+ - "统计最近 7 天的订单数量"
78
+ - "找出消费金额最高的 10 个用户"
79
+
80
+ Claude 会自动调用数据库工具完成查询!
81
+
82
+ ## 🛡️ 安全模式
83
+
84
+ **默认情况下,本工具运行在只读模式**,会拒绝所有写入操作(DELETE、UPDATE、DROP、TRUNCATE)。
85
+
86
+ 如果你需要执行写入操作(请谨慎!),需要显式添加参数:
87
+
88
+ ```json
89
+ {
90
+ "args": [
91
+ "universal-db-mcp",
92
+ "--danger-allow-write",
93
+ "--type", "mysql",
94
+ ...
95
+ ]
96
+ }
97
+ ```
98
+
99
+ ⚠️ **警告**:启用写入模式后,Claude 可以修改你的数据库。请仅在开发环境使用,或确保你完全理解操作的后果。
100
+
101
+ ## 📖 支持的数据库
102
+
103
+ | 数据库 | 类型参数 | 状态 |
104
+ |--------|---------|------|
105
+ | MySQL | `--type mysql` | ✅ 已支持 |
106
+ | PostgreSQL | `--type postgres` | ✅ 已支持 |
107
+ | Redis | `--type redis` | ✅ 已支持 |
108
+ | MongoDB | `--type mongo` | 🚧 计划中 |
109
+ | SQLite | `--type sqlite` | 🚧 计划中 |
110
+
111
+ ## 🔧 命令行参数
112
+
113
+ ```bash
114
+ universal-db-mcp [选项]
115
+
116
+ 选项:
117
+ --type <db> 数据库类型 (mysql|postgres|redis)
118
+ --host <host> 数据库主机地址 (默认: localhost)
119
+ --port <port> 数据库端口
120
+ --user <user> 用户名
121
+ --password <password> 密码
122
+ --database <database> 数据库名称
123
+ --danger-allow-write 启用写入模式(危险!)
124
+ --help 显示帮助信息
125
+ ```
126
+
127
+ ## 🏗️ 架构设计
128
+
129
+ 本项目采用模块化适配器模式,方便社区贡献新的数据库支持:
130
+
131
+ ```
132
+ src/
133
+ ├── adapters/ # 数据库适配器
134
+ │ ├── mysql.ts
135
+ │ ├── postgres.ts
136
+ │ └── redis.ts
137
+ ├── types/ # TypeScript 类型定义
138
+ │ └── adapter.ts
139
+ ├── utils/ # 工具函数
140
+ │ └── safety.ts # 安全检查逻辑
141
+ └── server.ts # MCP 服务器主逻辑
142
+ ```
143
+
144
+ ## 🤝 贡献指南
145
+
146
+ 欢迎提交 Issue 和 Pull Request!
147
+
148
+ 如果你想添加新的数据库支持,只需:
149
+
150
+ 1. 在 `src/adapters/` 下实现 `DbAdapter` 接口
151
+ 2. 添加对应的数据库驱动依赖
152
+ 3. 更新 README 文档
153
+
154
+ 详见 [CONTRIBUTING.md](./CONTRIBUTING.md)
155
+
156
+ ## 📄 开源协议
157
+
158
+ MIT License - 自由使用,欢迎 Star ⭐
159
+
160
+ ## 🙏 致谢
161
+
162
+ - [Model Context Protocol](https://modelcontextprotocol.io/) - Anthropic 提供的强大协议
163
+ - 所有贡献者和使用者
164
+
165
+ ---
166
+
167
+ **如果这个项目对你有帮助,请给个 Star ⭐ 支持一下!**
@@ -0,0 +1,41 @@
1
+ /**
2
+ * MySQL 数据库适配器
3
+ * 使用 mysql2 驱动实现 DbAdapter 接口
4
+ */
5
+ import type { DbAdapter, QueryResult, SchemaInfo } from '../types/adapter.js';
6
+ export declare class MySQLAdapter implements DbAdapter {
7
+ private connection;
8
+ private config;
9
+ constructor(config: {
10
+ host: string;
11
+ port: number;
12
+ user?: string;
13
+ password?: string;
14
+ database?: string;
15
+ });
16
+ /**
17
+ * 连接到 MySQL 数据库
18
+ */
19
+ connect(): Promise<void>;
20
+ /**
21
+ * 断开数据库连接
22
+ */
23
+ disconnect(): Promise<void>;
24
+ /**
25
+ * 执行 SQL 查询
26
+ */
27
+ executeQuery(query: string, params?: unknown[]): Promise<QueryResult>;
28
+ /**
29
+ * 获取数据库结构信息
30
+ */
31
+ getSchema(): Promise<SchemaInfo>;
32
+ /**
33
+ * 获取单个表的详细信息
34
+ */
35
+ private getTableInfo;
36
+ /**
37
+ * 检查是否为写操作
38
+ */
39
+ isWriteOperation(query: string): boolean;
40
+ }
41
+ //# sourceMappingURL=mysql.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mysql.d.ts","sourceRoot":"","sources":["../../src/adapters/mysql.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EACV,SAAS,EACT,WAAW,EACX,UAAU,EAIX,MAAM,qBAAqB,CAAC;AAG7B,qBAAa,YAAa,YAAW,SAAS;IAC5C,OAAO,CAAC,UAAU,CAAiC;IACnD,OAAO,CAAC,MAAM,CAMZ;gBAEU,MAAM,EAAE;QAClB,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB;IAID;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAqB9B;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAOjC;;OAEG;IACG,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC;IAwC3E;;OAEG;IACG,SAAS,IAAI,OAAO,CAAC,UAAU,CAAC;IAwCtC;;OAEG;YACW,YAAY;IAuE1B;;OAEG;IACH,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;CAGzC"}