mcp-dbutils 0.16.0__py3-none-any.whl → 0.17.0__py3-none-any.whl

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,572 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: mcp-dbutils
3
- Version: 0.16.0
4
- Summary: MCP Database Utilities Service
5
- Author: Dong Hao
6
- License-Expression: MIT
7
- License-File: LICENSE
8
- Requires-Python: >=3.10
9
- Requires-Dist: mcp>=1.2.1
10
- Requires-Dist: mysql-connector-python>=8.2.0
11
- Requires-Dist: psycopg2-binary>=2.9.10
12
- Requires-Dist: python-dotenv>=1.0.1
13
- Requires-Dist: pyyaml>=6.0.2
14
- Provides-Extra: test
15
- Requires-Dist: aiosqlite>=0.19.0; extra == 'test'
16
- Requires-Dist: docker>=7.0.0; extra == 'test'
17
- Requires-Dist: pre-commit>=3.6.0; extra == 'test'
18
- Requires-Dist: pytest-asyncio>=0.23.0; extra == 'test'
19
- Requires-Dist: pytest-cov>=4.1.0; extra == 'test'
20
- Requires-Dist: pytest-docker>=2.0.0; extra == 'test'
21
- Requires-Dist: pytest>=7.0.0; extra == 'test'
22
- Requires-Dist: ruff>=0.3.0; extra == 'test'
23
- Requires-Dist: testcontainers>=3.7.0; extra == 'test'
24
- Description-Content-Type: text/markdown
25
-
26
- # MCP Database Utilities
27
-
28
- ![GitHub Repo stars](https://img.shields.io/github/stars/donghao1393/mcp-dbutils)
29
- ![PyPI version](https://img.shields.io/pypi/v/mcp-dbutils)
30
- [![Coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/donghao1393/bdd0a63ec2a816539ff8c136ceb41e48/raw/coverage.json)](https://github.com/donghao1393/mcp-dbutils/actions)
31
- ![Python](https://img.shields.io/badge/Python-3.10%2B-blue)
32
- ![License](https://img.shields.io/github/license/donghao1393/mcp-dbutils)
33
- [![smithery badge](https://smithery.ai/badge/@donghao1393/mcp-dbutils)](https://smithery.ai/server/@donghao1393/mcp-dbutils)
34
-
35
- [中文文档](README_CN.md)
36
-
37
- ## Overview
38
- MCP Database Utilities is a unified database access service that supports multiple database types (PostgreSQL, SQLite, and MySQL). Through its abstraction layer design, it provides a simple and unified database operation interface for MCP servers.
39
-
40
- ## Features
41
- - Unified database access interface
42
- - Support for multiple database configurations
43
- - Secure read-only query execution
44
- - Table structure and schema information retrieval
45
- - Database tables listing via MCP tools
46
- - Intelligent connection management and resource cleanup
47
- - Debug mode support
48
- - SSL/TLS connection support for PostgreSQL and MySQL
49
-
50
- ## Installation and Configuration
51
-
52
- ### Installation Methods
53
- #### Installing via Smithery
54
-
55
- To install Database Utilities for Claude Desktop automatically via [Smithery](https://smithery.ai/server/@donghao1393/mcp-dbutils):
56
-
57
- ```bash
58
- npx -y @smithery/cli install @donghao1393/mcp-dbutils --client claude
59
- ```
60
-
61
- #### Using uvx (Recommended)
62
- No installation required, run directly using `uvx`:
63
- ```bash
64
- uvx mcp-dbutils --config /path/to/config.yaml
65
- ```
66
-
67
- Add to Claude configuration:
68
- ```json
69
- "mcpServers": {
70
- "dbutils": {
71
- "command": "uvx",
72
- "args": [
73
- "mcp-dbutils",
74
- "--config",
75
- "/path/to/config.yaml"
76
- ],
77
- "env": {
78
- "MCP_DEBUG": "1" // Optional: Enable debug mode
79
- }
80
- }
81
- }
82
- ```
83
-
84
- #### Using pip
85
- ```bash
86
- pip install mcp-dbutils
87
- ```
88
-
89
- Add to Claude configuration:
90
- ```json
91
- "mcpServers": {
92
- "dbutils": {
93
- "command": "python",
94
- "args": [
95
- "-m",
96
- "mcp_dbutils",
97
- "--config",
98
- "/path/to/config.yaml"
99
- ],
100
- "env": {
101
- "MCP_DEBUG": "1" // Optional: Enable debug mode
102
- }
103
- }
104
- }
105
- ```
106
-
107
- #### Using Docker
108
- ```bash
109
- docker run -i --rm \
110
- -v /path/to/config.yaml:/app/config.yaml \
111
- -v /path/to/sqlite.db:/app/sqlite.db \ # Optional: for SQLite database
112
- -e MCP_DEBUG=1 \ # Optional: Enable debug mode
113
- mcp/dbutils --config /app/config.yaml
114
- ```
115
-
116
- Add to Claude configuration:
117
- ```json
118
- "mcpServers": {
119
- "dbutils": {
120
- "command": "docker",
121
- "args": [
122
- "run",
123
- "-i",
124
- "--rm",
125
- "-v",
126
- "/path/to/config.yaml:/app/config.yaml",
127
- "-v",
128
- "/path/to/sqlite.db:/app/sqlite.db", // Optional: for SQLite database
129
- "mcp/dbutils",
130
- "--config",
131
- "/app/config.yaml"
132
- ],
133
- "env": {
134
- "MCP_DEBUG": "1" // Optional: Enable debug mode
135
- }
136
- }
137
- }
138
- ```
139
-
140
- > **Note for Docker database connections:**
141
- > - For SQLite: Mount your database file using `-v /path/to/sqlite.db:/app/sqlite.db`
142
- > - For PostgreSQL running on host:
143
- > - On Mac/Windows: Use `host.docker.internal` as host in config
144
- > - On Linux: Use `172.17.0.1` (docker0 IP) or run with `--network="host"`
145
-
146
- ### Requirements
147
- - Python 3.10+
148
- - PostgreSQL (optional)
149
- - SQLite3 (optional)
150
- - MySQL (optional)
151
-
152
- ### Configuration File
153
- The project requires a YAML configuration file, specified via the `--config` parameter. Configuration examples:
154
-
155
- ```yaml
156
- connections:
157
- # SQLite configuration examples
158
- dev-db:
159
- type: sqlite
160
- path: /path/to/dev.db
161
- # Password is optional
162
- password:
163
-
164
- # PostgreSQL standard configuration
165
- test-db:
166
- type: postgres
167
- host: postgres.example.com
168
- port: 5432
169
- dbname: test_db
170
- user: test_user
171
- password: test_pass
172
-
173
- # PostgreSQL URL configuration with SSL
174
- prod-db:
175
- type: postgres
176
- url: postgresql://postgres.example.com:5432/prod-db?sslmode=verify-full
177
- user: prod_user
178
- password: prod_pass
179
-
180
- # PostgreSQL full SSL configuration example
181
- secure-db:
182
- type: postgres
183
- host: secure-db.example.com
184
- port: 5432
185
- dbname: secure_db
186
- user: secure_user
187
- password: secure_pass
188
- ssl:
189
- mode: verify-full # disable/require/verify-ca/verify-full
190
- cert: /path/to/client-cert.pem
191
- key: /path/to/client-key.pem
192
- root: /path/to/root.crt
193
-
194
- # MySQL standard configuration
195
- sandbox-mysql:
196
- type: mysql
197
- host: localhost
198
- port: 3306
199
- database: sandbox_db
200
- user: sandbox_user
201
- password: sandbox_pass
202
- charset: utf8mb4
203
-
204
- # MySQL URL configuration
205
- integration-mysql:
206
- type: mysql
207
- url: mysql://mysql.example.com:3306/integration_db?charset=utf8mb4
208
- user: integration_user
209
- password: integration_pass
210
-
211
- # MySQL with SSL configuration
212
- secure-mysql:
213
- type: mysql
214
- host: secure-mysql.example.com
215
- port: 3306
216
- database: secure_db
217
- user: secure_user
218
- password: secure_pass
219
- charset: utf8mb4
220
- ssl:
221
- mode: verify_identity
222
- ca: /path/to/ca.pem
223
- cert: /path/to/client-cert.pem
224
- key: /path/to/client-key.pem
225
- ```
226
-
227
- Database SSL Configuration Options:
228
-
229
- PostgreSQL SSL Configuration:
230
- 1. Using URL parameters:
231
- ```
232
- postgresql://host:port/dbname?sslmode=verify-full&sslcert=/path/to/cert.pem
233
- ```
234
- 2. Using dedicated SSL configuration section:
235
- ```yaml
236
- ssl:
237
- mode: verify-full # SSL verification mode
238
- cert: /path/to/cert.pem # Client certificate
239
- key: /path/to/key.pem # Client private key
240
- root: /path/to/root.crt # CA certificate
241
- ```
242
-
243
- PostgreSQL SSL Modes:
244
- - disable: No SSL
245
- - require: Use SSL but no certificate verification
246
- - verify-ca: Verify server certificate is signed by trusted CA
247
- - verify-full: Verify server certificate and hostname match
248
-
249
- MySQL SSL Configuration:
250
- 1. Using URL parameters:
251
- ```
252
- mysql://host:port/dbname?ssl-mode=verify_identity&ssl-ca=/path/to/ca.pem
253
- ```
254
- 2. Using dedicated SSL configuration section:
255
- ```yaml
256
- ssl:
257
- mode: verify_identity # SSL verification mode
258
- ca: /path/to/ca.pem # CA certificate
259
- cert: /path/to/cert.pem # Client certificate
260
- key: /path/to/key.pem # Client private key
261
- ```
262
-
263
- MySQL SSL Modes:
264
- - disabled: No SSL
265
- - preferred: Use SSL if available, but allow unencrypted connection
266
- - required: Always use SSL, but don't verify server certificate
267
- - verify_ca: Verify server certificate is signed by trusted CA
268
- - verify_identity: Verify server certificate and hostname match
269
-
270
- SQLite Configuration Options:
271
- 1. Basic configuration with path:
272
- ```yaml
273
- type: sqlite
274
- path: /path/to/db.sqlite
275
- password: optional_password # Optional encryption
276
- ```
277
- 2. Using URI parameters:
278
- ```yaml
279
- type: sqlite
280
- path: /path/to/db.sqlite?mode=ro&cache=shared
281
- ```
282
-
283
- ### Debug Mode
284
- Set environment variable `MCP_DEBUG=1` to enable debug mode for detailed logging output.
285
-
286
- ## Architecture Design
287
-
288
- ### Core Concept: Abstraction Layer
289
-
290
- ```mermaid
291
- graph TD
292
- Client[Client] --> DatabaseServer[Database Server]
293
- subgraph MCP Server
294
- DatabaseServer
295
- DatabaseHandler[Database Handler]
296
- PostgresHandler[PostgreSQL Handler]
297
- SQLiteHandler[SQLite Handler]
298
- MySQLHandler[MySQL Handler]
299
- DatabaseServer --> DatabaseHandler
300
- DatabaseHandler --> PostgresHandler
301
- DatabaseHandler --> SQLiteHandler
302
- DatabaseHandler --> MySQLHandler
303
- end
304
- PostgresHandler --> PostgreSQL[(PostgreSQL)]
305
- SQLiteHandler --> SQLite[(SQLite)]
306
- MySQLHandler --> MySQL[(MySQL)]
307
- ```
308
-
309
- The abstraction layer design is the core architectural concept in MCP Database Utilities. Just like a universal remote control that works with different devices, users only need to know the basic operations without understanding the underlying complexities.
310
-
311
- #### 1. Simplified User Interaction
312
- - Users only need to know the database configuration name (e.g., "my_postgres")
313
- - No need to deal with connection parameters and implementation details
314
- - MCP server automatically handles database connections and queries
315
-
316
- #### 2. Unified Interface Design
317
- - DatabaseHandler abstract class defines unified operation interfaces
318
- - All specific database implementations (PostgreSQL/SQLite/MySQL) follow the same interface
319
- - Users interact with different databases in the same way
320
-
321
- #### 3. Configuration and Implementation Separation
322
- - Complex database configuration parameters are encapsulated in configuration files
323
- - Runtime access through simple database names
324
- - Easy management and modification of database configurations without affecting business code
325
-
326
- ### System Components
327
- 1. DatabaseServer
328
- - Core component of the MCP server
329
- - Handles resource and tool requests
330
- - Manages database connection lifecycle
331
-
332
- 2. DatabaseHandler
333
- - Abstract base class defining unified interface
334
- - Includes get_tables(), get_schema(), execute_query(), etc.
335
- - Implemented by PostgreSQL, SQLite, and MySQL handlers
336
-
337
- 3. Configuration System
338
- - YAML-based configuration file
339
- - Support for multiple database configurations
340
- - Type-safe configuration validation
341
-
342
- 4. Error Handling and Logging
343
- - Unified error handling mechanism
344
- - Detailed logging output
345
- - Sensitive information masking
346
-
347
- ## Usage Examples
348
-
349
- ### Basic Query
350
- ```python
351
- # Access through connection name
352
- async with server.get_handler("my_postgres") as handler:
353
- # Execute SQL query
354
- result = await handler.execute_query("SELECT * FROM users")
355
- ```
356
-
357
- ### View Table Structure
358
- ```python
359
- # Get all tables
360
- tables = await handler.get_tables()
361
-
362
- # Get specific table schema
363
- schema = await handler.get_schema("users")
364
- ```
365
-
366
- ### Error Handling
367
- ```python
368
- try:
369
- async with server.get_handler("my_connection") as handler:
370
- result = await handler.execute_query("SELECT * FROM users")
371
- except ValueError as e:
372
- print(f"Configuration error: {e}")
373
- except Exception as e:
374
- print(f"Query error: {e}")
375
- ```
376
-
377
- ## Security Notes
378
- - Supports SELECT queries only to protect database security
379
- - Automatically masks sensitive information (like passwords) in logs
380
- - Executes queries in read-only transactions
381
-
382
- ## API Documentation
383
-
384
- ### DatabaseServer
385
- Core server class providing:
386
- - Resource list retrieval
387
- - Tool call handling (list_tables, query)
388
- - Database handler management
389
-
390
- ### MCP Tools
391
-
392
- #### dbutils-list-tables
393
- Lists all tables in the specified database.
394
- - Parameters:
395
- * connection: Database connection name
396
- - Returns: Text content with a list of table names
397
-
398
- #### dbutils-run-query
399
- Executes a SQL query on the specified database.
400
- - Parameters:
401
- * connection: Database connection name
402
- * sql: SQL query to execute (SELECT only)
403
- - Returns: Query results in a formatted text
404
-
405
- #### dbutils-get-stats
406
- Get table statistics information.
407
- - Parameters:
408
- * connection: Database connection name
409
- * table: Table name
410
- - Returns: Statistics including row count, size, column stats
411
-
412
- #### dbutils-list-constraints
413
- List table constraints (primary key, foreign keys, etc).
414
- - Parameters:
415
- * connection: Database connection name
416
- * table: Table name
417
- - Returns: Detailed constraint information
418
-
419
- #### dbutils-explain-query
420
- Get query execution plan with cost estimates.
421
- - Parameters:
422
- * connection: Database connection name
423
- * sql: SQL query to explain
424
- - Returns: Formatted execution plan
425
-
426
- #### dbutils-get-performance
427
- Get database performance statistics.
428
- - Parameters:
429
- * connection: Database connection name
430
- - Returns: Detailed performance statistics including query times, query types, error rates, and resource usage
431
-
432
- #### dbutils-analyze-query
433
- Analyze a SQL query for performance and provide optimization suggestions.
434
- - Parameters:
435
- * connection: Database connection name
436
- * sql: SQL query to analyze
437
- - Returns: Query analysis with execution plan, timing information, and optimization suggestions
438
-
439
- ### DatabaseHandler
440
- Abstract base class defining interfaces:
441
- - get_tables(): Get table resource list
442
- - get_schema(): Get table structure
443
- - execute_query(): Execute SQL query
444
- - cleanup(): Resource cleanup
445
-
446
- ### PostgreSQL Implementation
447
- Provides PostgreSQL-specific features:
448
- - Remote connection support
449
- - Table description information
450
- - Constraint queries
451
-
452
- ### SQLite Implementation
453
- Provides SQLite-specific features:
454
- - File path handling
455
- - URI scheme support
456
- - Password protection support (optional)
457
-
458
- ### MySQL Implementation
459
- Provides MySQL-specific features:
460
- - Remote connection support
461
- - Character set configuration
462
- - SSL/TLS secure connection
463
- - URL and standard connection methods
464
-
465
- ## Code Quality
466
-
467
- ### Quality Gates
468
- We use SonarCloud to maintain high code quality standards. All pull requests must pass the following quality gates:
469
-
470
- - Code Coverage: ≥ 80%
471
- - Code Quality:
472
- * No blocker or critical issues
473
- * Less than 10 major issues
474
- * Code duplication < 3%
475
- - Security:
476
- * No security vulnerabilities
477
- * No security hotspots
478
-
479
- ### Automated Checks
480
- Our CI/CD pipeline automatically performs:
481
- 1. Full test suite execution
482
- 2. Code coverage analysis
483
- 3. SonarCloud static code analysis
484
- 4. Quality gate validation
485
-
486
- Pull requests that don't meet these standards will be automatically blocked from merging.
487
-
488
- ### Code Style
489
- We use Ruff for code style checking and formatting:
490
-
491
- [![Code Style](https://img.shields.io/badge/code%20style-ruff-000000.svg)](https://github.com/astral-sh/ruff)
492
-
493
- All code must follow our style guide:
494
- - Line length: 88 characters
495
- - Indentation: 4 spaces
496
- - Quotes: Double quotes
497
- - Naming: PEP8 conventions
498
-
499
- For detailed guidelines, see [STYLE_GUIDE.md](docs/STYLE_GUIDE.md).
500
-
501
- ### Local Development
502
- To check code quality locally:
503
- 1. Run tests with coverage:
504
- ```bash
505
- pytest --cov=src/mcp_dbutils --cov-report=xml:coverage.xml tests/
506
- ```
507
- 2. Use SonarLint in your IDE to catch issues early
508
- 3. Review SonarCloud analysis results in PR comments
509
- 4. Run Ruff for code style checking:
510
- ```bash
511
- # Install Ruff
512
- uv pip install ruff
513
-
514
- # Check code style
515
- ruff check .
516
-
517
- # Format code
518
- ruff format .
519
- ```
520
- 5. Use pre-commit hooks for automatic checks:
521
- ```bash
522
- # Install pre-commit
523
- uv pip install pre-commit
524
- pre-commit install
525
-
526
- # Run all checks
527
- pre-commit run --all-files
528
- ```
529
-
530
- ### SonarCloud AI Integration
531
- We've implemented an AI-assisted workflow for fixing SonarCloud issues:
532
-
533
- 1. Our CI/CD pipeline automatically extracts SonarCloud analysis results
534
- 2. Results are formatted into both JSON and Markdown formats
535
- 3. These reports can be downloaded using the provided Fish function
536
- 4. The reports can then be provided to AI tools for analysis and fix suggestions
537
-
538
- For detailed instructions, see [SonarCloud AI Integration Guide](docs/sonarcloud-ai-integration.md).
539
-
540
- ```bash
541
- # Load the function
542
- source scripts/sonar-ai-fix.fish
543
-
544
- # Download the latest SonarCloud analysis reports
545
- sonar-ai-fix
546
- ```
547
-
548
- ## Contributing
549
- Contributions are welcome! Here's how you can help:
550
-
551
- 1. 🐛 Report bugs: Open an issue describing the bug and how to reproduce it
552
- 2. 💡 Suggest features: Open an issue to propose new features
553
- 3. 🛠️ Submit PRs: Fork the repo and create a pull request with your changes
554
-
555
- ### Development Setup
556
- 1. Clone the repository
557
- 2. Create a virtual environment using `uv venv`
558
- 3. Install dependencies with `uv sync --all-extras`
559
- 4. Run tests with `pytest`
560
-
561
- For detailed guidelines, see [CONTRIBUTING.md](.github/CONTRIBUTING.md)
562
-
563
- ## Acknowledgments
564
- - [MCP Servers](https://github.com/modelcontextprotocol/servers) for inspiration and demonstration
565
- - AI Editors:
566
- * [Claude Desktop](https://claude.ai/download)
567
- * [Cline](https://cline.bot)
568
- - [Model Context Protocol](https://modelcontextprotocol.io/) for comprehensive interfaces
569
-
570
- ## Star History
571
-
572
- [![Star History Chart](https://api.star-history.com/svg?repos=donghao1393/mcp-dbutils&type=Date)](https://star-history.com/#donghao1393/mcp-dbutils&Date)