dbmcp
Enables querying PostgreSQL databases via MCP, with multi-database routing, credential isolation, and truncated results plus full CSV export.
README
dbmcp
A small MCP server that proxies SQL queries to PostgreSQL. It:
- Routes to multiple databases by name — callers pick
devDb,stageDb, etc. (case-insensitive). Each can live on a different host with different credentials; the caller only ever knows the name. - Isolates credentials — the caller sends SQL and gets results back; the connection strings live only in the server's environment and are never returned.
- Enforces a hard timeout — every query is capped (default 30s) both by
PostgreSQL
statement_timeoutand by an in-process backstop. - Shrinks output — the inline response is kept under 1000 characters: each cell is truncated to the first 100 characters, and only the leading rows that fit are returned.
- Reports what was cut — metadata includes the total row count, which columns were truncated, and whether rows were omitted.
- Exports the full result as a public CSV — the complete, untruncated result
is written to
/files/<uuid>.csv, served from the same server so callers can fetch andgrepit.
Everything runs inside Docker — no Node packages are installed on the host.
Run
cp .env.example .env # set DATABASE_URL etc.
docker compose up --build # builds the image and runs the server
This starts:
dbmcp— the MCP server, mapped to host port${HOST_PORT:-3991}.devdb,stagedb— two optional local PostgreSQL servers (different users, passwords, and hostnames) for testing multi-database routing. In real use, delete them and point theDB_*_URLenv vars at your actual hosts.
Configuring databases
Each database is one env var following the DB_<NAME>_URL convention; the
<NAME> becomes the case-insensitive name callers use:
DB_DEVDB_URL=postgres://devuser:devpass@dev-host:5432/dev # -> "devDb"
DB_STAGEDB_URL=postgres://stageuser:secret@stage-host:5432/app # -> "stageDb"
DB_PRODDB_URL=postgres://readonly:secret@prod-host:5432/app # -> "prodDb"
Optionally set DATABASE_URL as the unnamed default used when a caller omits
database. If exactly one database is configured, it is the default
automatically.
Node is pinned to
node:24.16.0-alpine(the latest published Node 24 LTS;24.17.0is not yet on Docker Hub).
MCP endpoint
Streamable HTTP, stateless, at POST /mcp. Two tools:
| Tool | Input | Returns |
|---|---|---|
query |
sql (string), database (string, see below) |
truncated preview + metadata + public CSV URL |
list_databases |
none | available database names + the default (names only — never credentials) |
database selects which configured database to run against (case-insensitive).
It is optional when a default exists, otherwise required.
Example (raw JSON-RPC over HTTP):
curl -s -X POST http://localhost:3991/mcp \
-H 'Content-Type: application/json' \
-H 'Accept: application/json, text/event-stream' \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/call",
"params":{"name":"query","arguments":{"database":"devDb","sql":"SELECT * FROM demo"}}}'
Response payload (inside the MCP tool result text):
{
"columns": ["id", "big_text", "label"],
"rows": [["1", "xxxx…", "row-1"], ...],
"metadata": {
"database": "devDb",
"totalRows": 50,
"returnedRows": 6,
"truncatedColumns": ["big_text"],
"cellsTruncated": true,
"rowsOmitted": true,
"csvUrl": "http://localhost:3991/files/<uuid>.csv",
"note": "Showing first 6 of 50 rows. Fetch <url> for the full result."
}
}
Fetch the full result:
curl -s http://localhost:3991/files/<uuid>.csv | grep something
Connecting a client
The server speaks Streamable HTTP at http://localhost:3991/mcp. Start it
first (docker compose up), then point your client at that URL. None of the
options below require Node on your host — they connect over HTTP directly.
Claude Code (CLI)
claude mcp add --transport http dbmcp http://localhost:3991/mcp
claude mcp list # should show dbmcp as connected
Or add it to a project's .mcp.json:
{
"mcpServers": {
"dbmcp": { "type": "http", "url": "http://localhost:3991/mcp" }
}
}
Cursor (cursor-cli)
Add the server to ~/.cursor/mcp.json (global) or .cursor/mcp.json (project):
{
"mcpServers": {
"dbmcp": { "url": "http://localhost:3991/mcp" }
}
}
Then list tools from the CLI:
cursor-agent mcp list
Codex (CLI)
Codex reads ~/.codex/config.toml. Recent versions speak Streamable HTTP
natively:
experimental_use_rmcp_client = true
[mcp_servers.dbmcp]
url = "http://localhost:3991/mcp"
codex mcp list # verify dbmcp shows up
Fallback: older clients that only support stdio
If a client can't talk HTTP directly, bridge stdio→HTTP with mcp-remote
(this one does run a Node helper on the host):
// Claude Code / Cursor
{ "mcpServers": { "dbmcp": {
"command": "npx", "args": ["-y", "mcp-remote", "http://localhost:3991/mcp"]
} } }
# Codex (~/.codex/config.toml)
[mcp_servers.dbmcp]
command = "npx"
args = ["-y", "mcp-remote", "http://localhost:3991/mcp"]
Once connected, call list_databases to see available names, then query
with a database and sql.
Reaching it from another container
The server binds to 0.0.0.0, and the port is published on the host, so other
containers can reach it via host.docker.internal:
curl http://host.docker.internal:3991/mcp ...
On Docker Desktop this name resolves automatically. On plain Linux, give the calling container the host gateway mapping:
# in the consuming container's compose service
extra_hosts:
- "host.docker.internal:host-gateway"
So that the exported CSV links are fetchable from those containers (rather than
pointing back at the caller's own localhost), set on this server:
PUBLIC_BASE_URL=http://host.docker.internal:3991
If the caller is part of this compose project, it can also just use the service name directly:
http://dbmcp:3991/mcp.
Configuration (env)
| Variable | Default | Purpose |
|---|---|---|
HOST_PORT |
3991 |
Host port mapped to the container. |
DB_<NAME>_URL |
— | A named database connection (server-side only). |
DATABASE_URL |
— | Optional unnamed default database. |
QUERY_TIMEOUT_MS |
30000 |
Hard per-query timeout. |
MAX_OUTPUT_CHARS |
1000 |
Inline payload cap. |
MAX_CELL_CHARS |
100 |
Per-cell truncation length. |
PUBLIC_BASE_URL |
http://localhost:3991 |
Base URL used in CSV links. |
Security notes
- Callers never receive the connection string or password — only query results.
- The server runs arbitrary SQL as the configured role, so connect it with a least-privilege (ideally read-only) database user.
- Exported CSVs are world-readable by anyone who can reach
/files/<uuid>.csv; the filename is an unguessable UUID, but treat the endpoint as public.
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。