guarded-sql-mcp
This server lets an LLM agent query a fictional IT asset inventory (PostgreSQL) through seven fixed, read-only, parameterized tools — it cannot write SQL or reach unlisted tables/columns.
List sites (
list_sites, no params): site code, name, city, and count of non-retired devices.Search devices (
search_devices): filter by site code, status (online/offline/retired), or OS substring; returns hostname, site, OS, status, IP, last check-in.Get one device (
get_device, by hostname): device plus site name and the assigned person's name and email.Find people (
find_people): search by partial name or email; returns full name, email, department, site.Device software (
device_software, by hostname): installed packages, optionally filtered by name substring; returns name, version, installed_at.Stale devices (
stale_devices): non-retired devices not seen in N days (1–365, default 30) or never, oldest first, optionally by site.Open tickets (
open_tickets): unclosed tickets, highest priority first, filterable by site and priority (low/medium/high/critical).
Every tool is read-only, takes only bounded literal arguments (limit 1–100, default 25; string patterns/lengths enforced; unknown keys like sql rejected before the database is touched), matches text literally (no wildcard escapes), returns JSON with rowCount/hasMore/truncated, caps rows at 100, cells at 1000 chars and responses at 64 KiB, and excludes password/secret columns entirely.
Provides read-only access to a PostgreSQL database through a fixed catalog of parameterized queries. An LLM agent can query an IT asset inventory to list sites, search devices, find people, inspect installed software, identify stale devices, and view open tickets, without writing SQL or accessing restricted tables and columns.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@guarded-sql-mcp show me devices that haven't checked in for 90 days"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
guarded-sql-mcp
An MCP server that gives an LLM agent read access to a PostgreSQL database through a fixed catalog of parameterized queries. The model can pick a query and fill in bounded parameters. It cannot write SQL, and it cannot reach tables or columns that the catalog and the database role do not allow. This is a public reimplementation of a pattern I use in five internal, closed-source MCP servers that expose PostgreSQL and SQL Server to LLM agents; none of that code is here. It was written from scratch, with a fictional schema and data, so that the approach and the tests behind it can be read in full.
The schema is a fictional IT asset inventory (sites, devices, people, installed software, tickets).
Threat model
Treat everything the model sends as untrusted input. It may have read a malicious ticket title, a poisoned web page or a crafted email, and its tool arguments can carry whatever that content asked for. A system prompt that says "never read the password column" is a request, not an access control. So every restriction here is enforced in code or in the database, and each one is covered by a test:
No free-form SQL is reachable by the model. Every tool maps to one static, reviewed statement. Arguments are only ever bind parameters.
Password and secret columns are excluded by construction. They are rejected in the catalog at startup, stripped from results at runtime, and not granted to the database role.
The table allowlist is checked in code, not described in a prompt. A catalog entry that touches a table outside the allowlist stops the server from starting.
A single call cannot flood the model's context. Rows are capped at 100, every string cell at 1000 characters, and the whole JSON response at 64 KiB. Without the byte cap, one call over a few wide rows could return megabytes. Anything cut is flagged
truncated: truein the result and recorded in the audit log.
Related MCP server: MCP-SQL
Defense layers
flowchart TD
S["Startup: validateCatalog()"] -.->|must pass before the server is built| T
M["Model: tools/call"] --> T{"Tool in the catalog?"}
T -- no --> R1["Refused"]
T -- yes --> V{"Strict zod schema:<br/>bounded fields, no unknown keys"}
V -- invalid --> R2["Refused, executor never called"]
V -- valid --> P["params() builds $1..$n bind values"]
P --> X["PgExecutor: BEGIN READ ONLY,<br/>SET LOCAL statement_timeout,<br/>static SQL ending in LIMIT, COMMIT"]
X --> DB[("PostgreSQL as mcp_readonly:<br/>table and column grants")]
DB --> C["Row cap, then projection onto declared<br/>columns; sensitive keys dropped"]
C --> SZ["Size cap: long cells cut,<br/>trailing rows dropped to fit 64 KiB"]
SZ --> OUT["structuredContent + JSON text to the model"]
X -- error --> E["Generic error to the model,<br/>details to the audit log"]Fixed catalog (src/catalog.ts). One MCP tool per entry. There is no generic query tool and no parameter named
sql,queryorstatement.Input validation. Each entry has a
z.strictObjectschema. Unknown keys are rejected. Every string has a maximum length and usually a pattern (hostnames:^[A-Za-z0-9-]{1,63}$), every number has explicit bounds,limitis 1..100. Search text is matched withILIKE ... ESCAPE '\'after escaping%and_, so the model cannot turn a search into a wildcard dump.Startup catalog validation (src/validateCatalog.ts). Before the server is created, every entry is checked (see What the validator rejects). One bad entry means the process exits.
Read-only execution (src/executor.ts). Every call runs as
BEGIN READ ONLY, a transaction-localstatement_timeout(default 5 s), the statement, thenCOMMIT, orROLLBACKon any error. Every statement must end inLIMIT, and at most 100 rows leave the server. TheLIMITis bound to one row more than requested, so a result can sayhasMore: truewhen more rows matched than were returned.Output projection. Each row is rebuilt from the entry's declared column list. Undeclared keys are dropped. Keys matching the sensitive pattern are dropped at any depth, including inside JSON values, even if declared. Drops are recorded in the audit log.
Response size cap (src/response.ts). Every cell becomes a JSON scalar (dates as ISO strings, JSON values serialized). String cells longer than
MAX_CELL_CHARS(default 1000) are cut and end with…[truncated N chars]. Trailing rows are then dropped until the serialized body fitsMAX_RESPONSE_BYTES(default 64 KiB). The result carriestruncated: trueand the audit record says how many cells were cut and rows dropped.Database role (db/roles.sql).
mcp_readonlyhasSELECTon the allowlisted tables only, column-levelSELECTonpeoplethat leaves outpassword_hashandmfa_secret, nothing onapi_tokens, and noCREATEorTEMP. Its sessions default todefault_transaction_read_only = on,statement_timeout = 5sandidle_in_transaction_session_timeout = 10s, so even a session opened outside the server (psql with the same credentials) is read-only and time-bounded. The code layers do not rely on this, and the integration tests check it separately.
Database errors reach the model as a generic message. The SQLSTATE and message go to the audit log: JSON lines on stderr, because stdout is the MCP stdio channel.
{"ts":"2026-09-25T16:30:15.850Z","event":"tool_call","tool":"find_people","args":{"name_or_email":"rivera","limit":25},"rowCount":1,"durationMs":3.1,"outcome":"ok","hasMore":false,"truncated":false,"responseBytes":157}Tools
Tool | Parameters | Returns |
| none | code, name, city, count of non-retired devices |
|
| hostname, site, os, status, ip, last_seen_at |
|
| device, site, and the assigned person's name and email |
|
| full_name, email, department, site |
|
| name, version, installed_at |
|
| non-retired devices not seen for |
|
| ticket_id, title, priority, status, site, hostname, opened_at |
site is a site code such as north-branch. limit is 1-100, default 25.
All tools are annotated readOnlyHint: true.
Every tool declares an outputSchema derived from its column list, and a
successful result carries the same body twice: as structuredContent
(validated against that schema by the SDK) and as JSON text for clients that
do not read structured output:
{"rowCount":1,"hasMore":false,"truncated":false,"rows":[{"full_name":"Sam Rivera","email":"sam.rivera@example.com","department":"IT","site":"north-branch"}]}hasMore: true means more rows matched than were returned. truncated: true
means the size limits cut a value or dropped rows (layer 6 above). Cells are JSON
scalars: strings, numbers, booleans or null; timestamps are ISO 8601
strings.
Quick start
Requirements: Node.js 22 or later, and Docker for the local database.
The package is not published to npm yet, so run it from a clone:
git clone https://github.com/JimmyAlter/guarded-sql-mcp.git
cd guarded-sql-mcp
docker compose up -d # postgres:16 with db/schema.sql, roles.sql, seed.sql
npm ci
npm run build # produces dist/index.jsThe server reads its settings from environment variables and does not load
.env files. .env.example lists them, and the MCP client
passes them (see below). Always connect as mcp_readonly, never as the owner.
At startup the server logs a privilege_warning (src/roleCheck.ts)
if the role is a superuser, has BYPASSRLS, is a member of pg_read_all_data
or pg_write_all_data, can CREATE in a schema or create temporary tables,
can write to any table in public, has any privilege on a table outside the
allowlist (such as api_tokens), or can SELECT a column whose name looks
sensitive (such as people.password_hash). The integration tests check that
mcp_readonly gets no warnings and that the owner gets all of them.
Variable | Default | |
| required | A |
|
| Per-statement timeout inside each transaction (1-600000) |
|
| Upper bound on one tool result's JSON body (4096-1048576) |
|
| Longest string a cell may carry before it is cut (100-100000) |
| off |
|
Claude Code:
claude mcp add --transport stdio guarded-sql \
--env DATABASE_URL=postgres://mcp_readonly:mcp_readonly_dev@localhost:5432/inventory \
-- node /absolute/path/to/guarded-sql-mcp/dist/index.jsClaude Desktop (or any client that takes an mcpServers block):
{
"mcpServers": {
"guarded-sql": {
"command": "node",
"args": ["/absolute/path/to/guarded-sql-mcp/dist/index.js"],
"env": {
"DATABASE_URL": "postgres://mcp_readonly:mcp_readonly_dev@localhost:5432/inventory"
}
}
}
}Once the package is published to npm, npx -y guarded-sql-mcp will replace
node /absolute/path/to/guarded-sql-mcp/dist/index.js in both snippets. Until
then that command does not work.
The password in db/roles.sql and the compose file is for local development.
Anywhere else, set a real one with ALTER ROLE mcp_readonly PASSWORD '...'.
Demo
scripts/demo.mjs connects a real MCP SDK client to the
built server over an in-memory transport and makes five calls. The only stand-in
is the database: a fake executor answers every query with one fixed row copied
from db/seed.sql and records whether it was called, so the output is the same
on every run and needs no PostgreSQL. This is not an LLM session; it shows
exactly what any MCP client, model-driven or not, gets back.
npm run build && npm run demoThe output below was produced by that command, unedited (audit records are
printed without their ts and durationMs fields, which change every run).
CI runs node scripts/demo.mjs --check and fails if it drifts:
tools/list -> 7 tools: list_sites, search_devices, get_device, find_people, device_software, stale_devices, open_tickets
## A normal call
tools/call find_people {"name_or_email":"rivera"}
isError: false
text: {"rowCount":1,"hasMore":false,"truncated":false,"rows":[{"full_name":"Sam Rivera","email":"sam.rivera@example.com","department":"IT","site":"north-branch"}]}
structuredContent equals text: true
database reached: yes
audit: {"event":"tool_call","tool":"find_people","args":{"name_or_email":"rivera","limit":25},"rowCount":1,"outcome":"ok","hasMore":false,"truncated":false,"responseBytes":157}
## Refused: unknown key `sql`
tools/call get_device {"hostname":"nb-lt-001","sql":"SELECT token_hash FROM api_tokens"}
isError: true
text: MCP error -32602: Input validation error: Invalid arguments for tool get_device: Unrecognized key: "sql"
database reached: no
audit: (none: refused before the handler ran)
## Refused: limit above the cap
tools/call search_devices {"limit":1000}
isError: true
text: MCP error -32602: Input validation error: Invalid arguments for tool search_devices: Too big: expected number to be <=100 at limit
database reached: no
audit: (none: refused before the handler ran)
## Refused: injection-looking hostname
tools/call get_device {"hostname":"x'; DROP TABLE devices;--"}
isError: true
text: MCP error -32602: Input validation error: Invalid arguments for tool get_device: hostname: 1-63 letters, digits or hyphens at hostname
database reached: no
audit: (none: refused before the handler ran)
## Refused: a tool that does not exist
tools/call run_sql {"sql":"SELECT 1"}
isError: true
text: MCP error -32602: Tool run_sql not found
database reached: no
audit: (none: refused before the handler ran)Invalid arguments are refused by the SDK against the tool's strict schema before the handler runs, so they never reach the database and are not in the audit log (see Limitations).
Adding a query
Add an entry with defineQuery in src/catalog.ts and append
it to CATALOG:
export const devicesByPerson = defineQuery({
name: 'devices_by_person',
title: 'Devices by person',
description: 'List devices assigned to a person, by exact email address.',
input: z.strictObject({
email: z.string().max(120).regex(/^[^\s@]+@[^\s@]+$/),
limit,
}),
sql: `
SELECT d.hostname, d.os, d.status
FROM devices d
JOIN people p ON p.id = d.assigned_person_id
WHERE lower(p.email) = lower($1)
ORDER BY d.hostname
LIMIT $2`,
params: (i) => [i.email, fetchLimit(i.limit)],
tables: ['devices', 'people'],
columns: ['hostname', 'os', 'status'],
example: { email: 'sam.rivera@example.com' },
});If the query needs a table or column the role cannot read, update
db/roles.sql as well. The integration suite runs every entry's example
against the seeded database and expects rows back.
What the validator rejects
Rule | Example |
| Declares or reads |
| SQL reads a table missing from |
| Comma joins, schema-qualified or quoted names, |
| A declared column, or any identifier in the SQL, matching |
| Anything that does not start with |
|
|
|
|
| No trailing |
| A trailing |
| Comments, dollar quoting, unbalanced quotes |
|
|
|
|
| Self-explanatory |
Testing
npm run typecheck # tsc --noEmit, strict
npm test # unit + protocol tests, no database needed
npm run lint # typescript-eslint, strict-type-checked
npm run build && npm run smoke # start dist/index.js over stdio and check tools/list
npm run demo # the transcript in the Demo section (after build)
npm run test:integration # needs DATABASE_URL (as mcp_readonly); skipped otherwiseWhat each suite shows:
Unit: validator (
test/unit/validateCatalog.test.ts). The real catalog passes. For each rule above, a small inline catalog that breaks it is rejected, includingFROM a JOIN bwith no alias and a sensitive column behind an innocent alias.Unit: inputs (
test/unit/inputs.test.ts). Out-of-range limits, bad hostnames such asx'; DROP TABLE devices;--, overlong and control-character strings, unknown keys likesqlare all rejected.%and_are escaped, and everyILIKEhas a matchingESCAPE '\'.Unit: executor (
test/unit/executor.test.ts). Projection keeps only declared columns and drops sensitive keys at any depth. The row cap holds when the executor returns 500 rows. Arguments reach the executor only as bind parameters. Against a recording fake pool,PgExecutorissuesBEGIN READ ONLY, the timeout, the query,COMMIT, andROLLBACKon error.Unit: response size (
test/unit/response.test.ts,test/unit/config.test.ts). Long cells are cut without splitting a surrogate pair, trailing rows are dropped until the body fits, escaped characters are counted at their serialized size, and one over-wide row yields zero rows rather than an oversized response. Out-of-range limits in the environment stop the server from starting.Protocol (
test/protocol/server.test.ts). A real SDKClientconnects overInMemoryTransport.tools/listis exactly the catalog, and every schema is closed and bounded. Invalid arguments and unknown tools are refused before the executor runs. When a fake executor returnspassword_hash,mfa_secretandtoken_hash, none of them reaches the client. Database errors come back generic. Each tool'soutputSchemalists exactly its columns, andstructuredContentequals the JSON text. An executor that returns 101 rows of ~1 MB each produces a response under 64 KiB, flaggedtruncatedand audited.Integration (
test/integration/database.test.ts). Runs against PostgreSQL loaded withdb/*.sql, connected asmcp_readonly. Every tool returns rows with no sensitive keys and none of the seeded secret values. Writes through the executor fail with25006(read-only transaction), and the timeout cancelspg_sleep. A fresh session asmcp_readonlyshowsdefault_transaction_read_only = on, and writes still get42501inside an explicitREAD WRITEtransaction. The role gets42501onapi_tokens, onpeople.password_hash, and onSELECT *orrow_to_json(p)frompeople. Injection-looking inputs return zero rows, not errors.
CI (.github/workflows/ci.yml) runs typecheck, lint, tests,
build and the stdio smoke test on Node 22 and 24. It also runs the integration
suite against a postgres:16 service, with REQUIRE_INTEGRATION=1 so a
missing database fails the job instead of skipping it.
Limitations and non-goals
Not a general SQL tool. If a question is not covered by the catalog, the answer is a new reviewed entry, not a more flexible tool.
PostgreSQL only. The pattern works for SQL Server too, but this repository does not implement it.
The validator is a guard, not a parser. It uses regular expressions over a small catalog that humans review, and it fails closed on constructs it cannot verify (
EXTRACT(x FROM y),IS DISTINCT FROM, comma joins). Never use it to vet SQL from a user or a model. It also cannot see whole-row references such asSELECT p FROM people p. The column grants catch those, and the integration tests show it.Projection works on names, not content. It removes keys that look sensitive. It cannot know whether an innocently named column holds a secret.
Tool output is untrusted too. Names, emails and ticket titles are returned to the model by design, and a ticket title can itself contain a prompt injection. This server does not sanitize content. The client must treat tool results as data.
Refused calls are not audited. The SDK answers calls with invalid arguments or unknown tool names before the handler runs, so they do not appear in the audit log. Calls that pass validation are always logged.
The byte cap is per body, and the body is sent twice.
MAX_RESPONSE_BYTESbounds the JSON body; the same body goes out asstructuredContentand as text, so a result on the wire is at most about twice the cap plus the JSON-RPC envelope.The audit log keeps arguments verbatim by default. Search text such as a person's name or email in
find_peopleends up in the log as typed. SetAUDIT_REDACT_ARGS=1to log every string argument as"[redacted]"(numbers such aslimitare kept). Results are never logged, only row counts and sizes.No per-user authorization. It is a local stdio server. Whoever can start it gets the database role's access.
License
MIT © Thiago Langone
Available Tools
7 toolsdevice_softwareDevice softwareARead-onlyIdempotent
List software installed on a device, optionally filtered by package name.
Inputs: hostname (required), limit, name_contains. Returns: rows, hasMore, rowCount, truncated
find_peopleFind peopleARead-onlyIdempotent
Find people whose name or email contains the given text. Returns name, email, department and site.
Inputs: name_or_email (required), limit. Returns: rows, hasMore, rowCount, truncated
get_deviceGet deviceARead-onlyIdempotent
Get one device by hostname, including its site and the name and email of the person it is assigned to.
Inputs: hostname (required). Returns: rows, hasMore, rowCount, truncated
list_sitesList sitesARead-onlyIdempotent
List all sites with their code, city and number of non-retired devices.
Inputs: no named parameters. Returns: rows, hasMore, rowCount, truncated
open_ticketsOpen ticketsARead-onlyIdempotent
List tickets that are not closed, highest priority first, optionally filtered by site and priority.
Inputs: site, limit, priority. Returns: rows, hasMore, rowCount, truncated
search_devicesSearch devicesARead-onlyIdempotent
Search devices by site, status and operating system. Returns hostname, site, OS, status, IP and last check-in time.
Inputs: site, limit, status, os_contains. Returns: rows, hasMore, rowCount, truncated
stale_devicesStale devicesARead-onlyIdempotent
List non-retired devices that have not checked in for at least the given number of days (or never), oldest first.
Inputs: days, site, limit. Returns: rows, hasMore, rowCount, truncated
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 7 tools
Each tool targets a distinct resource or query need: sites, device search, single device, people search, software, stale devices, and open tickets. The only potential overlap between search_devices and stale_devices is resolved by stale_devices' unique time-since-check-in criterion.
Four tools follow a clear verb_noun pattern (list_sites, search_devices, get_device, find_people), but device_software, stale_devices, and open_tickets are noun phrases with no leading verb. Snake_case is consistent, but the overall convention is mixed.
Seven tools is well-scoped for a read-only IT asset and ticketing query server. Each tool addresses a distinct query dimension without redundancy or bloat.
The surface covers core read queries across sites, devices, people, software, and tickets, which is strong for a guarded SQL query server. Minor gaps include no ticket detail/closed-ticket query, no person detail beyond search, and no write operations.
Maintenance
Related MCP Connectors
Safe, read-only Postgres and MySQL access for AI agents. Audit log + column-level controls.
Query PostgreSQL databases in plain English — LLM-generated, safety-validated SQL.
Deterministic safety, correctness & cost gate that vets Postgres SQL before your AI agent runs it.
Security layer for AI agents: Retrieve for controlled SQL reads, Guard for allow/deny writes.
Related MCP Servers
- AlicenseNot gradedqualityBmaintenanceProvides a read-only PostgreSQL SQL surface for LLM agents via MCP, with defense-in-depth security layers for safe database queries.124 PyPI3MIT
- FlicenseNot gradedqualityDmaintenanceEnables LLM agents to explore and query a Postgres database through secure, read-only tools.-
- AlicenseAqualityBmaintenanceEnables AI assistants to discover allowed PostgreSQL schemas, run parameterized SELECT queries, and inspect query plans safely through parser-checked read-only transactions, timeouts, row caps, and role-based access controls.4MIT
- AlicenseNot gradedqualityBmaintenanceEnables LLM agents to run read-only SQL against PostgreSQL with safety enforced by the database itself — forced row-level security, signed tenant context, role limits, and fail-closed auditing. Queries stay confined to the rows and columns a principal may read, cannot modify or exhaust the database, and remain audited even when the SQL validator is switched off.MIT