ask-your-database

ask-your-database

Enables natural language queries against a Postgres database through MCP tools for listing tables, describing schemas, and running SQL queries, with write operations blocked for safety.

Category
访问服务器

README

Ask Your Database — MCP Server

A local MCP server that lets Claude Desktop or Claude Code answer plain-English questions against a Postgres database — e.g. "which customers spent less than $2,000 in the last year?" — by calling real tools, not by guessing.

Tested end-to-end: schema discovery, multi-step joins, and a blocked write all verified against a live Postgres instance.

What's here

mcp-demo/
├── server.py           # MCP server — exposes list_tables, describe_table, query
├── db.py                # connection + safety layer (read-only role, timeout, write-block)
├── seed.sql              # schema + sample data + read-only role setup
├── docker-compose.yml    # spins up Postgres with the seed data pre-loaded
├── requirements.txt       # mcp[cli], psycopg2-binary
└── README.md

1. Start Postgres

cd mcp-demo
docker compose up -d

This starts Postgres on localhost:5432, creates the demo database, loads the sample schema (customers, subscriptions, support_tickets), and creates a mcp_readonly role that can only SELECT.

No Docker? Run psql against any local Postgres instance and load seed.sql directly: psql -U postgres -d demo -f seed.sql (creates the database first if needed).

2. Set up Python

python3 -m venv venv
source venv/bin/activate        # Windows: venv\Scripts\activate
pip install -r requirements.txt

3. Point the server at your database

The server reads connection details from environment variables (defaults shown match seed.sql and docker-compose.yml, so you likely don't need to change anything for local testing):

Variable Default
PGHOST localhost
PGPORT 5432
PGDATABASE demo
PGUSER mcp_readonly
PGPASSWORD readonlypass

4. Smoke-test it standalone (optional but recommended)

export PGHOST=localhost PGPORT=5432 PGDATABASE=demo PGUSER=mcp_readonly PGPASSWORD=readonlypass
python3 server.py

It should sit there quietly waiting on stdio — that's correct, it's not meant to print anything until a client (like Claude Desktop) talks to it. Ctrl+C to stop.

5. Connect it to Claude Desktop

Open Claude Desktop's config file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json

Add this server (use the absolute path to server.py on your machine):

{
  "mcpServers": {
    "ask-your-database": {
      "command": "/absolute/path/to/mcp-demo/venv/bin/python3",
      "args": ["/absolute/path/to/mcp-demo/server.py"],
      "env": {
        "PGHOST": "localhost",
        "PGPORT": "5432",
        "PGDATABASE": "demo",
        "PGUSER": "mcp_readonly",
        "PGPASSWORD": "readonlypass"
      }
    }
  }
}

Restart Claude Desktop. You should see "ask-your-database" listed under the 🔌 tools/connectors icon in a new chat.

6. Connect it to Claude Code

From the project directory:

claude mcp add ask-your-database \
  --env PGHOST=localhost --env PGPORT=5432 --env PGDATABASE=demo \
  --env PGUSER=mcp_readonly --env PGPASSWORD=readonlypass \
  -- /absolute/path/to/mcp-demo/venv/bin/python3 /absolute/path/to/mcp-demo/server.py

7. Try it

Ask Claude, in plain English:

"Which customers churned last month, and what plan were they on?"

"Which customers spent less than $2,000 total in the last year?"

Claude will call list_tables and describe_table on its own to learn the schema (nothing about it is hardcoded in the prompt), then write and run the SQL itself via the query tool.

Try a write, too — it's blocked on purpose:

"Delete all customers with status 'churned'."

Adapting this to your real database

Swap the seed data for your actual schema, and point PGHOST/PGDATABASE at your real Postgres instance — using a read-only role, same as here. Never point this server at a production database with a role that can write. db.py's query logic doesn't need to change at all.

Troubleshooting

  • Server doesn't show up in Claude Desktop — check the path in claude_desktop_config.json is absolute, not relative, and that you restarted the app after editing the config.
  • permission denied on every query, even simple ones — check the mcp_readonly role's grants ran (last block of seed.sql); rerun that block if you dropped and recreated the database.
  • Connection refused — confirm Postgres is actually running (docker compose ps) and the port matches PGPORT.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选