mcp-database-server
Enables AI agents to query and explore MongoDB databases, including executing queries and retrieving schema details, sample data, and collection/table statistics.
Enables AI agents to query and explore MySQL databases, including executing SQL queries and retrieving schema, table details, sample data, and table statistics.
Enables AI agents to query and explore PostgreSQL databases, including executing SQL queries and retrieving schema, table details, sample data, and table statistics.
This server does not currently have a deployable release on Glama. Follow the repository's setup instructions to install and configure it for your MCP client, including any required credentials and local dependencies.
mcp-database-server
An MCP server that lets AI agents (Claude, Cursor, etc.) query and explore databases safely. Built with TypeScript, supports PostgreSQL, MySQL, and MongoDB.
Why?
I wanted a way to let Claude and other AI tools talk to my databases without giving them raw access. This server sits in between — it validates queries, blocks writes by default, and formats results so the AI can actually understand them.
Related MCP server: mcp-database-server
Architecture
AI Agent (Claude / Cursor / GPT)
│
│ JSON-RPC over stdio (MCP Protocol)
▼
┌─────────────────────────────────────┐
│ MCP DATABASE SERVER │
│ TypeScript · Node.js │
│ │
│ ┌──────────────┐ ┌─────────────┐ │
│ │ Safety Layer │ │ 5 Tools │ │
│ │ │ │ │ │
│ │ · SQL inject │ │ execute_ │ │
│ │ detection │ │ query │ │
│ │ · Write-op │ │ get_schema │ │
│ │ blocking │ │ describe_ │ │
│ │ · Row limits │ │ table │ │
│ │ · Timeouts │ │ sample_data │ │
│ │ · Sanitizer │ │ table_stats │ │
│ └──────────────┘ └─────────────┘ │
└──────────┬──────────────────────────┘
│ DB Adapter (factory pattern)
┌──────┼──────┐
▼ ▼ ▼
[PG] [MySQL] [MongoDB]Each query passes through the safety layer before reaching the database adapter. The adapter pattern means adding a new database is one file with a standard interface.
Features
PostgreSQL, MySQL, MongoDB support
Read-only by default (SELECT only, unless you flip
ALLOW_WRITE)SQL injection detection — blocks dangerous patterns, validates inputs
5 MCP tools: query, schema, describe, sample data, table stats
Output as markdown tables, JSON, or CSV
Schema exposed as an MCP resource
Built-in query helper prompt
Row limits, query timeouts, write protection
Docker setup with sample ecommerce data
Quick Start
# Clone and install
git clone https://github.com/rohitkapoorfriend/mcp-database-server
cd mcp-database-server
npm install
# Configure your database
cp .env.example .env
# Edit .env with your database credentials
# Build and start
npm run build
node build/index.jsInstallation
npm install
npm run buildConfiguration
All configuration is via environment variables:
Variable | Default | Description |
|
| Database type: |
|
| Database host |
|
| Database port |
|
| Database name |
|
| Database user |
| (empty) | Database password |
|
| Enable SSL connection |
|
| Allow INSERT/UPDATE/DELETE queries |
|
| Maximum rows per query result |
|
| Query timeout in milliseconds |
|
| Log level: |
Usage
Claude Desktop
Add to your claude_desktop_config.json:
{
"mcpServers": {
"database": {
"command": "node",
"args": ["/absolute/path/to/mcp-database-server/build/index.js"],
"env": {
"DB_TYPE": "postgresql",
"DB_HOST": "localhost",
"DB_PORT": "5432",
"DB_NAME": "mydb",
"DB_USER": "postgres",
"DB_PASSWORD": "your_password"
}
}
}
}Cursor IDE
Add to your Cursor MCP settings:
{
"mcpServers": {
"database": {
"command": "node",
"args": ["/absolute/path/to/mcp-database-server/build/index.js"],
"env": {
"DB_TYPE": "postgresql",
"DB_HOST": "localhost",
"DB_PORT": "5432",
"DB_NAME": "mydb",
"DB_USER": "postgres",
"DB_PASSWORD": "your_password"
}
}
}
}Claude Code CLI
DB_TYPE=postgresql DB_HOST=localhost DB_NAME=mydb DB_USER=postgres DB_PASSWORD=pass node build/index.jsTools
execute_query
Execute a SQL query against the database.
Input:
{ "query": "SELECT name, email FROM users LIMIT 5", "format": "table" }Output:
name | email
----- | -----------------
Alice | alice@example.com
Bob | bob@example.com
_2 row(s) returned in 12ms._get_schema
Get the full database schema.
Input:
{ "schema": "public" }Output: Markdown with all tables, columns, types, keys, indexes.
describe_table
Get detailed information about a specific table.
Input:
{ "table_name": "users" }Output: Column details, constraints, sample values, indexes.
sample_data
Get sample rows from a table.
Input:
{ "table_name": "orders", "limit": 5 }Output: Formatted markdown table with sample rows.
table_stats
Get statistics for tables.
Input:
{ "table_name": "users" }Output: Row count, table size, column count, index count.
Docker Setup
# Start server with PostgreSQL and seed data
cd docker
docker-compose up -d
# The PostgreSQL database will be seeded with an ecommerce sample:
# - users (20 rows)
# - products (22 rows)
# - orders (20 rows)
# - order_items (30 rows)Testing
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Test with MCP Inspector
npx @modelcontextprotocol/inspector node build/index.jsDevelopment
# Run in dev mode (auto-recompile)
npm run dev
# Build
npm run build
# Lint
npm run lintProject Structure
src/
├── index.ts # Entry point — MCP server setup
├── config.ts # Environment config with zod validation
├── databases/
│ ├── base.ts # DatabaseAdapter interface
│ ├── index.ts # Adapter factory
│ ├── postgresql.ts # PostgreSQL adapter
│ ├── mysql.ts # MySQL adapter
│ └── mongodb.ts # MongoDB adapter
├── tools/
│ ├── query.ts # execute_query tool
│ ├── schema.ts # get_schema tool
│ ├── describe.ts # describe_table tool
│ ├── sample.ts # sample_data tool
│ └── stats.ts # table_stats tool
├── resources/
│ └── schema-resource.ts # database://schema resource
├── prompts/
│ └── query-helper.ts # query_helper prompt
├── safety/
│ ├── query-validator.ts # SQL safety validation
│ └── sanitizer.ts # Input/output sanitization
└── utils/
├── formatter.ts # Result formatting (table/json/csv)
└── logger.ts # Structured stderr loggingRoadmap
SSE transport support (HTTP mode)
SQLite adapter
Redis adapter
Connection pooling metrics tool
Rate limiting per client
Contributing
Fork the repository
Create a feature branch:
git checkout -b feature/my-featureMake your changes and add tests
Ensure all tests pass:
npm testEnsure the build succeeds:
npm run buildSubmit a pull request
License
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
This server cannot be deployed
Maintenance
Related MCP Connectors
Safe, read-only Postgres and MySQL access for AI agents. Audit log + column-level controls.
PostgreSQL, MySQL, and SQL Server in one session. 26 read-only MCP tools for AI agents.
Query, explore, and manage your databases through natural conversation.
Query Postgres, MySQL, SQL Server, Oracle, BigQuery, ClickHouse and Redshift from your AI client.
Related MCP Servers
- AlicenseAqualityDmaintenanceEnables AI assistants to connect to and interact with PostgreSQL, MySQL, SQLite, and MongoDB databases through natural language, supporting schema exploration, query execution, data export, and more.13MIT
- FlicenseAqualityCmaintenanceEnables AI agents to interact with PostgreSQL or MySQL databases using natural language. Supports SQL queries, schema discovery, and pre-built aggregations without writing SQL.3-
- AlicenseNot gradedqualityCmaintenanceEnables AI agents to safely explore and query databases across SQLite, PostgreSQL, MySQL, and MSSQL, with schema introspection, natural language queries, and data profiling.MIT
- AlicenseNot gradedqualityCmaintenanceEnables AI agents to safely explore and query SQLite or PostgreSQL databases through read-only, guarded SELECT queries with column masking, row limits, timeouts, and audit logging.MIT