postgres-mcp
Enables read-only, SELECT-only querying of any Postgres database through MCP-compatible clients like Claude, with schema introspection and guarded SQL execution.
README
postgres-mcp
A generic, reusable MCP server for
read-only querying of any Postgres database via Claude (or any
MCP-compatible client). It has no knowledge of any particular schema —
list_tables, describe_table, and run_sql work purely off
information_schema and generic jsonb introspection, so it can be pointed
at any Postgres database without code changes.
Security model
Two independent layers, defense in depth:
- DB-level: the server connects as a role that only has
SELECTgranted — it cannot write even if a query tried to. Create this role withsetup_reader_role.sql. - App-level:
run_sql()rejects anything that isn't a plainSELECT(orWITH ... SELECT), enforces a 200-row cap, and sets a 5-second statement timeout — all as a fast-fail check before even hitting the database.
Setup
- Create a read-only role in your target database:
- Open
setup_reader_role.sql, replace the password placeholder and<your_db_name>with real values, then run it once against your database (e.g. viapsqlor your DB provider's SQL console).
- Open
- Set your connection string in a
.envfile in this directory:
(DATABASE_URL=postgresql://mcp_reader:yourpassword@host:5432/yourdb ANTHROPIC_API_KEY=sk-ant-...ANTHROPIC_API_KEYis only needed forchat.py, not for running the MCP server itself.) - Install dependencies:
pip install -r requirements.txt
Local testing
Test the query layer directly first, without any MCP client in the loop:
python chat.py "what tables do I have?"
Then test it as an actual MCP server with the Inspector:
fastmcp dev inspector mcp_server.py
If the Inspector's Node toolchain gives you trouble (this has happened before), fall back to running the server over HTTP directly and hitting it with the FastMCP Python client:
fastmcp run mcp_server.py --transport http --port 8000
from fastmcp import Client
import asyncio
async def main():
async with Client("http://localhost:8000/mcp") as client:
print(await client.call_tool("list_tables", {}))
asyncio.run(main())
Deployment (Prefect Horizon)
FastMCP's hosted deployment platform is currently called Prefect Horizon (it was previously "FastMCP Cloud" — this has rebranded before, so double-check the current name/URL at gofastmcp.com/deployment before following these steps, in case it's changed again).
- Push this repo to GitHub — a real remote must exist first (see the git commands at the bottom of this README if you haven't already).
- Go to the current platform's site (as of writing, horizon.prefect.io) and sign in with GitHub.
- Connect this repository.
- Configure the deployment:
- Entrypoint:
mcp_server.py:mcp— the:mcppart is the variable name the server object is assigned to in the file (see themcp = FastMCP(...)line inmcp_server.py). If you ever rename that variable or move the file, this entrypoint string must be updated to match exactly. - Authentication: turn this ON. Interactive MCP clients like claude.ai and Claude Desktop require real OAuth discovery endpoints to connect — a server without authentication enabled will not work with those clients, even if it works fine when you test it directly with a raw API call or the FastMCP Python client.
- Environment variables: add
DATABASE_URLin the platform's own dashboard. This is separate from, and does not read, this project's local.envfile — the value must be entered directly in the dashboard for the deployed server to have DB access.
- Entrypoint:
- Deploy, and copy the resulting server URL. It'll look something like
https://<your-server-name>.fastmcp.app/mcp(the exact domain may vary — use whatever the platform actually shows you). - Before connecting it anywhere else, test it with the platform's own
built-in Inspector/testing tool. Call
list_tablesthere first — it needs no arguments, so it's the fastest way to confirm the deployed server can actually reach your database.
Connecting to claude.ai
- Go to claude.ai → Settings → Connectors → Add custom connector.
- Paste the deployed server URL from the deployment step above.
- Complete the OAuth prompt it walks you through.
- Start a new chat (not one that predates the connector being added) and enable the connector in that chat.
- Test it with a simple question like "what tables do I have?"
- If you add new tools later and they don't show up, try "Refresh tools" in the connector's settings before assuming something's broken — this is a known caching behavior, not a bug.
Limits
This is genuinely schema-agnostic, but not limitation-free:
- Postgres-specific. It uses
jsonb_object_keys()and Postgres catalog syntax (information_schema, etc.). Pointing it at MySQL or SQLite would need real code changes todb_tools.py, not just a new connection string. - Schema-agnostic ≠ zero setup per database. Each new target
database still needs its own read-only role created
(
setup_reader_role.sql) and its own deployment (or at minimum its ownDATABASE_URL) pointed at it. This isn't a single server that transparently serves multiple databases.
Git setup
If you're starting from this code without git history yet:
git init
git add .
git commit -m "Initial commit: generic read-only Postgres MCP server"
git branch -M main
git remote add origin <your-repo-url>
git push -u origin main
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。