sqlguard-mcp:只读 SQL 防护 MCP 服务器
A read-only SQLite MCP server for AI agents. It lets Claude Code, Claude Desktop or any MCP client explore and query SQLite databases, and it is built so that a model cannot change your data even when it tries to.
It was extracted from MetricLens, a text-to-SQL project evaluated on the BIRD Mini-Dev benchmark, where the same guard ran about 20,000 model-generated queries and a prompt-injection test.
Why another SQLite server
The common reference SQLite server exposes write tools. For an agent that only needs to answer questions about data, that is more power than it should have. A read-only file mode alone is not enough either: SQLite still lets a read-only connection run ATTACH, most PRAGMAs and CREATE TEMP TABLE.
sqlguard-mcp stacks five layers:
- Static check: sqlglot must parse the input as a single
SELECT(CTEs andUNIONincluded). Multiple statements and write nodes anywhere in the tree, such asWITH t AS (...) DELETE ..., are rejected. - Read-only connection: the database is opened with
mode=ro, and a missing path is an error rather than a new empty file. - Authorizer:
sqlite3.set_authorizerallows read actions and a few metadata pragmas only, soATTACH,PRAGMA writable_schema = 1and temp tables are refused even if layer 1 is bypassed. - Timeout: a progress handler interrupts queries that run too long.
- Row cap: results are fetched with a hard limit so a large table cannot flood the model's context.
SQL that sqlglot cannot parse is still passed to SQLite, so the agent gets SQLite's own error message and can correct itself; layers 2 and 3 still apply.
Tools
| Tool | Purpose |
|---|---|
list_databases |
db_id of every database (the file name without extension) |
list_tables |
tables and their column counts |
describe_table |
columns, types, primary and foreign keys, 3 sample rows |
search_values |
case-insensitive search of text columns, to find the value actually stored (the user says "female", the column holds F) |
run_sql |
runs one SELECT; errors come back as {"error", "message"} instead of failing the call |
Usage
Run it from PyPI with uv, or install it with pip install sqlguard-mcp:
uvx sqlguard-mcp path/to/shop.db
Or from a checkout, with more than one database and your own limits:
uv run sqlguard-mcp path/to/shop.db path/to/folder_of_dbs --timeout 30 --max-rows 1000
Add it to Claude Code:
claude mcp add sqlguard -- uvx sqlguard-mcp /path/to/shop.db
Inspect it with the MCP Inspector:
npx @modelcontextprotocol/inspector uv run sqlguard-mcp /path/to/shop.db
Development
uv sync
uv run pytest
uv run ruff check
The tests include write attempts that bypass the static check, to show the connection layers hold on their own, plus a row of injected instructions stored in the data to show it comes back as plain data.
License
MIT
Metadata
Release files for sqlguard-mcp 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sqlguard_mcp-0.1.0.tar.gz | 8.5 kB | Without provenance | Details for sqlguard_mcp-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sqlguard_mcp-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for sqlguard_mcp-0.1.0-py3-none-any.whl |
Total release size: 18.9 kB
Release files / sqlguard_mcp-0.1.0.tar.gz
| Download URL | sqlguard_mcp-0.1.0.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c8a1853004623be50decbc28607c23398f49b027dc7008292986e0f18e1e9b76
|
|
BLAKE2b-256 checksum How to use checksums |
49f5d10b9ea762d7a4c1767da5272d773399e36e87990eca035106fbb669046f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / sqlguard_mcp-0.1.0-py3-none-any.whl
| Download URL | sqlguard_mcp-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c3bebb0728919074a4481ed549fa1d1c246ddff9a98b7594495f52c2dc84ac9f
|
|
BLAKE2b-256 checksum How to use checksums |
05f6723b6144058682d089a5d58b7aa31346b110d826038c4be07a96ac8fbb8d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for sqlguard-mcp 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.