Governed SQL MCP server: the agent never writes SQL. Queries are compiled from a semantic layer you control, blocked PII is refused before the query runs, and every call can land in a tamper-evident audit log. Multi-dialect (SQL Server, Postgres, SQLite).
Project description
sql-steward
Part of the Governed Agent Stack: free, on-prem building blocks for an AI agent you can point at a real database and audit.
A governed SQL gateway for AI agents, exposed over the Model Context Protocol. The agent never gets a connection string and never writes SQL. It calls typed tools; sql-steward compiles every query from a semantic layer you control, refuses blocked PII before the query runs, and returns rows. Same tools across SQL Server, Postgres and SQLite.
Most SQL MCP servers hand the model a run_sql tool and try to catch the bad queries on the way out. sql-steward removes the tool. There is no path from a prompt to raw SQL at your database, because the only thing the agent can do is name an entity or a metric and pick from allow-lists you wrote.
See it defend a database live. The governed vs ungoverned demo runs the same request through a naive run_sql agent, which leaks customer PII and empties a table, and through sql-steward, which refuses it at compile time.
Three guarantees
- Read-only by construction. There is no
run_sql,query, orexecutetool. The compiler can only ever build aSELECT, so a write isn't blocked, it's unrepresentable. - PII refused before retrieval. Every field can carry a PII tag. If a request touches a category your policy blocks, sql-steward refuses with a structured reason before any SQL is compiled or run.
- Auditable. Every call, refusal and error can be recorded in a tamper-evident, hash-chained log via agent-blackbox, with
audit-verifyto prove nothing was rewritten.
See it in 10 seconds
pip install sql-steward # or: pipx install sql-steward
sql-steward demo # zero config, no API key, no agent, SQLite
1) get_metric('mrr_total', dimensions=['plan']) -> safe aggregate
compiled: SELECT subscriptions.plan, SUM(subscriptions.mrr) AS mrr_total
FROM subscriptions GROUP BY subscriptions.plan LIMIT 1000
{'plan': 'pro', 'mrr_total': 297.0}
{'plan': 'team', 'mrr_total': 598.0}
2) get_metric('mrr_total', dimensions=['customers.country']) -> auto-join
compiled: ... INNER JOIN customers ON subscriptions.customer_id = customers.id ...
3) get_records('customers', fields=['id','email']) -> PII refusal
refused: {"kind": "pii_blocked", "detail": "Field 'customers.email' is tagged
EMAIL_ADDRESS, which this policy refuses."}
The semantic layer
This YAML is the entire contract between the agent and your database. Review it like code.
dialect: postgres
entities:
customers:
table: customers
fields:
id: {type: int}
name: {type: text, pii: PERSON}
email: {type: text, pii: EMAIL_ADDRESS}
country: {type: text}
subscriptions:
table: subscriptions
fields:
customer_id: {type: int}
plan: {type: text}
mrr: {type: numeric}
joins: # nothing reachable that isn't listed here
- left: subscriptions
right: customers
on: subscriptions.customer_id = customers.id
metrics:
mrr_total: # the aggregation is fixed; the agent only
entity: subscriptions # chooses dimensions/filters from the lists
aggregate: sum
field: mrr
dimensions_allowed: [plan, status, customers.country]
filters_allowed: [status, customers.country]
policy:
block_pii: [EMAIL_ADDRESS, CREDIT_CARD]
max_rows: 1000
Ask for a join that isn't defined and you get unreachable_entity, not an invented relationship. Ask to group a metric by a dimension that isn't listed and you get dimension_not_allowed.
Tools exposed to the agent
| Tool | Purpose |
|---|---|
list_entities() |
What can be read, plus the available metrics |
describe_entity(entity) |
Fields, types and PII tags (blocked ones flagged) |
list_metrics() |
Metrics and the dimensions/filters each allows |
get_records(entity, fields, filters, order_by, limit) |
Read rows from one entity |
get_metric(metric, dimensions, filters, limit) |
Compute a pre-approved aggregate |
semantic_search(entity, query, k, filters) |
pgvector nearest-neighbour search over an entity's embedding column |
audit_verify() |
Verify the tamper-evident audit chain |
Filters are {field, op, value}; operators are =, !=, <, <=, >, >=, like, in, not in, is null, is not null. Values are always bound parameters, never inlined.
Wire it into an MCP client
servers.yaml lives wherever you point SQL_STEWARD_LAYER. Claude Desktop (claude_desktop_config.json):
{
"mcpServers": {
"sql-steward": {
"command": "sql-steward",
"env": {
"SQL_STEWARD_LAYER": "/full/path/to/semantic.yaml",
"SQL_STEWARD_DB_URL": "postgresql+psycopg://readonly@db.internal/warehouse"
}
}
}
}
SQL_STEWARD_DB_URL is a SQLAlchemy URL, so the same server reads SQL Server (mssql+pyodbc://...), Postgres (postgresql+psycopg://...) or SQLite (sqlite:///path.db). Install the matching driver with the extras: pip install "sql-steward[postgres]" or "[mssql]".
Optional: the rest of the stack
The semantic layer is the primary control. These are extra layers, all opt-in, and no-ops if the library isn't installed:
pip install "sql-steward[rbac,mask,audit]"
export SQL_STEWARD_POLICY=/path/to/policy.yaml # query-warden second-pass role check
export SQL_STEWARD_ROLE=analyst
export SQL_STEWARD_MASK=1 # pii-veil masks anything left in results
export SQL_STEWARD_AUDIT_DB=logs/steward.db # agent-blackbox audit chain (on if installed)
export SQL_STEWARD_QUERY_BUDGET=200 # hard cap on queries per role per session
export SQL_STEWARD_EMBED_URL=http://localhost:11434/api/embeddings # local embeddings for semantic_search
export SQL_STEWARD_EMBED_MODEL=nomic-embed-text
Semantic search (pgvector)
Give an entity a search block pointing at a pgvector column and the agent gets a semantic_search tool, governed exactly like everything else (PII refused, results masked, calls audited):
entities:
documents:
table: documents
fields:
id: {type: int}
title: {type: text}
embedding: {type: vector}
search:
vector_column: embedding
dim: 768
returns: [id, title]
The query text is embedded locally (set SQL_STEWARD_EMBED_URL to a local Ollama endpoint, so nothing leaves the building), and matched with pgvector's <=> operator. PostgreSQL only. The embedding column is never returned.
- query-warden re-checks the compiled SQL against a role policy.
- pii-veil masks any PII that survives into result rows.
- agent-blackbox records every call in a hash-chained ledger;
sql-steward audit-verifychecks it.
How this is different
A typical SQL MCP validates arbitrary SQL the model wrote (a blocklist: catch what's bad). sql-steward compiles SQL from definitions you wrote (an allow-list: only what's described exists). The read-only and PII guarantees hold by construction rather than by inspection, and the query surface is the same across three engines.
Versus a semantic layer (Cube, dbt Semantic Layer, Cortex Analyst, Genie)
The nearest tools are not other MCP servers, they are semantic layers. Cube and the dbt Semantic Layer already expose compiled, metric-only access, and Snowflake Cortex Analyst and Databricks Genie both answer natural-language questions over a governed model. If you run on their platform, use them. This project exists for the case they do not cover:
- On-prem and air-gappable. sql-steward is a pip install that talks to SQL Server, Postgres or SQLite with no account, no warehouse, and no data leaving the building. Cortex Analyst is Snowflake, Genie is Databricks, and Cube's cloud features assume their service. For a factory or a hospital that cannot send data to a vendor, that difference is the whole decision.
- PII refused before retrieval, at the same chokepoint. A blocked field is refused at compile time for every caller, so the model cannot read what policy forbids even to reason over. Semantic layers govern which metrics you can query; they are not built to guarantee a tagged column never reaches the model.
- One tamper-evident audit for the whole agent, not just SQL. The same gate pattern wraps KQL, document retrieval and agent memory in the composed stack, under one hash-chained ledger. A warehouse semantic layer governs the warehouse; it does not govern the agent's other tools.
Short version: a semantic layer makes queries safe on its platform. sql-steward makes an agent safe on your infrastructure, across every surface it can reach.
Scaling to a real schema
The semantic layer is authored by hand on purpose, so it reads and reviews like code. That is the right default for tens of tables and the wrong one for thousands: nobody hand-writes a layer for a 10,000-table ERP, and returning the whole layer in one list_entities call would not fit an agent's context anyway. How that scales:
-
Bootstrap, then review.
sql-steward init --from-db <url>reflects a live schema, maps column types, proposes PII tags from column-name heuristics (biased toward over-tagging, so a leak is a review edit rather than a default), and infers joins from foreign keys. It emits a draft layer that loads and validates as-is, with a header that tells you what to narrow. The point is not to auto-expose everything; it is to remove the blank-page problem so a large schema starts as a reviewable file, not a hand-typed one.sql-steward init --from-db "postgresql+psycopg://readonly@db/warehouse" --out semantic.yaml # then delete entities you do not need, check the PII tags, add metrics
-
Scope beats size. A governed layer should expose the handful of entities an agent actually needs, not the whole database. A 10,000-table schema still becomes a 20-entity contract; the discipline is deciding what belongs (use
--include/--excludeto draft only a slice), which is a feature of the model, not a limit of the tool. -
Discovery grows with the layer.
list_entities/describe_entityare browsed as the layer grows; search and paging on those responses is the next edge to close before this points at a very large single layer.
Develop
git clone https://github.com/Pawansingh3889/sql-steward
cd sql-steward
pip install -e ".[dev]"
pytest -q
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sql_steward-0.3.3.tar.gz.
File metadata
- Download URL: sql_steward-0.3.3.tar.gz
- Upload date:
- Size: 32.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d316dac8fe1e2dcf0b01c8c97979e9edb9fc2d4e5cdd8e074c2e2e173105ff4
|
|
| MD5 |
6ab63969742e890f534b65e6f8b771ce
|
|
| BLAKE2b-256 |
a94e7948747c96076b7f486b37e7c6869b70ab395501662c88c9740260d26aeb
|
Provenance
The following attestation bundles were made for sql_steward-0.3.3.tar.gz:
Publisher:
release.yml on Pawansingh3889/sql-steward
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sql_steward-0.3.3.tar.gz -
Subject digest:
3d316dac8fe1e2dcf0b01c8c97979e9edb9fc2d4e5cdd8e074c2e2e173105ff4 - Sigstore transparency entry: 2107206349
- Sigstore integration time:
-
Permalink:
Pawansingh3889/sql-steward@483f55f37bfefeeba2feee5e91723da846cb9896 -
Branch / Tag:
refs/tags/v0.3.3 - Owner: https://github.com/Pawansingh3889
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@483f55f37bfefeeba2feee5e91723da846cb9896 -
Trigger Event:
release
-
Statement type:
File details
Details for the file sql_steward-0.3.3-py3-none-any.whl.
File metadata
- Download URL: sql_steward-0.3.3-py3-none-any.whl
- Upload date:
- Size: 30.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50fc1d447fc9415d53df0510b4960f4e23ebb4d14bfa9f549ae446679c43341a
|
|
| MD5 |
afda38182cdcdce256186e7d8af6322b
|
|
| BLAKE2b-256 |
9829c6533bcbc1ed2332a6ab110b458dee25b698d1bc01f47ee0dd265e3ba7bb
|
Provenance
The following attestation bundles were made for sql_steward-0.3.3-py3-none-any.whl:
Publisher:
release.yml on Pawansingh3889/sql-steward
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sql_steward-0.3.3-py3-none-any.whl -
Subject digest:
50fc1d447fc9415d53df0510b4960f4e23ebb4d14bfa9f549ae446679c43341a - Sigstore transparency entry: 2107206453
- Sigstore integration time:
-
Permalink:
Pawansingh3889/sql-steward@483f55f37bfefeeba2feee5e91723da846cb9896 -
Branch / Tag:
refs/tags/v0.3.3 - Owner: https://github.com/Pawansingh3889
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@483f55f37bfefeeba2feee5e91723da846cb9896 -
Trigger Event:
release
-
Statement type: