Postgres MCP Server

Postgres MCP Server

Enables querying and modifying PostgreSQL databases through MCP tools with read/write operations, schema inspection, and write-safety constraints that limit modifications to the mcp schema.

Category
访问服务器

README

Postgres MCP server & client

This folder contains an MCP server exposing Postgres tools and a tiny CLI client for quick testing. The server reads connection details from .env.

Setup

  • Ensure .env contains DB_ADDRESS, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD.
  • Install deps (already installed in this workspace): pip install -r requirements.txt.

Run the server

python mcp_postgres_server.py

It starts an MCP stdio server named postgres-mcp with tools: describe_database, run_read_query, run_write_query, and health_check. Writes are confined to the mcp schema and multi-statement input is rejected.

CLI testing

Use the CLI to spawn the server on demand and call tools:

python cli.py list-tools
python cli.py describe --schema ehr
python cli.py read "select * from mcp.example_table" --limit 25
python cli.py write "insert into example_table (col) values ('hello')"
# Or call any tool with raw JSON args
python cli.py call run_read_query --args '{"sql": "select now()"}'

Run as a systemd service

  1. Update systemd/postgres-mcp.service if your python path, user/group, or working directory differ. It expects /home/remote/miniconda3/bin/python, user remote, and .env at /home/data/NUHS/mcp/.env.
  2. Install and start:
chmod +x install_service.sh
./install_service.sh

(You’ll be prompted for sudo.) 3) Check status/logs:

sudo systemctl status postgres-mcp
journalctl -u postgres-mcp -f

Run over HTTP (streamable)

For MCP clients that need HTTP/SSE, start the server in streamable HTTP mode:

python mcp_postgres_server.py --transport streamable-http --host 0.0.0.0 --port 8000

Expose the port via Tailscale/VPN or your network. Keep the .env in place so the server can reach Postgres. If you switch the systemd service to HTTP, update ExecStart accordingly (same flags as above).

Authentication (Bearer)

If you set MCP_API_KEY in .env, the HTTP mode will require Authorization: Bearer <MCP_API_KEY> on every request. Stdio mode does not use the token.

Debugging HTTP connectivity

Use the helper script to test the HTTP endpoint (works against OpenWebUI-style connections):

python debug_http_client.py --url http://127.0.0.1:8000/mcp --token "$MCP_API_KEY"
# For Tailscale:
python debug_http_client.py --url http://100.x.x.x:8000/mcp --token "$MCP_API_KEY"

The script initializes a session and lists tools. If it fails, it will print the exception so you can check networking, port, or auth issues.

Using with different MCP clients

  • OpenWebUI (agent/MCP integration): In Settings → MCP, add a server entry with command python /home/data/NUHS/mcp/mcp_postgres_server.py (adjust to your python path). OpenWebUI will spawn the stdio server locally. If OpenWebUI runs on another machine, configure it to SSH/Tailscale into this host and run the same command so it can reach Postgres. For network-only mode, point it at the HTTP endpoint you start with --transport streamable-http.
  • Other stdio-capable clients (Cursor/Claude Desktop/etc.): Configure the MCP server command to /home/remote/miniconda3/bin/python /home/data/NUHS/mcp/mcp_postgres_server.py with working directory /home/data/NUHS/mcp. Ensure DB_* vars are available via .env or exported into the client’s environment before launch.
  • Network-only MCP clients: Start with the HTTP mode above (--transport streamable-http --host ... --port ...), open that port via Tailscale/VPN, and point the client at the resulting endpoint. If MCP_API_KEY is set, send Authorization: Bearer <MCP_API_KEY> in requests. Preserve the write-safety rules if you expose it.

Connecting a chat client (via Tailscale)

This MCP server speaks stdio by default. Most chat clients that support MCP spawn a command. To use it on a Tailscale node:

  • Install and log in to Tailscale on the server host; note its Tailscale IP (e.g., 100.x.x.x) from tailscale ip -4.
  • Enable and start the service on that host (./install_service.sh or sudo systemctl start postgres-mcp).
  • From your dev machine, use Tailscale SSH to run the client or CLI directly on the host: ssh remote@100.x.x.x 'cd /home/data/NUHS/mcp && python cli.py list-tools'.
  • If your chat app supports running an MCP server command remotely, configure it to SSH into the Tailscale IP and execute /home/remote/miniconda3/bin/python /home/data/NUHS/mcp/mcp_postgres_server.py.
  • For HTTP mode, run the server with --transport streamable-http --host 0.0.0.0 --port 8000 and point clients to http://100.x.x.x:8000/mcp (adjust port/host). If MCP_API_KEY is set, include Authorization: Bearer <MCP_API_KEY>.

Suggested system prompt for LLMs

If your chat client lets you provide a system prompt for MCP, start with:

You are an assistant that uses the "postgres-mcp" server to inspect and modify data in Postgres.
- Call "describe_database" first to understand schemas and tables.
- Use "run_read_query" for SELECTs; it auto-applies a LIMIT if missing.
- Use "run_write_query" only for writes inside the mcp schema; other schemas are blocked.
- Do not send multiple statements in one call.
- Prefer parameterized SQL and avoid destructive changes outside the mcp schema.

If connecting over HTTP and MCP_API_KEY is set, include Authorization: Bearer <MCP_API_KEY>; stdio mode does not need it.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选