db-connect-mcp 0.1.6__tar.gz
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.
- db_connect_mcp-0.1.6/.gitignore +181 -0
- db_connect_mcp-0.1.6/LICENSE +21 -0
- db_connect_mcp-0.1.6/PKG-INFO +489 -0
- db_connect_mcp-0.1.6/README.md +457 -0
- db_connect_mcp-0.1.6/pyproject.toml +94 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/__init__.py +30 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/__main__.py +6 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/adapters/__init__.py +72 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/adapters/base.py +175 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/adapters/clickhouse.py +384 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/adapters/mysql.py +373 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/adapters/postgresql.py +586 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/core/__init__.py +13 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/core/analyzer.py +114 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/core/connection.py +371 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/core/executor.py +251 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/core/inspector.py +345 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/models/__init__.py +23 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/models/capabilities.py +98 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/models/config.py +401 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/models/database.py +112 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/models/profile.py +85 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/models/query.py +119 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/models/statistics.py +176 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/models/table.py +230 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/server.py +524 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/utils/__init__.py +13 -0
- db_connect_mcp-0.1.6/src/db_connect_mcp/utils/serialization.py +126 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
|
|
110
|
+
# pdm
|
|
111
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
112
|
+
#pdm.lock
|
|
113
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
114
|
+
# in version control.
|
|
115
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
116
|
+
.pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
121
|
+
__pypackages__/
|
|
122
|
+
|
|
123
|
+
# Celery stuff
|
|
124
|
+
celerybeat-schedule
|
|
125
|
+
celerybeat.pid
|
|
126
|
+
|
|
127
|
+
# SageMath parsed files
|
|
128
|
+
*.sage.py
|
|
129
|
+
|
|
130
|
+
# Environments
|
|
131
|
+
.env
|
|
132
|
+
.venv
|
|
133
|
+
env/
|
|
134
|
+
venv/
|
|
135
|
+
ENV/
|
|
136
|
+
env.bak/
|
|
137
|
+
venv.bak/
|
|
138
|
+
|
|
139
|
+
# Spyder project settings
|
|
140
|
+
.spyderproject
|
|
141
|
+
.spyproject
|
|
142
|
+
|
|
143
|
+
# Rope project settings
|
|
144
|
+
.ropeproject
|
|
145
|
+
|
|
146
|
+
# mkdocs documentation
|
|
147
|
+
/site
|
|
148
|
+
|
|
149
|
+
# mypy
|
|
150
|
+
.mypy_cache/
|
|
151
|
+
.dmypy.json
|
|
152
|
+
dmypy.json
|
|
153
|
+
|
|
154
|
+
# Pyre type checker
|
|
155
|
+
.pyre/
|
|
156
|
+
|
|
157
|
+
# pytype static type analyzer
|
|
158
|
+
.pytype/
|
|
159
|
+
|
|
160
|
+
# Cython debug symbols
|
|
161
|
+
cython_debug/
|
|
162
|
+
|
|
163
|
+
# PyCharm
|
|
164
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
165
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
166
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
167
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
168
|
+
#.idea/
|
|
169
|
+
|
|
170
|
+
# Ruff stuff:
|
|
171
|
+
.ruff_cache/
|
|
172
|
+
|
|
173
|
+
# PyPI configuration file
|
|
174
|
+
.pypirc
|
|
175
|
+
|
|
176
|
+
# Cursor
|
|
177
|
+
# Cursor is an AI-powered code editor.`.cursorignore` specifies files/directories to
|
|
178
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
179
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
180
|
+
.cursorignore
|
|
181
|
+
.cursorindexingignore
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yuri Gui
|
|
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.
|
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: db-connect-mcp
|
|
3
|
+
Version: 0.1.6
|
|
4
|
+
Summary: Multi-database MCP server for PostgreSQL, MySQL, and ClickHouse
|
|
5
|
+
Project-URL: Homepage, https://github.com/yugui923/db-connect-mcp
|
|
6
|
+
Project-URL: Repository, https://github.com/yugui923/db-connect-mcp
|
|
7
|
+
Project-URL: Issues, https://github.com/yugui923/db-connect-mcp/issues
|
|
8
|
+
Author-email: Yuri Gui <yugui923@users.noreply.github.com>
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: analysis,claude,clickhouse,data-exploration,database,mcp,mysql,postgresql
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Framework :: AsyncIO
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Database
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Requires-Dist: aiomysql>=0.2.0
|
|
25
|
+
Requires-Dist: asyncpg>=0.29.0
|
|
26
|
+
Requires-Dist: clickhouse-connect>=0.7.0
|
|
27
|
+
Requires-Dist: mcp>=1.0.0
|
|
28
|
+
Requires-Dist: pydantic>=2.0.0
|
|
29
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
30
|
+
Requires-Dist: sqlalchemy[asyncio]>=2.0.0
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# db-connect-mcp - Multi-Database MCP Server
|
|
34
|
+
|
|
35
|
+
A read-only MCP (Model Context Protocol) server for exploratory data analysis across multiple database systems. This server provides safe, read-only access to PostgreSQL, MySQL, and ClickHouse databases with comprehensive analysis capabilities.
|
|
36
|
+
|
|
37
|
+
## Quick Start
|
|
38
|
+
|
|
39
|
+
1. **Install:**
|
|
40
|
+
```bash
|
|
41
|
+
pip install db-connect-mcp
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
2. **Add to Claude Desktop** `claude_desktop_config.json`:
|
|
45
|
+
```json
|
|
46
|
+
{
|
|
47
|
+
"mcpServers": {
|
|
48
|
+
"db-connect": {
|
|
49
|
+
"command": "python",
|
|
50
|
+
"args": ["-m", "db_connect_mcp"],
|
|
51
|
+
"env": {
|
|
52
|
+
"DATABASE_URL": "postgresql://user:pass@localhost:5432/mydb"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
3. **Restart Claude Desktop** and start querying your database!
|
|
60
|
+
|
|
61
|
+
> **Note**: Using `python -m db_connect_mcp` ensures the command works even if Python's Scripts directory isn't in your PATH.
|
|
62
|
+
|
|
63
|
+
## Features
|
|
64
|
+
|
|
65
|
+
### 🗄️ Multi-Database Support
|
|
66
|
+
- **PostgreSQL** - Full support with advanced metadata and statistics
|
|
67
|
+
- **MySQL** - Complete support for MySQL and MariaDB databases
|
|
68
|
+
- **ClickHouse** - Support for analytical workloads and columnar storage
|
|
69
|
+
|
|
70
|
+
### 🔍 Database Exploration
|
|
71
|
+
- **List schemas** - View all schemas in the database
|
|
72
|
+
- **List tables** - See all tables with metadata (size, row counts, comments)
|
|
73
|
+
- **Describe tables** - Get detailed column information, indexes, and constraints
|
|
74
|
+
- **View relationships** - Understand foreign key relationships between tables
|
|
75
|
+
|
|
76
|
+
### 📊 Data Analysis
|
|
77
|
+
- **Column profiling** - Statistical analysis of column data
|
|
78
|
+
- Basic statistics (count, unique values, nulls)
|
|
79
|
+
- Numeric statistics (mean, median, std dev, quartiles)
|
|
80
|
+
- Value frequency distribution
|
|
81
|
+
- Cardinality analysis
|
|
82
|
+
- **Data sampling** - Preview table data with configurable limits
|
|
83
|
+
- **Custom queries** - Execute read-only SQL queries safely
|
|
84
|
+
- **Database profiling** - Get high-level database metrics and largest tables
|
|
85
|
+
|
|
86
|
+
### 🔒 Safety Features
|
|
87
|
+
- **Read-only enforcement** - All connections are read-only at multiple levels
|
|
88
|
+
- **Query validation** - Only SELECT and WITH queries are allowed
|
|
89
|
+
- **Automatic limits** - Queries are automatically limited to prevent large result sets
|
|
90
|
+
- **Connection string safety** - Automatically adds read-only parameters
|
|
91
|
+
- **Database-specific safety** - Each adapter implements appropriate safety measures
|
|
92
|
+
|
|
93
|
+
## Installation
|
|
94
|
+
|
|
95
|
+
### Prerequisites
|
|
96
|
+
- **Python 3.10 or higher**
|
|
97
|
+
- **A database**: PostgreSQL (9.6+), MySQL/MariaDB (5.7+/10.2+), or ClickHouse
|
|
98
|
+
|
|
99
|
+
### Install via pip
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
pip install db-connect-mcp
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
That's it! The package is now ready to use.
|
|
106
|
+
|
|
107
|
+
> **For developers**: See [Development Guide](docs/DEVELOPMENT.md) for setting up a development environment.
|
|
108
|
+
|
|
109
|
+
## Configuration
|
|
110
|
+
|
|
111
|
+
Create a `.env` file with your database connection string:
|
|
112
|
+
|
|
113
|
+
```env
|
|
114
|
+
DATABASE_URL=your_database_connection_string_here
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
The server automatically detects the database type and adds appropriate read-only parameters.
|
|
118
|
+
|
|
119
|
+
### Connection String Examples
|
|
120
|
+
|
|
121
|
+
The server now provides more flexible and secure URL handling:
|
|
122
|
+
- **Automatic driver detection**: Async drivers are automatically added if not specified
|
|
123
|
+
- **JDBC URL support**: JDBC prefixes are automatically handled
|
|
124
|
+
- `jdbc:postgresql://...` → `postgresql+asyncpg://...`
|
|
125
|
+
- `jdbc:mysql://...` → `mysql+aiomysql://...`
|
|
126
|
+
- Works with all dialect variations (e.g., `jdbc:postgres://`, `jdbc:mariadb://`)
|
|
127
|
+
- **Database dialect variations**: Common variations are automatically normalized
|
|
128
|
+
- PostgreSQL: `postgresql`, `postgres`, `pg`, `psql`, `pgsql`
|
|
129
|
+
- MySQL/MariaDB: `mysql`, `mariadb`, `maria`
|
|
130
|
+
- ClickHouse: `clickhouse`, `ch`, `click`
|
|
131
|
+
- **Allowlist-based parameter filtering**: Only known-safe parameters are preserved
|
|
132
|
+
- **Database-specific parameters**: Each database type has its own set of supported parameters
|
|
133
|
+
- **Robust parsing**: Handles various URL formats gracefully
|
|
134
|
+
|
|
135
|
+
**PostgreSQL:**
|
|
136
|
+
```
|
|
137
|
+
# Simple URL (driver automatically added)
|
|
138
|
+
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
|
|
139
|
+
|
|
140
|
+
# Common variations (all normalized to postgresql+asyncpg)
|
|
141
|
+
DATABASE_URL=postgres://user:pass@host:5432/db # Heroku, AWS RDS style
|
|
142
|
+
DATABASE_URL=pg://user:pass@host:5432/db # Short form
|
|
143
|
+
DATABASE_URL=psql://user:pass@host:5432/db # CLI style
|
|
144
|
+
|
|
145
|
+
# JDBC URLs (automatically converted)
|
|
146
|
+
DATABASE_URL=jdbc:postgresql://user:pass@host:5432/db # From Java apps
|
|
147
|
+
DATABASE_URL=jdbc:postgres://user:pass@host:5432/db # JDBC with variant
|
|
148
|
+
|
|
149
|
+
# With explicit async driver
|
|
150
|
+
DATABASE_URL=postgresql+asyncpg://user:pass@host:5432/db
|
|
151
|
+
|
|
152
|
+
# With supported parameters (see list below)
|
|
153
|
+
DATABASE_URL=postgres://user:pass@host:5432/db?application_name=myapp&connect_timeout=10
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
**Supported PostgreSQL Parameters:**
|
|
157
|
+
- `application_name` - Identifies your app in pg_stat_activity (useful for monitoring)
|
|
158
|
+
- `connect_timeout` - Connection timeout in seconds
|
|
159
|
+
- `command_timeout` - Default timeout for operations
|
|
160
|
+
- `ssl` / `sslmode` - SSL connection requirements (automatically converted for asyncpg compatibility)
|
|
161
|
+
- `server_settings` - Server settings dictionary
|
|
162
|
+
- `options` - Command-line options to send to server
|
|
163
|
+
- Performance tuning: `prepared_statement_cache_size`, `max_cached_statement_lifetime`, etc.
|
|
164
|
+
|
|
165
|
+
**MySQL/MariaDB:**
|
|
166
|
+
```
|
|
167
|
+
# Simple URL (driver automatically added)
|
|
168
|
+
DATABASE_URL=mysql://root:password@localhost:3306/mydb
|
|
169
|
+
|
|
170
|
+
# MariaDB URLs (normalized to mysql+aiomysql)
|
|
171
|
+
DATABASE_URL=mariadb://user:pass@host:3306/db # MariaDB style
|
|
172
|
+
DATABASE_URL=maria://user:pass@host:3306/db # Short form
|
|
173
|
+
|
|
174
|
+
# JDBC URLs (automatically converted)
|
|
175
|
+
DATABASE_URL=jdbc:mysql://user:pass@host:3306/db # From Java apps
|
|
176
|
+
DATABASE_URL=jdbc:mariadb://user:pass@host:3306/db # JDBC MariaDB
|
|
177
|
+
|
|
178
|
+
# With explicit async driver
|
|
179
|
+
DATABASE_URL=mysql+aiomysql://user:pass@host:3306/db
|
|
180
|
+
|
|
181
|
+
# With charset (critical for proper Unicode support)
|
|
182
|
+
DATABASE_URL=mariadb://user:pass@remote.host:3306/db?charset=utf8mb4
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
**Supported MySQL Parameters:**
|
|
186
|
+
- `charset` - Character encoding (e.g., utf8mb4) - **critical for data integrity**
|
|
187
|
+
- `use_unicode` - Enable Unicode support
|
|
188
|
+
- `connect_timeout`, `read_timeout`, `write_timeout` - Various timeouts
|
|
189
|
+
- `autocommit` - Transaction autocommit mode
|
|
190
|
+
- `init_command` - Initial SQL command to run
|
|
191
|
+
- `sql_mode` - SQL mode settings
|
|
192
|
+
- `time_zone` - Time zone setting
|
|
193
|
+
|
|
194
|
+
**ClickHouse:**
|
|
195
|
+
```
|
|
196
|
+
# Simple URL (driver automatically added)
|
|
197
|
+
DATABASE_URL=clickhouse://default:@localhost:9000/default
|
|
198
|
+
|
|
199
|
+
# Short forms (normalized to clickhouse+asynch)
|
|
200
|
+
DATABASE_URL=ch://user:pass@host:9000/db # Short form
|
|
201
|
+
DATABASE_URL=click://user:pass@host:9000/db # Alternative
|
|
202
|
+
|
|
203
|
+
# JDBC URLs (automatically converted)
|
|
204
|
+
DATABASE_URL=jdbc:clickhouse://user:pass@host:9000/db # From Java apps
|
|
205
|
+
DATABASE_URL=jdbc:ch://user:pass@host:9000/db # JDBC with short form
|
|
206
|
+
|
|
207
|
+
# With explicit async driver
|
|
208
|
+
DATABASE_URL=clickhouse+asynch://user:pass@host:9000/db
|
|
209
|
+
|
|
210
|
+
# With performance settings
|
|
211
|
+
DATABASE_URL=ch://user:pass@host:9000/db?timeout=60&max_threads=4
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
**Supported ClickHouse Parameters:**
|
|
215
|
+
- `database` - Default database selection
|
|
216
|
+
- `timeout`, `connect_timeout`, `send_receive_timeout` - Various timeouts
|
|
217
|
+
- `compress`, `compression` - Enable compression
|
|
218
|
+
- `max_block_size`, `max_threads` - Performance tuning
|
|
219
|
+
|
|
220
|
+
**Note:**
|
|
221
|
+
- SSL parameters (`ssl`, `sslmode`) are automatically converted to the correct format for asyncpg
|
|
222
|
+
- Certificate file parameters (`sslcert`, `sslkey`, `sslrootcert`) are filtered out as they can cause compatibility issues
|
|
223
|
+
- Only parameters known to work with async drivers are preserved
|
|
224
|
+
|
|
225
|
+
## Usage
|
|
226
|
+
|
|
227
|
+
### Running the Server
|
|
228
|
+
|
|
229
|
+
```bash
|
|
230
|
+
# Run the server (works everywhere, no PATH configuration needed)
|
|
231
|
+
python -m db_connect_mcp
|
|
232
|
+
|
|
233
|
+
# With environment variable
|
|
234
|
+
DATABASE_URL="postgresql://user:pass@host:5432/db" python -m db_connect_mcp
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
> **Note**: Using `python -m db_connect_mcp` works regardless of whether Python's Scripts directory is in your PATH.
|
|
238
|
+
|
|
239
|
+
### Using with Claude Desktop
|
|
240
|
+
|
|
241
|
+
Add the server to your Claude Desktop configuration (`claude_desktop_config.json`):
|
|
242
|
+
|
|
243
|
+
```json
|
|
244
|
+
{
|
|
245
|
+
"mcpServers": {
|
|
246
|
+
"db-connect": {
|
|
247
|
+
"command": "python",
|
|
248
|
+
"args": ["-m", "db_connect_mcp"],
|
|
249
|
+
"env": {
|
|
250
|
+
"DATABASE_URL": "postgresql+asyncpg://user:pass@host:5432/db"
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
**Multiple database connections:**
|
|
258
|
+
|
|
259
|
+
```json
|
|
260
|
+
{
|
|
261
|
+
"mcpServers": {
|
|
262
|
+
"postgres-prod": {
|
|
263
|
+
"command": "python",
|
|
264
|
+
"args": ["-m", "db_connect_mcp"],
|
|
265
|
+
"env": {
|
|
266
|
+
"DATABASE_URL": "postgresql+asyncpg://user:pass@pg-host:5432/db"
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
"mysql-analytics": {
|
|
270
|
+
"command": "python",
|
|
271
|
+
"args": ["-m", "db_connect_mcp"],
|
|
272
|
+
"env": {
|
|
273
|
+
"DATABASE_URL": "mysql+aiomysql://user:pass@mysql-host:3306/analytics"
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
> **For development**: See [Development Guide](docs/DEVELOPMENT.md) for running from source with uv.
|
|
281
|
+
|
|
282
|
+
## Database Feature Support
|
|
283
|
+
|
|
284
|
+
| Feature | PostgreSQL | MySQL | ClickHouse |
|
|
285
|
+
|---------|------------|-------|------------|
|
|
286
|
+
| Schemas | ✅ Full | ✅ Full | ✅ Full |
|
|
287
|
+
| Tables | ✅ Full | ✅ Full | ✅ Full |
|
|
288
|
+
| Views | ✅ Full | ✅ Full | ✅ Full |
|
|
289
|
+
| Indexes | ✅ Full | ✅ Full | ⚠️ Limited |
|
|
290
|
+
| Foreign Keys | ✅ Full | ✅ Full | ❌ No |
|
|
291
|
+
| Constraints | ✅ Full | ✅ Full | ⚠️ Limited |
|
|
292
|
+
| Table Size | ✅ Exact | ✅ Exact | ✅ Exact |
|
|
293
|
+
| Row Count | ✅ Exact | ✅ Exact | ✅ Exact |
|
|
294
|
+
| Column Stats | ✅ Full | ✅ Full | ✅ Full |
|
|
295
|
+
| Sampling | ✅ Full | ✅ Full | ✅ Full |
|
|
296
|
+
|
|
297
|
+
## Available Tools
|
|
298
|
+
|
|
299
|
+
### list_schemas
|
|
300
|
+
List all schemas in the database.
|
|
301
|
+
|
|
302
|
+
### list_tables
|
|
303
|
+
List all tables in a schema with metadata.
|
|
304
|
+
- Parameters:
|
|
305
|
+
- `schema` (optional): Schema name (default: "public")
|
|
306
|
+
|
|
307
|
+
### describe_table
|
|
308
|
+
Get detailed information about a table.
|
|
309
|
+
- Parameters:
|
|
310
|
+
- `table_name`: Name of the table
|
|
311
|
+
- `schema` (optional): Schema name (default: "public")
|
|
312
|
+
|
|
313
|
+
### analyze_column
|
|
314
|
+
Analyze a column with statistics and distribution.
|
|
315
|
+
- Parameters:
|
|
316
|
+
- `table_name`: Name of the table
|
|
317
|
+
- `column_name`: Name of the column
|
|
318
|
+
- `schema` (optional): Schema name (default: "public")
|
|
319
|
+
|
|
320
|
+
### sample_data
|
|
321
|
+
Get a sample of data from a table.
|
|
322
|
+
- Parameters:
|
|
323
|
+
- `table_name`: Name of the table
|
|
324
|
+
- `schema` (optional): Schema name (default: "public")
|
|
325
|
+
- `limit` (optional): Number of rows (default: 100, max: 1000)
|
|
326
|
+
|
|
327
|
+
### execute_query
|
|
328
|
+
Execute a read-only SQL query.
|
|
329
|
+
- Parameters:
|
|
330
|
+
- `query`: SQL query (must be SELECT or WITH)
|
|
331
|
+
- `limit` (optional): Maximum rows (default: 1000, max: 10000)
|
|
332
|
+
|
|
333
|
+
### get_table_relationships
|
|
334
|
+
Get foreign key relationships in a schema.
|
|
335
|
+
- Parameters:
|
|
336
|
+
- `schema` (optional): Schema name (default: "public")
|
|
337
|
+
|
|
338
|
+
### profile_database
|
|
339
|
+
Get a high-level profile of the entire database.
|
|
340
|
+
|
|
341
|
+
## Example Usage in Claude
|
|
342
|
+
|
|
343
|
+
Once configured, you can use the server in Claude:
|
|
344
|
+
|
|
345
|
+
```
|
|
346
|
+
"Can you analyze my database and tell me about the table structure?"
|
|
347
|
+
|
|
348
|
+
"Show me the relationships between tables in the public schema"
|
|
349
|
+
|
|
350
|
+
"What's the distribution of values in the users.created_at column?"
|
|
351
|
+
|
|
352
|
+
"Give me a sample of data from the orders table"
|
|
353
|
+
|
|
354
|
+
"Run this query: SELECT COUNT(*) FROM users WHERE created_at > '2024-01-01'"
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
### Database-Specific Examples
|
|
358
|
+
|
|
359
|
+
**Working with PostgreSQL:**
|
|
360
|
+
```
|
|
361
|
+
"List all schemas except system ones"
|
|
362
|
+
"Show me the foreign key relationships in the sales schema"
|
|
363
|
+
"Analyze the performance of indexes on the products table"
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
**Working with MySQL:**
|
|
367
|
+
```
|
|
368
|
+
"What storage engines are being used in my database?"
|
|
369
|
+
"Show me all tables in the information_schema"
|
|
370
|
+
"Analyze the customer_orders table structure"
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
**Working with ClickHouse:**
|
|
374
|
+
```
|
|
375
|
+
"Show me the partitions for the events table"
|
|
376
|
+
"What's the compression ratio for the analytics.clicks table?"
|
|
377
|
+
"Sample 1000 rows from the metrics table"
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
## Safety and Security
|
|
381
|
+
|
|
382
|
+
- **Read-only by design**: The server enforces read-only access at multiple levels:
|
|
383
|
+
- Connection string parameters
|
|
384
|
+
- Session-level settings
|
|
385
|
+
- Query validation
|
|
386
|
+
|
|
387
|
+
- **No data modification**: INSERT, UPDATE, DELETE, CREATE, DROP, and other modification statements are blocked
|
|
388
|
+
|
|
389
|
+
- **Query limits**: All queries are automatically limited to prevent excessive resource usage
|
|
390
|
+
|
|
391
|
+
- **No sensitive operations**: No access to system catalogs or administrative functions
|
|
392
|
+
|
|
393
|
+
## Development
|
|
394
|
+
|
|
395
|
+
For detailed development setup, testing, and contribution guidelines, see the [Development Guide](docs/DEVELOPMENT.md).
|
|
396
|
+
|
|
397
|
+
### Project Structure
|
|
398
|
+
```
|
|
399
|
+
db-connect-mcp/
|
|
400
|
+
├── src/
|
|
401
|
+
│ └── db_connect_mcp/
|
|
402
|
+
│ ├── adapters/ # Database-specific adapters
|
|
403
|
+
│ │ ├── __init__.py
|
|
404
|
+
│ │ ├── base.py # Base adapter interface
|
|
405
|
+
│ │ ├── postgresql.py # PostgreSQL adapter
|
|
406
|
+
│ │ ├── mysql.py # MySQL adapter
|
|
407
|
+
│ │ └── clickhouse.py # ClickHouse adapter
|
|
408
|
+
│ ├── core/ # Core functionality
|
|
409
|
+
│ │ ├── __init__.py
|
|
410
|
+
│ │ ├── connection.py # Database connection management
|
|
411
|
+
│ │ ├── executor.py # Query execution
|
|
412
|
+
│ │ ├── inspector.py # Metadata inspection
|
|
413
|
+
│ │ └── analyzer.py # Statistical analysis
|
|
414
|
+
│ ├── models/ # Data models
|
|
415
|
+
│ │ ├── __init__.py
|
|
416
|
+
│ │ ├── capabilities.py # Database capabilities
|
|
417
|
+
│ │ ├── config.py # Configuration models
|
|
418
|
+
│ │ ├── database.py # Database models
|
|
419
|
+
│ │ ├── query.py # Query models
|
|
420
|
+
│ │ ├── statistics.py # Statistics models
|
|
421
|
+
│ │ └── table.py # Table metadata models
|
|
422
|
+
│ ├── __init__.py
|
|
423
|
+
│ ├── __main__.py # Module entry point
|
|
424
|
+
│ └── server.py # Main MCP server implementation
|
|
425
|
+
├── tests/
|
|
426
|
+
│ ├── conftest.py # Test configuration
|
|
427
|
+
│ └── test_server.py # Integration tests
|
|
428
|
+
├── .env.example # Example environment configuration
|
|
429
|
+
├── main.py # Legacy entry point (optional)
|
|
430
|
+
├── pyproject.toml # Project dependencies and console scripts
|
|
431
|
+
└── README.md # This file
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
### Architecture
|
|
435
|
+
|
|
436
|
+
The server uses an adapter pattern to support multiple database systems:
|
|
437
|
+
|
|
438
|
+
- **Adapters**: Each database type has its own adapter that implements database-specific functionality
|
|
439
|
+
- **Core**: Shared functionality for connection management, query execution, and metadata inspection
|
|
440
|
+
- **Models**: Pydantic models for type safety and validation
|
|
441
|
+
- **Server**: MCP server implementation that routes requests to appropriate components
|
|
442
|
+
|
|
443
|
+
### Running Tests
|
|
444
|
+
|
|
445
|
+
See the [Development Guide](docs/DEVELOPMENT.md#running-tests) and [Test Guide](tests/README.md) for detailed testing instructions.
|
|
446
|
+
|
|
447
|
+
## Troubleshooting
|
|
448
|
+
|
|
449
|
+
### Connection Issues
|
|
450
|
+
- Verify your DATABASE_URL is correct and includes the appropriate driver
|
|
451
|
+
- Check network connectivity to the database
|
|
452
|
+
- Ensure the database user has appropriate read permissions
|
|
453
|
+
- For PostgreSQL: Check if SSL is required (`?ssl=require`)
|
|
454
|
+
- For MySQL: Verify charset settings (`?charset=utf8mb4`)
|
|
455
|
+
- For ClickHouse: Check port (default is 9000 for native, 8123 for HTTP)
|
|
456
|
+
|
|
457
|
+
### Database-Specific Issues
|
|
458
|
+
|
|
459
|
+
**PostgreSQL:**
|
|
460
|
+
- Ensure `asyncpg` driver is specified for async operations
|
|
461
|
+
- SSL certificates may be required for cloud databases
|
|
462
|
+
|
|
463
|
+
**MySQL/MariaDB:**
|
|
464
|
+
- Use `aiomysql` driver for async support
|
|
465
|
+
- Check MySQL version compatibility (5.7+ or MariaDB 10.2+)
|
|
466
|
+
- Verify charset and collation settings
|
|
467
|
+
|
|
468
|
+
**ClickHouse:**
|
|
469
|
+
- Use `asynch` driver for async operations
|
|
470
|
+
- Note that ClickHouse has limited support for foreign keys and constraints
|
|
471
|
+
- Some statistical functions may not be available
|
|
472
|
+
|
|
473
|
+
### Permission Errors
|
|
474
|
+
- The database user needs at least SELECT permissions on the schemas/tables you want to analyze
|
|
475
|
+
- Some statistical functions may require additional permissions
|
|
476
|
+
- ClickHouse may require specific permissions for system tables
|
|
477
|
+
|
|
478
|
+
### Large Result Sets
|
|
479
|
+
- Use the `limit` parameter to control result size
|
|
480
|
+
- The server automatically limits results to prevent memory issues
|
|
481
|
+
- For large analyses, consider using more specific queries
|
|
482
|
+
|
|
483
|
+
## Contributing
|
|
484
|
+
|
|
485
|
+
Contributions are welcome! The server is designed to be read-only and safe by default. Any new features should maintain these safety guarantees.
|
|
486
|
+
|
|
487
|
+
## License
|
|
488
|
+
|
|
489
|
+
MIT License - See LICENSE file for details
|