mcp-server-motherduck 0.6.4__py3-none-any.whl → 1.0.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.
@@ -0,0 +1,130 @@
1
+ """
2
+ Switch database connection tool - Change the primary database connection.
3
+ """
4
+
5
+ import os
6
+ from typing import Any
7
+
8
+ DESCRIPTION = "Switch to a different database connection. The new connection respects the server's read-only/read-write mode."
9
+
10
+
11
+ def _is_local_file_path(path: str) -> bool:
12
+ """Check if path is a local file path (not :memory:, md:, s3://, etc.)."""
13
+ if path == ":memory:":
14
+ return False
15
+ if path.startswith("md:") or path.startswith("motherduck:"):
16
+ return False
17
+ if path.startswith("s3://"):
18
+ return False
19
+ return True
20
+
21
+
22
+ def _validate_path(path: str) -> str | None:
23
+ """
24
+ Validate database path.
25
+
26
+ Returns None if valid, or an error message if invalid.
27
+
28
+ Valid paths:
29
+ - :memory:
30
+ - md: or motherduck: paths
31
+ - s3:// paths
32
+ - Absolute local file paths
33
+
34
+ Invalid:
35
+ - Relative paths (must use absolute paths for local files)
36
+ """
37
+ # Special paths that are always valid
38
+ if not _is_local_file_path(path):
39
+ return None
40
+
41
+ # Local file paths must be absolute
42
+ if not os.path.isabs(path):
43
+ return f"Relative paths are not allowed. Please use an absolute path. Got: {path}"
44
+
45
+ return None
46
+
47
+
48
+ def switch_database_connection(
49
+ path: str,
50
+ db_client: Any,
51
+ server_read_only: bool = False,
52
+ create_if_not_exists: bool = False,
53
+ ) -> dict[str, Any]:
54
+ """
55
+ Switch to a different primary database.
56
+
57
+ Args:
58
+ path: Database path. For local files, must be an absolute path.
59
+ Also accepts :memory:, md:database_name, or s3:// paths.
60
+ db_client: DatabaseClient instance (injected by server)
61
+ server_read_only: Server's global read-only setting (injected by server)
62
+ create_if_not_exists: If True, create the database file if it doesn't exist.
63
+ Only allowed when server is in read-write mode.
64
+
65
+ Returns:
66
+ JSON-serializable dict with result
67
+ """
68
+ # Validate path
69
+ error = _validate_path(path)
70
+ if error:
71
+ return {
72
+ "success": False,
73
+ "error": error,
74
+ "errorType": "ValueError",
75
+ }
76
+
77
+ # For local file paths, check if file exists
78
+ if _is_local_file_path(path):
79
+ file_exists = os.path.exists(path)
80
+
81
+ if not file_exists:
82
+ if not create_if_not_exists:
83
+ return {
84
+ "success": False,
85
+ "error": f"Database file does not exist: {path}. Set create_if_not_exists=True to create a new database.",
86
+ "errorType": "FileNotFoundError",
87
+ }
88
+
89
+ if server_read_only:
90
+ return {
91
+ "success": False,
92
+ "error": "Cannot create new database file in read-only mode. The server must be started with --read-write to create databases.",
93
+ "errorType": "PermissionError",
94
+ }
95
+
96
+ # In-memory databases can't be read-only (DuckDB limitation)
97
+ if path == ":memory:":
98
+ effective_read_only = False
99
+ warning = (
100
+ "In-memory databases cannot be read-only (DuckDB limitation)"
101
+ if server_read_only
102
+ else None
103
+ )
104
+ else:
105
+ effective_read_only = server_read_only
106
+ warning = None
107
+
108
+ try:
109
+ previous_db = db_client.db_path
110
+ db_client.switch_database(path, effective_read_only)
111
+
112
+ result: dict[str, Any] = {
113
+ "success": True,
114
+ "message": f"Switched to database: {path}",
115
+ "previousDatabase": previous_db,
116
+ "currentDatabase": path,
117
+ "readOnly": effective_read_only,
118
+ }
119
+
120
+ if warning:
121
+ result["warning"] = warning
122
+
123
+ return result
124
+
125
+ except Exception as e:
126
+ return {
127
+ "success": False,
128
+ "error": str(e),
129
+ "errorType": type(e).__name__,
130
+ }
@@ -0,0 +1,225 @@
1
+ Metadata-Version: 2.4
2
+ Name: mcp-server-motherduck
3
+ Version: 1.0.0
4
+ Summary: A MCP server for MotherDuck and local DuckDB
5
+ Author-email: tdoehmen <till@motherduck.com>
6
+ License-File: LICENSE
7
+ Requires-Python: >=3.10
8
+ Requires-Dist: click>=8.1.8
9
+ Requires-Dist: duckdb==1.4.4
10
+ Requires-Dist: fastmcp<3,>=2.14
11
+ Requires-Dist: pytz>=2025.2
12
+ Provides-Extra: dev
13
+ Requires-Dist: pytest-asyncio>=0.24; extra == 'dev'
14
+ Requires-Dist: pytest>=8.0; extra == 'dev'
15
+ Requires-Dist: python-dotenv>=1.0; extra == 'dev'
16
+ Requires-Dist: ruff>=0.4.0; extra == 'dev'
17
+ Description-Content-Type: text/markdown
18
+
19
+ <p align="center">
20
+ <img src="src/mcp_server_motherduck/assets/duck_feet_square.png" alt="MotherDuck / DuckDB Local MCP Server" width="120">
21
+ </p>
22
+
23
+ <h1 align="center">DuckDB / MotherDuck Local MCP Server</h1>
24
+
25
+ <p align="center">
26
+ SQL analytics and data engineering for AI Assistants and IDEs.
27
+ </p>
28
+
29
+ ---
30
+
31
+ Connect AI assistants to your data using DuckDB's powerful analytical SQL engine. Supports connecting to local DuckDB files, in-memory databases, S3-hosted databases, and MotherDuck. Allows executing SQL read- and write-queries, browsing database catalogs, and switching between different database connections on-the-fly.
32
+
33
+ **Looking for a fully-managed remote MCP server for MotherDuck?** → [Go to the MotherDuck Remote MCP docs](https://motherduck.com/docs/sql-reference/mcp/)
34
+
35
+ ### Remote vs Local MCP
36
+
37
+ | | **[Remote MCP](https://motherduck.com/docs/sql-reference/mcp/)** | **Local MCP** (this repo) |
38
+ |---|---|---|
39
+ | **Hosting** | Hosted by MotherDuck | Runs locally/self-hosted |
40
+ | **Setup** | Zero-setup | Requires local installation |
41
+ | **Access** | Read-only | Read-write supported |
42
+ | **Local filesystem** | - | Query across local and remote databases, ingest data from / export data to local filesystem |
43
+
44
+ > 📝 **Migrating from v0.x?**
45
+ > - **Read-only by default**: The server now runs in read-only mode by default. Add `--read-write` to enable write access. See [Securing for Production](#securing-for-production).
46
+ > - **Default database changed**: `--db-path` default changed from `md:` to `:memory:`. Add `--db-path md:` explicitly for MotherDuck.
47
+ > - **MotherDuck read-only requires read-scaling token**: MotherDuck connections in read-only mode require a [read-scaling token](https://motherduck.com/docs/key-tasks/authenticating-and-connecting-to-motherduck/authenticating-to-motherduck/#read-scaling-tokens). Regular tokens require `--read-write`.
48
+
49
+ ## Quick Start
50
+
51
+ **Prerequisites**: Install `uv` via `pip install uv` or `brew install uv`
52
+
53
+ ### Connecting to In-Memory DuckDB (Dev Mode)
54
+
55
+ ```json
56
+ {
57
+ "mcpServers": {
58
+ "DuckDB (in-memory, r/w)": {
59
+ "command": "uvx",
60
+ "args": ["mcp-server-motherduck", "--db-path", ":memory:", "--read-write", "--allow-switch-databases"]
61
+ }
62
+ }
63
+ }
64
+ ```
65
+
66
+ Full flexibility with no guardrails — read-write access and the ability to switch to any database (local files, S3, or MotherDuck) at runtime.
67
+
68
+ ### Connecting to a Local DuckDB File in Read-Only Mode
69
+
70
+ ```json
71
+ {
72
+ "mcpServers": {
73
+ "DuckDB (read-only)": {
74
+ "command": "uvx",
75
+ "args": ["mcp-server-motherduck", "--db-path", "/absolute/path/to/your.duckdb"]
76
+ }
77
+ }
78
+ }
79
+ ```
80
+
81
+ Connects to a specific DuckDB file in read-only mode. Won't hold on to the file lock, so convenient to use alongside a write connection to the same DuckDB file. You can also connect to remote DuckDB files on S3 using `s3://bucket/path.duckdb` — see [Environment Variables](#environment-variables) for S3 authentication. If you're considering third-party access to the MCP, see [Securing for Production](#securing-for-production).
82
+
83
+ ### Connecting to MotherDuck in Read-Write Mode
84
+
85
+ ```json
86
+ {
87
+ "mcpServers": {
88
+ "MotherDuck (local, r/w)": {
89
+ "command": "uvx",
90
+ "args": ["mcp-server-motherduck", "--db-path", "md:", "--read-write"],
91
+ "env": {
92
+ "motherduck_token": "<YOUR_MOTHERDUCK_TOKEN>"
93
+ }
94
+ }
95
+ }
96
+ }
97
+ ```
98
+
99
+ See [Command Line Parameters](#command-line-parameters) for more options, [Securing for Production](#securing-for-production) for deployment guidance, and [Troubleshooting](#troubleshooting) if you encounter issues.
100
+
101
+ ## Client Setup
102
+
103
+ | Client | Config Location | One-Click Install |
104
+ |--------|-----------------|-------------------|
105
+ | **Claude Desktop** | Settings → Developer → Edit Config | [MCPB package](https://github.com/motherduckdb/mcp-server-motherduck/releases) |
106
+ | **Claude Code** | Use CLI commands below | - |
107
+ | **Cursor** | Settings → MCP → Add new global MCP server | [<img src="https://cursor.com/deeplink/mcp-install-dark.svg" alt="Install in Cursor" height="20">](https://cursor.com/en/install-mcp?name=DuckDB&config=eyJjb21tYW5kIjoidXZ4IG1jcC1zZXJ2ZXItbW90aGVyZHVjayAtLWRiLXBhdGggOm1lbW9yeTogLS1yZWFkLXdyaXRlIC0tYWxsb3ctc3dpdGNoLWRhdGFiYXNlcyIsImVudiI6e319) |
108
+ | **VS Code** | `Ctrl+Shift+P` → "Preferences: Open User Settings (JSON)" | [![Install with UV in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square)](https://insiders.vscode.dev/redirect/mcp/install?name=mcp-server-motherduck&config=%7B%22command%22%3A%22uvx%22%2C%22args%22%3A%5B%22mcp-server-motherduck%22%2C%22--db-path%22%2C%22%3Amemory%3A%22%2C%22--read-write%22%2C%22--allow-switch-databases%22%5D%7D) |
109
+
110
+ Any MCP-compatible client can use this server. Add the JSON configuration from [Quick Start](#quick-start) to your client's MCP config file. Consult your client's documentation for the config file location.
111
+
112
+ <details>
113
+ <summary><b>Claude Code CLI commands</b></summary>
114
+
115
+ **In-Memory DuckDB (Dev Mode):**
116
+ ```bash
117
+ claude mcp add duckdb --transport stdio -- uvx mcp-server-motherduck --db-path :memory: --read-write --allow-switch-databases
118
+ ```
119
+
120
+ **Local DuckDB (Read-Only):**
121
+ ```bash
122
+ claude mcp add duckdb --transport stdio -- uvx mcp-server-motherduck --db-path /absolute/path/to/db.duckdb
123
+ ```
124
+
125
+ **MotherDuck (Read-Write):**
126
+ ```bash
127
+ claude mcp add motherduck --transport stdio --env motherduck_token=YOUR_TOKEN -- uvx mcp-server-motherduck --db-path md: --read-write
128
+ ```
129
+
130
+ </details>
131
+
132
+ ## Tools
133
+
134
+ | Tool | Description | Required Inputs | Optional Inputs |
135
+ |------|-------------|-----------------|-----------------|
136
+ | `execute_query` | Execute SQL query (DuckDB dialect) | `sql` | - |
137
+ | `list_databases` | List all databases (useful for MotherDuck or multiple attached DBs) | - | - |
138
+ | `list_tables` | List tables and views | - | `database`, `schema` |
139
+ | `list_columns` | List columns of a table/view | `table` | `database`, `schema` |
140
+ | `switch_database_connection`* | Switch to different database | `path` | `create_if_not_exists` |
141
+
142
+ *Requires `--allow-switch-databases` flag
143
+
144
+ All tools return JSON. Results are limited to 1024 rows / 50,000 chars by default (configurable via `--max-rows`, `--max-chars`).
145
+
146
+ ## Securing for Production
147
+
148
+ When giving third parties access to a self-hosted MCP server, **read-only mode alone is not sufficient** — it still allows access to the local filesystem, changing DuckDB settings, and other potentially sensitive operations.
149
+
150
+ For production deployments with third-party access, we recommend **[MotherDuck Remote MCP](https://motherduck.com/docs/sql-reference/mcp/)** — zero-setup, read-only, and hosted by MotherDuck.
151
+
152
+ **Self-hosting MotherDuck MCP:** Fork this repo and customize as needed. Use a **[service account](https://motherduck.com/docs/key-tasks/service-accounts-guide/)** with **[read-scaling tokens](https://motherduck.com/docs/key-tasks/authenticating-and-connecting-to-motherduck/read-scaling/#creating-a-read-scaling-token)** and enable **[SaaS mode](https://motherduck.com/docs/key-tasks/authenticating-and-connecting-to-motherduck/authenticating-to-motherduck/#authentication-using-saas-mode)** to restrict local file access.
153
+
154
+ **Self-hosting DuckDB MCP:** Use `--init-sql` to apply security settings. See the [Securing DuckDB guide](https://duckdb.org/docs/stable/operations_manual/securing_duckdb/overview) for available options.
155
+
156
+ ## Command Line Parameters
157
+
158
+ | Parameter | Default | Description |
159
+ |-----------|---------|-------------|
160
+ | `--db-path` | `:memory:` | Database path: local file (absolute), `md:` (MotherDuck), or `s3://` URL |
161
+ | `--motherduck-token` | `motherduck_token` env var | MotherDuck access token |
162
+ | `--read-write` | `False` | Enable write access |
163
+ | `--motherduck-saas-mode` | `False` | MotherDuck SaaS mode (restricts local access) |
164
+ | `--allow-switch-databases` | `False` | Enable `switch_database_connection` tool |
165
+ | `--max-rows` | `1024` | Max rows returned |
166
+ | `--max-chars` | `50000` | Max characters returned |
167
+ | `--query-timeout` | `-1` | Query timeout in seconds (-1 = disabled) |
168
+ | `--init-sql` | `None` | SQL to execute on startup |
169
+ | `--ephemeral-connections` | `True` | Use temporary connections for read-only local files |
170
+ | `--transport` | `stdio` | Transport type: `stdio` or `http` |
171
+ | `--port` | `8000` | Port for HTTP transport |
172
+ | `--host` | `127.0.0.1` | Host for HTTP transport |
173
+
174
+ ## Environment Variables
175
+
176
+ | Variable | Description |
177
+ |----------|-------------|
178
+ | `motherduck_token` or `MOTHERDUCK_TOKEN` | MotherDuck access token (alternative to `--motherduck-token`) |
179
+ | `HOME` | Used by DuckDB for extensions and config. Override with `--home-dir` if not set. |
180
+ | `AWS_ACCESS_KEY_ID` | AWS access key for S3 database connections |
181
+ | `AWS_SECRET_ACCESS_KEY` | AWS secret key for S3 database connections |
182
+ | `AWS_SESSION_TOKEN` | AWS session token for temporary credentials (IAM roles, SSO, EC2 instance profiles) |
183
+ | `AWS_DEFAULT_REGION` | AWS region for S3 connections |
184
+
185
+ ## Troubleshooting
186
+
187
+ - **`spawn uvx ENOENT`**: Specify full path to `uvx` (run `which uvx` to find it)
188
+ - **File locked**: Make sure `--ephemeral-connections` is turned on (default: true) and that you're not connected in read-write mode
189
+
190
+ ## Resources
191
+
192
+ - [MotherDuck MCP Documentation](https://motherduck.com/docs/sql-reference/mcp/)
193
+ - [Close the Loop: Faster Data Pipelines with MCP, DuckDB & AI (Blog)](https://motherduck.com/blog/faster-data-pipelines-with-mcp-duckdb-ai/)
194
+ - [Faster Data Pipelines with MCP and DuckDB (YouTube)](https://www.youtube.com/watch?v=yG1mv8ZRxcU)
195
+
196
+ ## Development
197
+
198
+ To run from source:
199
+
200
+ ```json
201
+ {
202
+ "mcpServers": {
203
+ "Local DuckDB (Dev)": {
204
+ "command": "uv",
205
+ "args": ["--directory", "/path/to/mcp-server-motherduck", "run", "mcp-server-motherduck", "--db-path", "md:"],
206
+ "env": {
207
+ "motherduck_token": "<YOUR_MOTHERDUCK_TOKEN>"
208
+ }
209
+ }
210
+ }
211
+ }
212
+ ```
213
+
214
+ ## Release Process
215
+
216
+ 1. Run the `Release New Version` GitHub Action
217
+ 2. Enter version in `MAJOR.MINOR.PATCH` format
218
+ 3. The workflow bumps version, publishes to PyPI/MCP registry, and creates the GitHub release with MCPB package
219
+
220
+ ## License
221
+
222
+ MIT License - see [LICENSE](LICENSE) file.
223
+
224
+ ##
225
+ mcp-name: io.github.motherduckdb/mcp-server-motherduck
@@ -0,0 +1,17 @@
1
+ mcp_server_motherduck/__init__.py,sha256=Kn6Bla9XiEOUs27xC2eGF6AqbqW2kl1U4NIfNcxNZ8I,8778
2
+ mcp_server_motherduck/configs.py,sha256=7dp4moLZ7hskacbC2ric7-kZgmz3X7bFTm4Hte07Erc,771
3
+ mcp_server_motherduck/database.py,sha256=vaFzGiQckwLnwSICAItHz0M7HUkE0rloxTIJ2Fw8ik0,17436
4
+ mcp_server_motherduck/instructions.py,sha256=U5PviYJ7bTxpr6geeqrdYnNgNQhDuQinL6tD-g9w4jk,6557
5
+ mcp_server_motherduck/server.py,sha256=DCTdhRqKqW8gFWtKzgyluTFhBL-AeI8APvco8MnDHSo,8719
6
+ mcp_server_motherduck/assets/duck_feet_square.png,sha256=P3j0ERFMeSyy67HA2wHxuDLNGhRcMd5LngLibtZ9TzA,6193
7
+ mcp_server_motherduck/tools/__init__.py,sha256=95JNMlxICn8w5O-mjMsqP7vVWsu45LYTn0AUtjR7W2A,466
8
+ mcp_server_motherduck/tools/execute_query.py,sha256=dcHECyZpr-85Ic8E-pfMM9oqfCF_ehW0OKRXkcyOEkE,563
9
+ mcp_server_motherduck/tools/list_columns.py,sha256=PLhhOddCaBwWEX-bX3Hg3heEHkLz8mRHhz4LIQwwYjo,2855
10
+ mcp_server_motherduck/tools/list_databases.py,sha256=xE-swUgttvllz81Jf_Ngdr_qJ_sBTd0OOaaObPhXVZ0,1665
11
+ mcp_server_motherduck/tools/list_tables.py,sha256=9rd4-tHXEOq3ezDdxpxPr9JpPTSft8enIj0Qc33oSMY,2540
12
+ mcp_server_motherduck/tools/switch_database_connection.py,sha256=Yuaj-oXZIfguNMjdKsGYagOlXooKe8B9H4U1G83HfIs,3880
13
+ mcp_server_motherduck-1.0.0.dist-info/METADATA,sha256=FosKIyVTwLXfk90KzTXJTPByKRlsg0gHblDx42QQv9c,10763
14
+ mcp_server_motherduck-1.0.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
15
+ mcp_server_motherduck-1.0.0.dist-info/entry_points.txt,sha256=dRTgcvWJn40bz0PVuKPylK6w92cFN32lwunZOgo5j4s,69
16
+ mcp_server_motherduck-1.0.0.dist-info/licenses/LICENSE,sha256=Tj68w9jCiceFKTvZ3jET-008NjhozcQMXpm-fyL9WUI,1067
17
+ mcp_server_motherduck-1.0.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.27.0
2
+ Generator: hatchling 1.28.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any