sql-safe-mcp 1.2.0__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.
- sql_safe_mcp-1.2.0/.gitignore +19 -0
- sql_safe_mcp-1.2.0/CHANGELOG.md +79 -0
- sql_safe_mcp-1.2.0/LICENSE +21 -0
- sql_safe_mcp-1.2.0/PKG-INFO +259 -0
- sql_safe_mcp-1.2.0/README.md +221 -0
- sql_safe_mcp-1.2.0/pyproject.toml +89 -0
- sql_safe_mcp-1.2.0/server.json +21 -0
- sql_safe_mcp-1.2.0/sql-safe-mcp.example.yaml +37 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/__init__.py +3 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/__main__.py +43 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/config.py +201 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/db/__init__.py +1 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/db/extras.py +35 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/db/mysql.py +63 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/db/reflection.py +132 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/db/registry.py +123 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/db/sqlserver.py +65 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/errors.py +49 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/mcp_server.py +140 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/models.py +124 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/security/__init__.py +1 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/security/dialect.py +29 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/security/executor.py +129 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/security/lineage.py +224 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/security/parser.py +183 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/security/pipeline.py +43 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/security/policy.py +101 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/security/reasons.py +43 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/security/schema.py +139 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/security/tokens.py +207 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/security/validated_query.py +209 -0
- sql_safe_mcp-1.2.0/src/sql_safe_mcp/service.py +325 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/.serena/
|
|
2
|
+
/*.agents.md
|
|
3
|
+
/.coverage
|
|
4
|
+
/.coverage.*
|
|
5
|
+
/coverage.xml
|
|
6
|
+
/htmlcov/
|
|
7
|
+
.venv/
|
|
8
|
+
__pycache__/
|
|
9
|
+
*.egg-info/
|
|
10
|
+
/dist/
|
|
11
|
+
/.pytest_cache/
|
|
12
|
+
/.ruff_cache/
|
|
13
|
+
.env
|
|
14
|
+
.env.*
|
|
15
|
+
/.hypothesis/
|
|
16
|
+
/.mutmut-cache
|
|
17
|
+
/mutants/
|
|
18
|
+
/.cbm-cache/
|
|
19
|
+
/.cbm-runtime/
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable externally observable changes to this project are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and uses the
|
|
6
|
+
categories `Added`, `Changed`, `Deprecated`, `Removed`, `Fixed`, and `Security`.
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [1.2.0] - 2026-09-20
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- MySQL and MariaDB support: `engine: mysql` or `engine: mariadb` with a `mysql+pymysql://` URL.
|
|
15
|
+
All seven tools work on them. MySQL and MariaDB have no schema level, so `schema` is always
|
|
16
|
+
`null` in responses and a `db.table` name is rejected as a cross-database reference.
|
|
17
|
+
- `execute_sql` and `pii_safe` on MySQL and MariaDB, with `LIMIT` in place of `TOP`. A `pii` rule
|
|
18
|
+
for these engines must not set `schema`. `TIME` results are returned as `time` values within a
|
|
19
|
+
day; longer values fail with `DATABASE_ERROR`.
|
|
20
|
+
- Stored procedures on MySQL and MariaDB report the body from `information_schema.ROUTINES`, which
|
|
21
|
+
is `null` when the login may not see it.
|
|
22
|
+
- `COUNT(*)` is accepted on the `mysql` dialect.
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
|
|
26
|
+
- **Breaking:** the project is renamed from `sql-mini-mcp` to `sql-safe-mcp`. The package, the
|
|
27
|
+
Python module (`sql_safe_mcp`), the command (`sql-safe-mcp`), the example configuration file, and
|
|
28
|
+
every environment variable (`SQL_MINI_MCP_CONFIG` is now `SQL_SAFE_MCP_CONFIG`) change; the old
|
|
29
|
+
names are not kept as aliases. The `sql-mini-mcp` package on PyPI becomes a deprecated shim that
|
|
30
|
+
depends on `sql-safe-mcp`.
|
|
31
|
+
- The `QUERY_REJECTED` reason for a non-integer row limit now reads
|
|
32
|
+
`TOP/LIMIT must be a non-negative integer literal`.
|
|
33
|
+
|
|
34
|
+
### Security
|
|
35
|
+
|
|
36
|
+
- MySQL and MariaDB sessions drop `NO_BACKSLASH_ESCAPES`, so the string escaping in the generated
|
|
37
|
+
SQL always means what the validated query means, even if the server enables that mode.
|
|
38
|
+
- The validation pipeline takes its SQL dialect only from the trusted server configuration and
|
|
39
|
+
requires it explicitly; policy, tokens, and the validated query are shared by every engine.
|
|
40
|
+
|
|
41
|
+
## [1.1.0] - 2026-09-20
|
|
42
|
+
|
|
43
|
+
### Changed
|
|
44
|
+
|
|
45
|
+
- `QUERY_REJECTED` errors from `execute_sql` now come from a fixed catalog of reasons. The wording
|
|
46
|
+
for unsupported syntax names the construct as `unsupported construct: <Node>` or
|
|
47
|
+
`unsupported option: <Node>.<argument>`; other reasons keep their earlier text.
|
|
48
|
+
|
|
49
|
+
### Security
|
|
50
|
+
|
|
51
|
+
- `execute_sql` no longer forwards comments from the caller's SQL to the database. The executed
|
|
52
|
+
statement is generated only from the validated query, so comment text can never become
|
|
53
|
+
executable text.
|
|
54
|
+
|
|
55
|
+
## [1.0.0] - 2026-09-20
|
|
56
|
+
|
|
57
|
+
### Added
|
|
58
|
+
|
|
59
|
+
- `execute_sql` tool for `pii_safe` SQL Server aliases. It runs one restricted `SELECT`, returns
|
|
60
|
+
configured PII columns as alias-bound tokens, accepts those tokens only in `=` and `IN`
|
|
61
|
+
predicates, and returns at most `max_rows` rows with a `truncated` flag. Servers with
|
|
62
|
+
`access_level: metadata` return `ACCESS_LEVEL_DENIED`.
|
|
63
|
+
|
|
64
|
+
## [0.9.1] - 2026-09-20
|
|
65
|
+
|
|
66
|
+
### Added
|
|
67
|
+
|
|
68
|
+
- Publishing of release-tag packages to PyPI and their metadata to the MCP Registry.
|
|
69
|
+
|
|
70
|
+
### Fixed
|
|
71
|
+
|
|
72
|
+
- Source distributions now include only release files, excluding local development caches.
|
|
73
|
+
|
|
74
|
+
## [0.9.0] - 2026-09-20
|
|
75
|
+
|
|
76
|
+
### Added
|
|
77
|
+
|
|
78
|
+
- Read-only SQL Server metadata tools for configured server aliases, databases, tables, and
|
|
79
|
+
stored procedures.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Anton Padapryhara
|
|
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,259 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: sql-safe-mcp
|
|
3
|
+
Version: 1.2.0
|
|
4
|
+
Summary: Minimal, read-only, PII-safe MCP server for SQL databases.
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2026 Anton Padapryhara
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Python: <3.15,>=3.12
|
|
28
|
+
Requires-Dist: anyio<5,>=4.8
|
|
29
|
+
Requires-Dist: cryptography<47,>=46.0.7
|
|
30
|
+
Requires-Dist: mcp<3,>=2
|
|
31
|
+
Requires-Dist: pydantic<3,>=2.10
|
|
32
|
+
Requires-Dist: pymysql>=1.2.3
|
|
33
|
+
Requires-Dist: pyodbc<6,>=5.2
|
|
34
|
+
Requires-Dist: pyyaml<7,>=6
|
|
35
|
+
Requires-Dist: sqlalchemy<3,>=2.0
|
|
36
|
+
Requires-Dist: sqlglot<31,>=30.18
|
|
37
|
+
Description-Content-Type: text/markdown
|
|
38
|
+
|
|
39
|
+
<div align="center">
|
|
40
|
+
|
|
41
|
+
# sql-safe-mcp
|
|
42
|
+
|
|
43
|
+
A read-only, PII-safe SQL Server, MySQL and MariaDB MCP server for coding agents: schema knowledge and safe queries, with no way to change or leak data.
|
|
44
|
+
|
|
45
|
+
[](https://github.com/proprock/sql-safe-mcp/actions/workflows/ci.yml)
|
|
46
|
+
[](https://github.com/proprock/sql-safe-mcp/releases)
|
|
47
|
+
[](https://pypi.org/project/sql-safe-mcp/)
|
|
48
|
+
[](https://www.python.org/downloads/)
|
|
49
|
+
[](LICENSE)
|
|
50
|
+
|
|
51
|
+
[](https://modelcontextprotocol.io)
|
|
52
|
+
[](server.json)
|
|
53
|
+
|
|
54
|
+
<img src="https://raw.githubusercontent.com/proprock/sql-safe-mcp/master/images/luna-guard.jpg" alt="Read the data. Protect the identity." width="760">
|
|
55
|
+
|
|
56
|
+
<i>Read the data. Protect the identity.</i>
|
|
57
|
+
|
|
58
|
+
</div>
|
|
59
|
+
|
|
60
|
+
<!-- mcp-name: io.github.proprock/sql-safe-mcp -->
|
|
61
|
+
|
|
62
|
+
General-purpose database MCP servers hand the agent a raw SQL prompt and dozens of tools. This
|
|
63
|
+
server gives a coding agent the schema knowledge it needs to write correct code - servers,
|
|
64
|
+
databases, tables, columns, keys, indexes, stored procedures - and, on servers you mark
|
|
65
|
+
`pii_safe`, a way to look at real rows without ever seeing the personal data in them.
|
|
66
|
+
|
|
67
|
+
## Read-only by design. PII-safe by default
|
|
68
|
+
|
|
69
|
+
- **Read-only by construction** - seven tools, all annotated read-only. No tool writes data, and
|
|
70
|
+
the server never executes SQL an agent wrote: metadata comes from SQLAlchemy Inspector and fixed
|
|
71
|
+
catalog queries, and `execute_sql` runs only a validated, regenerated `SELECT`.
|
|
72
|
+
- **PII-safe by default** - on a `pii_safe` server, the columns you configure come back as
|
|
73
|
+
alias-bound, authenticated tokens (`pii:v1:...`), never as plaintext. An agent can still
|
|
74
|
+
project, count, and filter on them with `=` and `IN` using tokens it was given, so it can follow
|
|
75
|
+
a record without reading it. Tokens do not work on another server alias or with another key.
|
|
76
|
+
- **Fails closed** - SQL validation is an allowlist. Unknown syntax, unresolved lineage, and
|
|
77
|
+
unsupported protected-value types are refused, not guessed at. The verification evidence is in
|
|
78
|
+
the [security model](SECURITY-MODEL.md).
|
|
79
|
+
- **Least access first** - `access_level: metadata` (the default) exposes schema only;
|
|
80
|
+
`execute_sql` needs an explicit `pii_safe` alias with its own key. Database permissions stay the
|
|
81
|
+
primary control, so use a least-privilege login.
|
|
82
|
+
|
|
83
|
+
## Also
|
|
84
|
+
|
|
85
|
+
- **Your aliases, not your network** - the agent sees only the server aliases you configure. There
|
|
86
|
+
is no network discovery, and the catalog is not published as MCP resources.
|
|
87
|
+
- **Secrets stay out of sight** - connection URLs live in YAML with `${NAME}` placeholders resolved
|
|
88
|
+
from the environment. They never appear in logs or model-visible errors.
|
|
89
|
+
- **Compact, predictable output** - object-rooted results with stable sorting, literal
|
|
90
|
+
case-insensitive name filters, and stored procedure lists that do not expand definitions.
|
|
91
|
+
- **Errors an agent can act on** - an ambiguous name lists the candidate schemas. Errors never
|
|
92
|
+
contain connection details, credentials, keys, tokens, or rows.
|
|
93
|
+
- **On PyPI** - `uvx sql-safe-mcp`, no repo clone required.
|
|
94
|
+
|
|
95
|
+
| Tool | Access | Purpose |
|
|
96
|
+
|---|---|---|
|
|
97
|
+
| `list_servers` | 🟢 read | Configured server aliases |
|
|
98
|
+
| `list_databases` | 🟢 read | Databases visible to the credentials |
|
|
99
|
+
| `list_tables` | 🟢 read | Base tables, filtered by schema or name |
|
|
100
|
+
| `get_table_definition` | 🟢 read | Columns, keys, constraints, and indexes of one table |
|
|
101
|
+
| `list_stored_procedures` | 🟢 read | Stored procedures, without definitions |
|
|
102
|
+
| `get_stored_procedure` | 🟢 read | The definition of one stored procedure |
|
|
103
|
+
| `execute_sql` | 🟢 read | One restricted `SELECT` on a `pii_safe` server; protected columns return tokens |
|
|
104
|
+
|
|
105
|
+
> [!NOTE]
|
|
106
|
+
> **Status:** SQL Server supports every tool. MySQL and MariaDB (`engine: mysql` or `mariadb`,
|
|
107
|
+
> `mysql+pymysql` URLs) support every tool too. `schema` is always `null` there because the
|
|
108
|
+
> database is the catalog, and `execute_sql` uses `LIMIT` instead of `TOP`. See
|
|
109
|
+
> [ARCHITECTURE.md](ARCHITECTURE.md).
|
|
110
|
+
|
|
111
|
+
- [Install](#install)
|
|
112
|
+
- [Configure](#configure)
|
|
113
|
+
- [PII-safe queries](#pii-safe-queries)
|
|
114
|
+
- [Security](#security)
|
|
115
|
+
- [Contributing](#contributing)
|
|
116
|
+
|
|
117
|
+
More detail lives in [`docs/`](docs): the [configuration reference](docs/configuration.md),
|
|
118
|
+
[what the tools return](docs/tools.md), and the [security model](SECURITY-MODEL.md).
|
|
119
|
+
|
|
120
|
+
## Install
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
uvx sql-safe-mcp
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
or
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
pip install sql-safe-mcp
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Pin a version when you want a fixed surface: `uvx sql-safe-mcp==1.2.0`.
|
|
133
|
+
|
|
134
|
+
Requires Python 3.12+, [uv](https://docs.astral.sh/uv/) (or `pip`), and
|
|
135
|
+
[Microsoft ODBC Driver 18 for SQL Server](https://learn.microsoft.com/sql/connect/odbc/download-odbc-driver-for-sql-server)
|
|
136
|
+
when you connect to SQL Server. MySQL and MariaDB use the bundled PyMySQL driver and need nothing
|
|
137
|
+
else.
|
|
138
|
+
|
|
139
|
+
Verified against SQL Server 2022, MySQL 8.4, and MariaDB 11.4 (see [CHECKS.md](CHECKS.md)).
|
|
140
|
+
|
|
141
|
+
## Configure
|
|
142
|
+
|
|
143
|
+
Copy [sql-safe-mcp.example.yaml](sql-safe-mcp.example.yaml) to `sql-safe-mcp.yaml`, list your
|
|
144
|
+
servers, and keep credentials in environment variables:
|
|
145
|
+
|
|
146
|
+
```yaml
|
|
147
|
+
version: 1
|
|
148
|
+
servers:
|
|
149
|
+
reporting:
|
|
150
|
+
engine: sqlserver
|
|
151
|
+
access_level: metadata
|
|
152
|
+
connection_url: "${REPORTING_SQL_URL}"
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Point the server at the file with `SQL_SAFE_MCP_CONFIG` (or `--config`), and check it without
|
|
156
|
+
connecting to any database:
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
SQL_SAFE_MCP_CONFIG=sql-safe-mcp.yaml uvx sql-safe-mcp --check-config
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Configuration is validated at startup, and an error names the problem without printing a URL or
|
|
163
|
+
secret. Keep credentials in the host's own configuration and never commit them. The server acts
|
|
164
|
+
with the database account's permissions, so use a dedicated login with the least access the job
|
|
165
|
+
needs. Every setting, including the runtime limits, is in
|
|
166
|
+
[configuration.md](docs/configuration.md).
|
|
167
|
+
|
|
168
|
+
<details>
|
|
169
|
+
<summary><b>Claude Code</b></summary>
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
claude mcp add --env SQL_SAFE_MCP_CONFIG=/path/to/sql-safe-mcp.yaml --env REPORTING_SQL_URL=mssql+pyodbc://... --transport stdio sql-safe -- uvx sql-safe-mcp
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Put at least one other option between the last `--env` and the server name, as above - the CLI
|
|
176
|
+
otherwise reads the name as another `KEY=value` pair.
|
|
177
|
+
|
|
178
|
+
</details>
|
|
179
|
+
|
|
180
|
+
<details>
|
|
181
|
+
<summary><b>Claude Desktop</b></summary>
|
|
182
|
+
|
|
183
|
+
In `claude_desktop_config.json`:
|
|
184
|
+
|
|
185
|
+
```json
|
|
186
|
+
{
|
|
187
|
+
"mcpServers": {
|
|
188
|
+
"sql-safe": {
|
|
189
|
+
"command": "uvx",
|
|
190
|
+
"args": ["sql-safe-mcp"],
|
|
191
|
+
"env": {
|
|
192
|
+
"SQL_SAFE_MCP_CONFIG": "/path/to/sql-safe-mcp.yaml",
|
|
193
|
+
"REPORTING_SQL_URL": "mssql+pyodbc://..."
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
</details>
|
|
201
|
+
|
|
202
|
+
<details>
|
|
203
|
+
<summary><b>Codex CLI</b></summary>
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
codex mcp add sql-safe --env SQL_SAFE_MCP_CONFIG=/path/to/sql-safe-mcp.yaml --env REPORTING_SQL_URL=mssql+pyodbc://... -- uvx sql-safe-mcp
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
</details>
|
|
210
|
+
|
|
211
|
+
<details>
|
|
212
|
+
<summary><b>Any other stdio host</b></summary>
|
|
213
|
+
|
|
214
|
+
Command `uvx`, argument `sql-safe-mcp`, and the environment variables your configuration
|
|
215
|
+
references, plus `SQL_SAFE_MCP_CONFIG`. The server speaks MCP over stdio and logs only to stderr.
|
|
216
|
+
|
|
217
|
+
</details>
|
|
218
|
+
|
|
219
|
+
## PII-safe queries
|
|
220
|
+
|
|
221
|
+
Mark an alias `access_level: pii_safe`, give it its own key, and list the protected columns:
|
|
222
|
+
|
|
223
|
+
```yaml
|
|
224
|
+
servers:
|
|
225
|
+
legacy_prod:
|
|
226
|
+
engine: sqlserver
|
|
227
|
+
access_level: pii_safe
|
|
228
|
+
connection_url: "${LEGACY_PROD_SQL_URL}"
|
|
229
|
+
pii_key_env: LEGACY_PROD_PII_KEY
|
|
230
|
+
pii:
|
|
231
|
+
rules:
|
|
232
|
+
- database: "*"
|
|
233
|
+
schema: dbo
|
|
234
|
+
table: Users
|
|
235
|
+
columns: [Email, FirstName, LastName]
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
`execute_sql` then accepts one restricted `SELECT`. Protected cells come back as tokens, and a
|
|
239
|
+
token is accepted only in `=` and `IN` predicates on the same alias. Protection covers the columns
|
|
240
|
+
you list, so list every column that holds personal data. The rule format, key generation, and the
|
|
241
|
+
accepted SQL are in [configuration.md](docs/configuration.md) and [tools.md](docs/tools.md).
|
|
242
|
+
|
|
243
|
+
## Security
|
|
244
|
+
|
|
245
|
+
The MCP caller, SQL input, database metadata, rows, and tokens are untrusted; the operator, the
|
|
246
|
+
process environment, and the database credentials are the trusted boundary. Database permissions
|
|
247
|
+
remain the primary authorization control - this server never widens them. The full model and its
|
|
248
|
+
verification are in [SECURITY-MODEL.md](SECURITY-MODEL.md). To report a vulnerability,
|
|
249
|
+
use the private channel in [SECURITY.md](SECURITY.md).
|
|
250
|
+
|
|
251
|
+
## Contributing
|
|
252
|
+
|
|
253
|
+
Setup, checks, the test commands, the branch and commit conventions, and the release model are in
|
|
254
|
+
[CONTRIBUTING.md](CONTRIBUTING.md). Changes that affect someone running the server are recorded in
|
|
255
|
+
[CHANGELOG.md](CHANGELOG.md).
|
|
256
|
+
|
|
257
|
+
## License
|
|
258
|
+
|
|
259
|
+
[MIT](LICENSE).
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# sql-safe-mcp
|
|
4
|
+
|
|
5
|
+
A read-only, PII-safe SQL Server, MySQL and MariaDB MCP server for coding agents: schema knowledge and safe queries, with no way to change or leak data.
|
|
6
|
+
|
|
7
|
+
[](https://github.com/proprock/sql-safe-mcp/actions/workflows/ci.yml)
|
|
8
|
+
[](https://github.com/proprock/sql-safe-mcp/releases)
|
|
9
|
+
[](https://pypi.org/project/sql-safe-mcp/)
|
|
10
|
+
[](https://www.python.org/downloads/)
|
|
11
|
+
[](LICENSE)
|
|
12
|
+
|
|
13
|
+
[](https://modelcontextprotocol.io)
|
|
14
|
+
[](server.json)
|
|
15
|
+
|
|
16
|
+
<img src="https://raw.githubusercontent.com/proprock/sql-safe-mcp/master/images/luna-guard.jpg" alt="Read the data. Protect the identity." width="760">
|
|
17
|
+
|
|
18
|
+
<i>Read the data. Protect the identity.</i>
|
|
19
|
+
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
<!-- mcp-name: io.github.proprock/sql-safe-mcp -->
|
|
23
|
+
|
|
24
|
+
General-purpose database MCP servers hand the agent a raw SQL prompt and dozens of tools. This
|
|
25
|
+
server gives a coding agent the schema knowledge it needs to write correct code - servers,
|
|
26
|
+
databases, tables, columns, keys, indexes, stored procedures - and, on servers you mark
|
|
27
|
+
`pii_safe`, a way to look at real rows without ever seeing the personal data in them.
|
|
28
|
+
|
|
29
|
+
## Read-only by design. PII-safe by default
|
|
30
|
+
|
|
31
|
+
- **Read-only by construction** - seven tools, all annotated read-only. No tool writes data, and
|
|
32
|
+
the server never executes SQL an agent wrote: metadata comes from SQLAlchemy Inspector and fixed
|
|
33
|
+
catalog queries, and `execute_sql` runs only a validated, regenerated `SELECT`.
|
|
34
|
+
- **PII-safe by default** - on a `pii_safe` server, the columns you configure come back as
|
|
35
|
+
alias-bound, authenticated tokens (`pii:v1:...`), never as plaintext. An agent can still
|
|
36
|
+
project, count, and filter on them with `=` and `IN` using tokens it was given, so it can follow
|
|
37
|
+
a record without reading it. Tokens do not work on another server alias or with another key.
|
|
38
|
+
- **Fails closed** - SQL validation is an allowlist. Unknown syntax, unresolved lineage, and
|
|
39
|
+
unsupported protected-value types are refused, not guessed at. The verification evidence is in
|
|
40
|
+
the [security model](SECURITY-MODEL.md).
|
|
41
|
+
- **Least access first** - `access_level: metadata` (the default) exposes schema only;
|
|
42
|
+
`execute_sql` needs an explicit `pii_safe` alias with its own key. Database permissions stay the
|
|
43
|
+
primary control, so use a least-privilege login.
|
|
44
|
+
|
|
45
|
+
## Also
|
|
46
|
+
|
|
47
|
+
- **Your aliases, not your network** - the agent sees only the server aliases you configure. There
|
|
48
|
+
is no network discovery, and the catalog is not published as MCP resources.
|
|
49
|
+
- **Secrets stay out of sight** - connection URLs live in YAML with `${NAME}` placeholders resolved
|
|
50
|
+
from the environment. They never appear in logs or model-visible errors.
|
|
51
|
+
- **Compact, predictable output** - object-rooted results with stable sorting, literal
|
|
52
|
+
case-insensitive name filters, and stored procedure lists that do not expand definitions.
|
|
53
|
+
- **Errors an agent can act on** - an ambiguous name lists the candidate schemas. Errors never
|
|
54
|
+
contain connection details, credentials, keys, tokens, or rows.
|
|
55
|
+
- **On PyPI** - `uvx sql-safe-mcp`, no repo clone required.
|
|
56
|
+
|
|
57
|
+
| Tool | Access | Purpose |
|
|
58
|
+
|---|---|---|
|
|
59
|
+
| `list_servers` | 🟢 read | Configured server aliases |
|
|
60
|
+
| `list_databases` | 🟢 read | Databases visible to the credentials |
|
|
61
|
+
| `list_tables` | 🟢 read | Base tables, filtered by schema or name |
|
|
62
|
+
| `get_table_definition` | 🟢 read | Columns, keys, constraints, and indexes of one table |
|
|
63
|
+
| `list_stored_procedures` | 🟢 read | Stored procedures, without definitions |
|
|
64
|
+
| `get_stored_procedure` | 🟢 read | The definition of one stored procedure |
|
|
65
|
+
| `execute_sql` | 🟢 read | One restricted `SELECT` on a `pii_safe` server; protected columns return tokens |
|
|
66
|
+
|
|
67
|
+
> [!NOTE]
|
|
68
|
+
> **Status:** SQL Server supports every tool. MySQL and MariaDB (`engine: mysql` or `mariadb`,
|
|
69
|
+
> `mysql+pymysql` URLs) support every tool too. `schema` is always `null` there because the
|
|
70
|
+
> database is the catalog, and `execute_sql` uses `LIMIT` instead of `TOP`. See
|
|
71
|
+
> [ARCHITECTURE.md](ARCHITECTURE.md).
|
|
72
|
+
|
|
73
|
+
- [Install](#install)
|
|
74
|
+
- [Configure](#configure)
|
|
75
|
+
- [PII-safe queries](#pii-safe-queries)
|
|
76
|
+
- [Security](#security)
|
|
77
|
+
- [Contributing](#contributing)
|
|
78
|
+
|
|
79
|
+
More detail lives in [`docs/`](docs): the [configuration reference](docs/configuration.md),
|
|
80
|
+
[what the tools return](docs/tools.md), and the [security model](SECURITY-MODEL.md).
|
|
81
|
+
|
|
82
|
+
## Install
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
uvx sql-safe-mcp
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
or
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
pip install sql-safe-mcp
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Pin a version when you want a fixed surface: `uvx sql-safe-mcp==1.2.0`.
|
|
95
|
+
|
|
96
|
+
Requires Python 3.12+, [uv](https://docs.astral.sh/uv/) (or `pip`), and
|
|
97
|
+
[Microsoft ODBC Driver 18 for SQL Server](https://learn.microsoft.com/sql/connect/odbc/download-odbc-driver-for-sql-server)
|
|
98
|
+
when you connect to SQL Server. MySQL and MariaDB use the bundled PyMySQL driver and need nothing
|
|
99
|
+
else.
|
|
100
|
+
|
|
101
|
+
Verified against SQL Server 2022, MySQL 8.4, and MariaDB 11.4 (see [CHECKS.md](CHECKS.md)).
|
|
102
|
+
|
|
103
|
+
## Configure
|
|
104
|
+
|
|
105
|
+
Copy [sql-safe-mcp.example.yaml](sql-safe-mcp.example.yaml) to `sql-safe-mcp.yaml`, list your
|
|
106
|
+
servers, and keep credentials in environment variables:
|
|
107
|
+
|
|
108
|
+
```yaml
|
|
109
|
+
version: 1
|
|
110
|
+
servers:
|
|
111
|
+
reporting:
|
|
112
|
+
engine: sqlserver
|
|
113
|
+
access_level: metadata
|
|
114
|
+
connection_url: "${REPORTING_SQL_URL}"
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Point the server at the file with `SQL_SAFE_MCP_CONFIG` (or `--config`), and check it without
|
|
118
|
+
connecting to any database:
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
SQL_SAFE_MCP_CONFIG=sql-safe-mcp.yaml uvx sql-safe-mcp --check-config
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Configuration is validated at startup, and an error names the problem without printing a URL or
|
|
125
|
+
secret. Keep credentials in the host's own configuration and never commit them. The server acts
|
|
126
|
+
with the database account's permissions, so use a dedicated login with the least access the job
|
|
127
|
+
needs. Every setting, including the runtime limits, is in
|
|
128
|
+
[configuration.md](docs/configuration.md).
|
|
129
|
+
|
|
130
|
+
<details>
|
|
131
|
+
<summary><b>Claude Code</b></summary>
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
claude mcp add --env SQL_SAFE_MCP_CONFIG=/path/to/sql-safe-mcp.yaml --env REPORTING_SQL_URL=mssql+pyodbc://... --transport stdio sql-safe -- uvx sql-safe-mcp
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Put at least one other option between the last `--env` and the server name, as above - the CLI
|
|
138
|
+
otherwise reads the name as another `KEY=value` pair.
|
|
139
|
+
|
|
140
|
+
</details>
|
|
141
|
+
|
|
142
|
+
<details>
|
|
143
|
+
<summary><b>Claude Desktop</b></summary>
|
|
144
|
+
|
|
145
|
+
In `claude_desktop_config.json`:
|
|
146
|
+
|
|
147
|
+
```json
|
|
148
|
+
{
|
|
149
|
+
"mcpServers": {
|
|
150
|
+
"sql-safe": {
|
|
151
|
+
"command": "uvx",
|
|
152
|
+
"args": ["sql-safe-mcp"],
|
|
153
|
+
"env": {
|
|
154
|
+
"SQL_SAFE_MCP_CONFIG": "/path/to/sql-safe-mcp.yaml",
|
|
155
|
+
"REPORTING_SQL_URL": "mssql+pyodbc://..."
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
</details>
|
|
163
|
+
|
|
164
|
+
<details>
|
|
165
|
+
<summary><b>Codex CLI</b></summary>
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
codex mcp add sql-safe --env SQL_SAFE_MCP_CONFIG=/path/to/sql-safe-mcp.yaml --env REPORTING_SQL_URL=mssql+pyodbc://... -- uvx sql-safe-mcp
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
</details>
|
|
172
|
+
|
|
173
|
+
<details>
|
|
174
|
+
<summary><b>Any other stdio host</b></summary>
|
|
175
|
+
|
|
176
|
+
Command `uvx`, argument `sql-safe-mcp`, and the environment variables your configuration
|
|
177
|
+
references, plus `SQL_SAFE_MCP_CONFIG`. The server speaks MCP over stdio and logs only to stderr.
|
|
178
|
+
|
|
179
|
+
</details>
|
|
180
|
+
|
|
181
|
+
## PII-safe queries
|
|
182
|
+
|
|
183
|
+
Mark an alias `access_level: pii_safe`, give it its own key, and list the protected columns:
|
|
184
|
+
|
|
185
|
+
```yaml
|
|
186
|
+
servers:
|
|
187
|
+
legacy_prod:
|
|
188
|
+
engine: sqlserver
|
|
189
|
+
access_level: pii_safe
|
|
190
|
+
connection_url: "${LEGACY_PROD_SQL_URL}"
|
|
191
|
+
pii_key_env: LEGACY_PROD_PII_KEY
|
|
192
|
+
pii:
|
|
193
|
+
rules:
|
|
194
|
+
- database: "*"
|
|
195
|
+
schema: dbo
|
|
196
|
+
table: Users
|
|
197
|
+
columns: [Email, FirstName, LastName]
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
`execute_sql` then accepts one restricted `SELECT`. Protected cells come back as tokens, and a
|
|
201
|
+
token is accepted only in `=` and `IN` predicates on the same alias. Protection covers the columns
|
|
202
|
+
you list, so list every column that holds personal data. The rule format, key generation, and the
|
|
203
|
+
accepted SQL are in [configuration.md](docs/configuration.md) and [tools.md](docs/tools.md).
|
|
204
|
+
|
|
205
|
+
## Security
|
|
206
|
+
|
|
207
|
+
The MCP caller, SQL input, database metadata, rows, and tokens are untrusted; the operator, the
|
|
208
|
+
process environment, and the database credentials are the trusted boundary. Database permissions
|
|
209
|
+
remain the primary authorization control - this server never widens them. The full model and its
|
|
210
|
+
verification are in [SECURITY-MODEL.md](SECURITY-MODEL.md). To report a vulnerability,
|
|
211
|
+
use the private channel in [SECURITY.md](SECURITY.md).
|
|
212
|
+
|
|
213
|
+
## Contributing
|
|
214
|
+
|
|
215
|
+
Setup, checks, the test commands, the branch and commit conventions, and the release model are in
|
|
216
|
+
[CONTRIBUTING.md](CONTRIBUTING.md). Changes that affect someone running the server are recorded in
|
|
217
|
+
[CHANGELOG.md](CHANGELOG.md).
|
|
218
|
+
|
|
219
|
+
## License
|
|
220
|
+
|
|
221
|
+
[MIT](LICENSE).
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "sql-safe-mcp"
|
|
3
|
+
version = "1.2.0"
|
|
4
|
+
description = "Minimal, read-only, PII-safe MCP server for SQL databases."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12,<3.15"
|
|
7
|
+
license = { file = "LICENSE" }
|
|
8
|
+
dependencies = [
|
|
9
|
+
"anyio>=4.8,<5",
|
|
10
|
+
"cryptography>=46.0.7,<47",
|
|
11
|
+
"mcp>=2,<3",
|
|
12
|
+
"pydantic>=2.10,<3",
|
|
13
|
+
"pymysql>=1.2.3",
|
|
14
|
+
"pyodbc>=5.2,<6",
|
|
15
|
+
"pyyaml>=6,<7",
|
|
16
|
+
"sqlalchemy>=2.0,<3",
|
|
17
|
+
"sqlglot>=30.18,<31",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.scripts]
|
|
21
|
+
sql-safe-mcp = "sql_safe_mcp.__main__:main"
|
|
22
|
+
|
|
23
|
+
[dependency-groups]
|
|
24
|
+
dev = [
|
|
25
|
+
"hypothesis>=6.168,<7",
|
|
26
|
+
"mutmut>=3.8,<4 ; sys_platform != 'win32'",
|
|
27
|
+
"prek>=0.2",
|
|
28
|
+
"pytest>=8.3,<10",
|
|
29
|
+
"pytest-cov>=6,<8",
|
|
30
|
+
"ruff>=0.11",
|
|
31
|
+
"ty>=0.0.1a20",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[build-system]
|
|
35
|
+
requires = ["hatchling"]
|
|
36
|
+
build-backend = "hatchling.build"
|
|
37
|
+
|
|
38
|
+
[tool.hatch.build.targets.wheel]
|
|
39
|
+
packages = ["src/sql_safe_mcp"]
|
|
40
|
+
|
|
41
|
+
[tool.hatch.build.targets.sdist]
|
|
42
|
+
include = [
|
|
43
|
+
"/CHANGELOG.md",
|
|
44
|
+
"/LICENSE",
|
|
45
|
+
"/README.md",
|
|
46
|
+
"/pyproject.toml",
|
|
47
|
+
"/server.json",
|
|
48
|
+
"/sql-safe-mcp.example.yaml",
|
|
49
|
+
"/src",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
[tool.pytest.ini_options]
|
|
53
|
+
addopts = "-ra --strict-markers"
|
|
54
|
+
testpaths = ["tests"]
|
|
55
|
+
pythonpath = ["tests/security", "tests/integration/sqlserver", "tests/integration/mysql"]
|
|
56
|
+
markers = [
|
|
57
|
+
"integration: requires a live database",
|
|
58
|
+
"deep: expensive security verification",
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
[tool.mutmut]
|
|
62
|
+
source_paths = ["src/sql_safe_mcp"]
|
|
63
|
+
also_copy = ["server.json", "README.md"]
|
|
64
|
+
only_mutate = ["*security/*.py", "*config.py"]
|
|
65
|
+
mutate_only_covered_lines = true
|
|
66
|
+
pytest_add_cli_args = ["-q", "-m", "not integration and not deep", "-p", "no:cacheprovider"]
|
|
67
|
+
pytest_add_cli_args_test_selection = ["tests/unit", "tests/security", "tests/contract"]
|
|
68
|
+
timeout_multiplier = 3.0
|
|
69
|
+
timeout_constant = 5.0
|
|
70
|
+
|
|
71
|
+
[tool.coverage.run]
|
|
72
|
+
branch = true
|
|
73
|
+
source = ["sql_safe_mcp"]
|
|
74
|
+
|
|
75
|
+
[tool.ruff]
|
|
76
|
+
line-length = 100
|
|
77
|
+
target-version = "py312"
|
|
78
|
+
|
|
79
|
+
[tool.ruff.lint]
|
|
80
|
+
select = ["E", "F", "I", "B", "UP", "SIM", "RUF"]
|
|
81
|
+
|
|
82
|
+
[tool.ruff.format]
|
|
83
|
+
quote-style = "double"
|
|
84
|
+
|
|
85
|
+
[tool.ty.src]
|
|
86
|
+
include = ["src", "tests"]
|
|
87
|
+
|
|
88
|
+
[tool.ty.environment]
|
|
89
|
+
extra-paths = ["tests/security", "tests/integration/sqlserver", "tests/integration/mysql"]
|