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
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。