@aranshayan/mcp-database 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +136 -2
- package/dist/index.js +1213 -0
- package/package.json +63 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 aranshayan
|
|
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
CHANGED
|
@@ -1,3 +1,137 @@
|
|
|
1
|
-
#
|
|
1
|
+
# mcp-database
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/aranshayan/mcp-database/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@aranshayan/mcp-database)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
A **safe-by-default [Model Context Protocol](https://modelcontextprotocol.io) server for PostgreSQL, MySQL/MariaDB and SQLite.**
|
|
8
|
+
Let Claude (or any MCP client) explore your schema and answer questions from your data — without handing it a loaded gun.
|
|
9
|
+
|
|
10
|
+
- **Read-only by default.** Writes need an explicit `--read-write` flag, and the `execute` tool does not even exist until you pass it.
|
|
11
|
+
- **Defence in depth.** Every query is checked by a SQL guard *and* runs inside a read-only transaction / read-only connection, so a guard bypass still cannot write.
|
|
12
|
+
- **Built for LLMs.** Schema discovery tools, parameterised queries, row caps, statement timeouts, compact JSON output, errors that explain what to do next.
|
|
13
|
+
- **Zero native dependencies.** SQLite uses Node's built-in `node:sqlite`; Postgres and MySQL use pure-JS drivers. `npx` just works.
|
|
14
|
+
- **One server, three engines.** The connection string decides the dialect.
|
|
15
|
+
|
|
16
|
+
> **Only PostgreSQL, with personal data in it?** [pg-guard-mcp](https://github.com/aranshayan/pg-guard-mcp) is the stricter sibling: Postgres only, never writes, masks the columns you mark as personal data and keeps an append-only audit log.
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
19
|
+
|
|
20
|
+
Use a database account that can only `SELECT` (see [Create a read-only role](#create-a-read-only-role)).
|
|
21
|
+
|
|
22
|
+
**Claude Desktop** — `claude_desktop_config.json`:
|
|
23
|
+
|
|
24
|
+
```json
|
|
25
|
+
{
|
|
26
|
+
"mcpServers": {
|
|
27
|
+
"database": {
|
|
28
|
+
"command": "npx",
|
|
29
|
+
"args": ["-y", "@aranshayan/mcp-database"],
|
|
30
|
+
"env": { "DATABASE_URL": "postgres://readonly:secret@localhost:5432/mydb" }
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
**Claude Code:**
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
claude mcp add database --env DATABASE_URL=postgres://readonly:secret@localhost:5432/mydb -- npx -y @aranshayan/mcp-database
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**Cursor / VS Code / any MCP client:** run `npx -y @aranshayan/mcp-database` as a stdio server with `DATABASE_URL` in its environment.
|
|
43
|
+
|
|
44
|
+
| Engine | `DATABASE_URL` |
|
|
45
|
+
| --- | --- |
|
|
46
|
+
| PostgreSQL | `postgres://user:pass@host:5432/db` |
|
|
47
|
+
| MySQL / MariaDB | `mysql://user:pass@host:3306/db` |
|
|
48
|
+
| SQLite | `sqlite:///absolute/path/app.db` or just `./app.db` |
|
|
49
|
+
|
|
50
|
+
Requires **Node.js 22.13+**.
|
|
51
|
+
|
|
52
|
+
## Tools
|
|
53
|
+
|
|
54
|
+
| Tool | What it does |
|
|
55
|
+
| --- | --- |
|
|
56
|
+
| `list_schemas` | Schemas / databases on the connection. |
|
|
57
|
+
| `list_tables` | Tables and views with type and estimated row count. |
|
|
58
|
+
| `describe_table` | Columns (type, nullability, default, primary key, comment), indexes, foreign keys. |
|
|
59
|
+
| `sample_rows` | Preview the first 1–100 rows of a table. |
|
|
60
|
+
| `query` | Run **one** read-only statement (`SELECT` / `WITH` / `VALUES` / `SHOW` / `EXPLAIN`) with `params`. |
|
|
61
|
+
| `execute` | *Only with `--read-write`.* Run **one** `INSERT` / `UPDATE` / `DELETE` in a transaction. |
|
|
62
|
+
|
|
63
|
+
All read tools are annotated `readOnlyHint`; `execute` is annotated `destructiveHint` so clients can ask for confirmation.
|
|
64
|
+
|
|
65
|
+
## Options
|
|
66
|
+
|
|
67
|
+
| Flag | Environment | Default | |
|
|
68
|
+
| --- | --- | --- | --- |
|
|
69
|
+
| *(url)* / `--url` | `DATABASE_URL` | — | Connection string. Prefer the environment variable: command-line arguments are visible in process lists. |
|
|
70
|
+
| `--read-write` | `MCP_DATABASE_READ_WRITE=1` | off | Enables `execute`. |
|
|
71
|
+
| `--allow-ddl` | `MCP_DATABASE_ALLOW_DDL=1` | off | With `--read-write`, also permit `CREATE` / `ALTER` / `DROP` / `TRUNCATE`. |
|
|
72
|
+
| `--max-rows <n>` | `MCP_DATABASE_MAX_ROWS` | 200 | Rows returned per query (max 5000). |
|
|
73
|
+
| `--timeout-ms <n>` | `MCP_DATABASE_TIMEOUT_MS` | 15000 | Statement timeout. |
|
|
74
|
+
|
|
75
|
+
## Safety model
|
|
76
|
+
|
|
77
|
+
Three independent layers, so no single bug is fatal:
|
|
78
|
+
|
|
79
|
+
1. **SQL guard** (`src/sql-guard.ts`). Comments, string literals and quoted identifiers are stripped per dialect (MySQL `#` and `--␠` comments, PostgreSQL dollar-quoting and nested comments, SQLite `[brackets]`…), then the statement must be a *single* statement of an allowed kind. Rejected outright: multiple statements, `COPY`, `CALL`, `SET`, `ATTACH`, `SELECT … INTO OUTFILE`, `LOAD_FILE()`, `pg_read_file()`, `load_extension()`, SQLite `PRAGMA` assignment, MySQL `/*! … */` executable comments, and more. `UPDATE` / `DELETE` without `WHERE` is refused in read-write mode.
|
|
80
|
+
2. **Engine-level read-only.**
|
|
81
|
+
- PostgreSQL: `BEGIN READ ONLY` + `SET LOCAL statement_timeout`, results streamed through a cursor.
|
|
82
|
+
- MySQL: `SET SESSION TRANSACTION READ ONLY` + `START TRANSACTION READ ONLY` + `MAX_EXECUTION_TIME`. (The session-level setting matters: MySQL DDL commits implicitly and would otherwise escape a plain read-only transaction. This is covered by a test.)
|
|
83
|
+
- SQLite: the file is opened `readOnly` and `PRAGMA query_only = ON`. Queries run in a separate process with its own read-only connection, which is killed when the statement timeout passes.
|
|
84
|
+
3. **Your database account.** The guard and read-only mode are defences, not a substitute for least privilege. Connect with a role that can only read what the model should see.
|
|
85
|
+
|
|
86
|
+
Errors are scrubbed of the connection string and password before they reach the model, and the server never logs to stdout (reserved for the protocol).
|
|
87
|
+
|
|
88
|
+
### Create a read-only role
|
|
89
|
+
|
|
90
|
+
```sql
|
|
91
|
+
-- PostgreSQL
|
|
92
|
+
CREATE ROLE mcp_readonly LOGIN PASSWORD 'change-me';
|
|
93
|
+
GRANT CONNECT ON DATABASE mydb TO mcp_readonly;
|
|
94
|
+
GRANT USAGE ON SCHEMA public TO mcp_readonly;
|
|
95
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_readonly;
|
|
96
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO mcp_readonly;
|
|
97
|
+
|
|
98
|
+
-- MySQL
|
|
99
|
+
CREATE USER 'mcp_readonly'@'%' IDENTIFIED BY 'change-me';
|
|
100
|
+
GRANT SELECT ON mydb.* TO 'mcp_readonly'@'%';
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Known limits
|
|
104
|
+
|
|
105
|
+
- **SQLite timeouts stop the query process.** `node:sqlite` cannot interrupt a statement, so `query` and `sample_rows` run in a child process that is killed (SIGKILL) at `--timeout-ms` and restarted for the next query. The schema tools and `execute` run in the server process without a timeout; they are short metadata reads or single writes. An in-memory database (`:memory:`) only exists inside the server process, so its queries have no timeout.
|
|
106
|
+
- **MySQL buffers a result set before the row cap is applied** (`mysql2` has no server-side cursor). Keep a `LIMIT` in large queries. `MAX_EXECUTION_TIME` bounds time.
|
|
107
|
+
- Only the first statement type is classified; the engine-level read-only layer is what ultimately prevents writes through exotic constructs.
|
|
108
|
+
- `EXPLAIN ANALYZE` actually executes the statement. It is classified by the statement it wraps, and in read-only mode a wrapped write is rejected.
|
|
109
|
+
|
|
110
|
+
## What is tested
|
|
111
|
+
|
|
112
|
+
`npm test` runs 100+ tests, including:
|
|
113
|
+
|
|
114
|
+
- the SQL guard against comment/string/dollar-quote smuggling tricks, per dialect;
|
|
115
|
+
- **SQLite** through `node:sqlite`, including a runaway recursive CTE that is stopped at the timeout and its process killed;
|
|
116
|
+
- **PostgreSQL** against a real Postgres engine ([PGlite](https://pglite.dev) behind a TCP socket) through the real `pg` driver, including cursors;
|
|
117
|
+
- **MySQL and MariaDB** against real servers. CI runs the suite on MySQL 8.4, MySQL 9 and MariaDB 11; locally, set `TEST_MYSQL_URL` to a scratch database (skipped otherwise);
|
|
118
|
+
- **PostgreSQL `statement_timeout` enforcement** against a real PostgreSQL 17 server in CI (`TEST_PG_URL` locally), including that the pool keeps working after a cancelled statement;
|
|
119
|
+
- the **built CLI over stdio**, exactly as an MCP client launches it.
|
|
120
|
+
|
|
121
|
+
MariaDB-specific code paths (`max_statement_time`, schemas without `STATISTICS.EXPRESSION`) now run in CI through the MariaDB 11 job.
|
|
122
|
+
|
|
123
|
+
## Development
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
npm install
|
|
127
|
+
npm test # unit + integration + e2e
|
|
128
|
+
npm run typecheck
|
|
129
|
+
npm run build # bundles to dist/index.js
|
|
130
|
+
|
|
131
|
+
# optional: also run the server suites against scratch databases
|
|
132
|
+
TEST_MYSQL_URL=mysql://user:pass@127.0.0.1:3306/scratch TEST_PG_URL=postgres://user:pass@127.0.0.1:5432/scratch npm test
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## License
|
|
136
|
+
|
|
137
|
+
[MIT](LICENSE)
|