Skip to main content
Glama
mcp-database-server

mcp-database-server

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.

TypeScript Node.js License: MIT Docker MCP

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.js

Installation

npm install
npm run build

Configuration

All configuration is via environment variables:

Variable

Default

Description

DB_TYPE

postgresql

Database type: postgresql, mysql, mongodb

DB_HOST

localhost

Database host

DB_PORT

5432

Database port

DB_NAME

mydb

Database name

DB_USER

postgres

Database user

DB_PASSWORD

(empty)

Database password

DB_SSL

false

Enable SSL connection

ALLOW_WRITE

false

Allow INSERT/UPDATE/DELETE queries

MAX_ROWS

1000

Maximum rows per query result

QUERY_TIMEOUT

30000

Query timeout in milliseconds

LOG_LEVEL

info

Log level: debug, info, warn, error

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.js

Tools

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.js

Development

# Run in dev mode (auto-recompile)
npm run dev

# Build
npm run build

# Lint
npm run lint

Project 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 logging

Roadmap

  • SSE transport support (HTTP mode)

  • SQLite adapter

  • Redis adapter

  • Connection pooling metrics tool

  • Rate limiting per client

Contributing

  1. Fork the repository

  2. Create a feature branch: git checkout -b feature/my-feature

  3. Make your changes and add tests

  4. Ensure all tests pass: npm test

  5. Ensure the build succeeds: npm run build

  6. Submit a pull request

License

MIT

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      A
      quality
      D
      maintenance
      Enables 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.
      13
      MIT
    • F
      license
      A
      quality
      C
      maintenance
      Enables 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
      -
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables AI agents to safely explore and query databases across SQLite, PostgreSQL, MySQL, and MSSQL, with schema introspection, natural language queries, and data profiling.
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables 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