mcp-data-gateway
Enables AI agents to query a PostgreSQL database through a small set of controlled, read-only tools for schema inspection, row lookup, and aggregate statistics.
README
mcp-data-gateway
A production-minded MCP server that lets AI agents query a PostgreSQL database through a small set of controlled, read-only tools.
The core idea: agents never get raw database access. Every interaction goes through explicitly designed tools with validated inputs and a read-only SQL guard, backed by a least-privilege database role as the authoritative enforcement layer.
Status: in progress. The config, database layer, SQL guard, and data loader are implemented, and the schema, passenger-lookup, and stats tools are exposed over the FastMCP stdio server. An end-to-end smoke test and further hardening remain. See docs/project-plan.md.
Why
Giving an LLM agent a database connection string is easy — and dangerous. This project demonstrates a safer pattern:
- Read-only by design — three independent layers: tool design, a SQL validation
guard as defense-in-depth, and a least-privilege database role as the authoritative
control. The guard is a deny-by-default first filter, not a full SQL parser; the
database role/session (
SELECT-only,default_transaction_read_only) is what ultimately enforces read-only and table access, and is planned for M2. See SECURITY.md. - Small, purposeful tool surface — schema inspection, row lookup, and aggregate stats. No generic "run any SQL" escape hatch for write operations.
- Boring, auditable stack — Python 3.12, psycopg, PostgreSQL, Docker Compose.
The demo dataset is the classic Titanic passenger list: small, well-known, and suitable as a low-risk public demo dataset.
Quickstart
Requires Python 3.12+, Docker, and make.
cp .env.example .env # defaults work for local development
make up # start PostgreSQL via Docker Compose
make install # create venv and install dependencies
make load-data # create the schema, load sample data, set up the reader role
make run # start the MCP server on stdio
make load-data connects as the local admin (POSTGRES_*) only for setup: it
creates the passengers table with a small deterministic sample, then creates
the gateway_reader role with SELECT-only access. The server itself connects
via DATABASE_URL, which points at gateway_reader — never the admin user. The
script is idempotent, so you can re-run it safely.
To use with an MCP-capable client, register the server with a stdio transport
pointing at python -m mcp_data_gateway.server.
Repository layout
src/mcp_data_gateway/
server.py # MCP server entrypoint (stdio)
config.py # environment-based configuration
db.py # connection handling
tools/ # the agent-facing tools
schema.py # describe tables and columns
passengers.py # look up passenger rows
stats.py # aggregate statistics
security/
readonly_sql.py # read-only SQL guard
scripts/ # data loading and smoke test
tests/ # pytest suite
docs/ # architecture, process, decision records
Documentation
Full documentation lives in docs/ and builds into a static
site with MkDocs:
- Architecture — components and the request path
- Security model — the defense-in-depth layers
- Tool reference — the six MCP tools in detail
- Operations — configuration, container, logs, commands
- Validation — what is proven, and how
- Production considerations — demonstrated vs. still required
- Demo walkthrough — run it locally with Docker
- Decisions — key design choices
make docs # build the site into site/
make docs-serve # preview locally with live reload
Development
make test # run pytest
make lint # ruff check + format check
make audit # bandit + pip-audit
pre-commit install # enable git hooks
Engineering conventions are described in docs/engineering-process.md; design decisions are captured as ADRs in docs/decision-records/.
License
MIT
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.