mcp-database

mcp-database

MCP server for multiple databases (PostgreSQL, MySQL, MariaDB, SQLite, MongoDB, Redis) with tools for schema inspection, querying, performance diagnostics, and safe write operations, featuring access modes, PII masking, and audit logging.

Category
访问服务器

README

mcp-database

CI Python 3.12+ License: Apache 2.0

MCP database server for PostgreSQL, MySQL, MariaDB, SQLite, MongoDB, and Redis. One connection per instance, per-connection access modes, and a full performance-diagnosis toolkit for dev, SRE, and DBA workflows.

Contents

Supported databases

One connection per instance; the engine is inferred from the URI scheme (override with ENGINE_TYPE).

Database URI scheme ENGINE_TYPE Driver Highlights
PostgreSQL postgresql://, postgres:// postgres psycopg 3 pgvector KNN, native full-text search, HypoPG hypothetical indexes, transactional DDL dry-run, planner column stats
MySQL mysql:// mysql aiomysql FULLTEXT search, planner column stats + histograms, performance_schema diagnostics
MariaDB mariadb:// mariadb aiomysql Follows the MySQL surface with the engine differences absorbed (max_statement_time, SHOW SLAVE HOSTS)
SQLite sqlite:///relative.db, sqlite:////absolute.db sqlite aiosqlite File databases with structurally read-only reads (mode=ro), FTS5 text search, EXPLAIN QUERY PLAN
MongoDB mongodb://, mongodb+srv:// mongodb PyMongo (async) Atlas $search / $vectorSearch, aggregation reads, replica-set / shard topology
Redis redis://, rediss:// redis redis-py (async) Allowlisted command envelope (FLUSH*/EVAL/KEYS never allowed), SCAN-based inspection, SLOWLOG/INFO/ACL diagnostics

Engine support per tool (some features are engine-specific — e.g. HypoPG is PostgreSQL-only) is in docs/tools.md. CI verifies every tool against real PostgreSQL 16/17, MySQL 8.0/8.4, MariaDB 10.11/11.4, SQLite, MongoDB 7/8, and Redis 7/8 (plus pgvector and Atlas-local images) on every push.

Features

  • 39 tools, risk-encoded names. db_schema_* / db_read_* / db_perf_* never write (safe to always-allow); db_write_* / db_admin_* need approval.
  • Vector + full-text search. pgvector KNN and FTS (PostgreSQL), FULLTEXT (MySQL), Atlas $search / $vectorSearch (MongoDB), plus index inspection and quality diagnostics.
  • Cluster tooling. Replication-lag and topology triage (db_perf_cluster) with audited actions: promote, start/stop replica, step down, freeze.
  • Access modes per connection. read_write, read_only, monitor (perf analysis with no data access, for production with PII).
  • Fail-closed read validation. sqlglot rejects writes, DDL, multi-statement and dangerous functions; MongoDB is limited to a read-only op allowlist.
  • Fine-grained write control. write_ops limits which operations run; database_modes sets a different mode per database.
  • Write safety belts. dry_run (execute, roll back, report impact) and expected_max_rows (auto-abort oversized writes).
  • PII masking. redact_fields masks values with ***; ["*"] shows type placeholders only, never values.
  • Audit log. Every write, admin and export call is logged as JSONL (mode 600); literals become ?, so PII never touches disk.
  • Result limits. 500 rows / 1 MiB per response with source-side LIMIT injection; db_read_export streams big results to JSON/CSV.

Tools

Full reference with parameters and per-database support indicators: docs/tools.md.

Class Tools Safe to always-allow
db_schema_ connections, databases, objects, describe, ddl, search, relationships, users, grants, search_indexes
db_read_ query, sample, export, vector_search, text_search
db_perf_ explain, column_stats, diagnose, top_queries, active_ops, blocking, index_stats, table_stats, health, replication, settings, logs, vector_stats, cluster
db_write_ query (with dry_run / expected_max_rows), search_index case by case
db_admin_ kill, analyze, maintain, cluster case by case

Access-mode matrix:

Class read_write read_only monitor
db_schema_ ✅ (no data sampling)
db_read_
db_perf_
db_write_ / db_admin_

Quick start

Requirements: Docker (runs both the playground databases and the published server image). uv is only needed for local development.

# 1. Clone (for the playground compose file and seed data)
git clone https://github.com/DiegoBulhoes/mcp-database.git && cd mcp-database

# 2. Start and seed the playground databases (PostgreSQL + MySQL + MongoDB)
make up && make mongo-rs && make seed

# 3. Register with Claude Code — runs the published image, one connection per server entry.
#    All configuration lives in --env; the docker -e flags are a fixed forwarding template.
claude mcp add db-postgres \
  --env URI="postgresql://dev:dev@localhost:5432/app" \
  --env ENGINE_TYPE=postgres \
  --env MODE=read_write \
  -- docker run -i --rm --network host -e URI -e ENGINE_TYPE -e MODE -e NAME \
       ghcr.io/diegobulhoes/mcp-database:latest

--network host lets the container reach localhost databases (Linux). On macOS/Windows Docker Desktop, drop it and use host.docker.internal in the URI instead.

Then ask Claude things like "why is pg_app slow?" and it will chain db_perf_active_opsdb_perf_blockingdb_perf_top_queriesdb_perf_explain without a single permission prompt (see below).

Querying

You don't call the tools yourself; your AI assistant does, picking the connection by name (it discovers what exists via db_schema_connections). You just ask in natural language:

You ask The assistant calls
"how many orders over 100 in pg_app?" db_read_query(conn="pg_app", query="SELECT count(*) FROM orders WHERE total > 100")
"top pages by clicks in mongo_app" db_read_query(conn="mongo_app", query={"collection": "events", "operation": "aggregate", "pipeline": [{"$group": {"_id": "$page", "n": {"$sum": 1}}}]})
"why is pg_app slow?" db_perf_active_opsdb_perf_blockingdb_perf_top_queriesdb_perf_explain (the SRE runbook, no prompts)
"upgrade user 42 to the pro plan" db_write_query(conn="pg_app", query="UPDATE users SET plan = 'pro' WHERE id = 42", expected_max_rows=1) (this one asks for your approval)

The query argument depends on the connection type:

  • PostgreSQL / MySQL: a SQL string. db_read_query accepts only SELECT/UNION/VALUES (parser-validated, fail-closed); everything else goes through db_write_query.
  • MongoDB: a JSON object {"collection", "operation", ...}. Reads: find, aggregate, countDocuments, distinct, listIndexes; writes (via db_write_query): insertMany, updateMany, deleteMany, createIndex.

Example query values:

SELECT id, total FROM orders WHERE total > 100 ORDER BY total DESC LIMIT 20
{"collection": "events", "operation": "aggregate",
 "pipeline": [{"$group": {"_id": "$page", "n": {"$sum": 1}}}, {"$sort": {"n": -1}}]}

Write safety belts on db_write_query:

  • Unbounded-mutation guard: an UPDATE/DELETE with no WHERE (or a MongoDB updateMany/deleteMany with an empty filter) is rejected outright unless the caller declares intent — either a dry_run or an expected_max_rows. This stops a careless model from wiping a whole table without saying so. (DROP/TRUNCATE are explicit by nature and stay allowed.)
  • dry_run: true executes inside a transaction and rolls back, reporting how many rows would be affected.
  • expected_max_rows: N aborts with rollback if the write would affect more than N rows (catches a missing WHERE before it hurts).

References

Projects and resources that shaped this server's design:

  • Model Context Protocol: protocol specification and the Python SDK (FastMCP) this server is built on.
  • anthropics/skills (Anthropic): two skills from this repo are bundled in .claude/skills/: mcp-builder, whose best practices this server was audited against (tool naming with the db_ service prefix, parameter descriptions, annotations, actionable errors, and the agent evaluations format), and algorithmic-art for generative-art sessions.
  • postgres-mcp (Crystal DBA): inspiration for access modes, safe SQL execution, and the performance/health tool set.
  • mongodb-mcp-server (MongoDB): inspiration for the export tool, byte-based response limits, and server log access.
  • mcp-server-mysql (Ben Borla): inspiration for per-operation write permissions (write_ops) and per-database modes (database_modes).

License

Apache 2.0

推荐服务器

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

官方
精选