database-mcp-server

database-mcp-server

A Model Context Protocol (MCP) server that provides database operations as tools for LLM-powered applications. Supports PostgreSQL, MySQL, and MongoDB databases with connection pooling and flexible result formatting.

Category
访问服务器

README

Database-MCP-Server

A Model Context Protocol (MCP) server that provides database operations as tools for LLM-powered applications. Supports PostgreSQL, MySQL, and MongoDB databases with connection pooling and flexible result formatting.

Features

  • Multi-database support: PostgreSQL, MySQL, and MongoDB
  • Connection pooling: Efficient database connection management
  • Multiple result formats: JSON and Markdown table output
  • Streamable HTTP: Modern MCP transport via HTTP
  • Async operations: Full async/await support for non-blocking database queries

Tools

Tool Description
execute_query Execute arbitrary SQL queries against the database
list_databases List all available databases
list_tables List all tables in a database (defaults to configured database)
describe_table Get schema information for a specific table

Requirements

  • Python 3.11+
  • PostgreSQL 12+, MySQL 8.0+, or MongoDB 4.0+

Installation

# Clone the repository
git clone <repository-url>
cd database-mcp-server

# Install dependencies using uv (recommended)
uv pip install -e .

# Or using pip
pip install -e .

Configuration

Copy .env.example to .env and configure the environment variables:

cp .env.example .env

Then edit .env with your database credentials and settings:

# Database Configuration
DATABASE_PROVIDER=postgres  # or mysql, mongodb
DATABASE_HOST=localhost
DATABASE_PORT=5432          # 3306 for MySQL, 27017 for MongoDB
DATABASE_USER=your_username
DATABASE_PASSWORD=your_password
DATABASE_NAME=your_database
DATABASE_MIN_POOL_SIZE=1
DATABASE_MAX_POOL_SIZE=10

# Server Configuration (optional)
HTTP_HOST=0.0.0.0          # Bind address (defaults to all interfaces)
HTTP_PORT=8080

Configuration Options

Variable Required Default Description
DATABASE_PROVIDER Yes - Database provider: postgres, mysql, or mongodb
DATABASE_HOST Yes - Database server hostname or IP
DATABASE_PORT Yes - Database server port (5432 for PostgreSQL, 3306 for MySQL, 27017 for MongoDB)
DATABASE_USER Yes - Database username
DATABASE_PASSWORD No - Database password
DATABASE_NAME Yes - Default database to connect to
DATABASE_MIN_POOL_SIZE Yes - Minimum pool size for connections
DATABASE_MAX_POOL_SIZE Yes - Maximum pool size for connections
HTTP_HOST No 0.0.0.0 HTTP server bind address
HTTP_PORT No 8080 HTTP server port for MCP
READ_ONLY_MODE No true Enable read-only mode (blocks write operations)

Read-Only Mode

By default, the server runs in read-only mode to prevent accidental data modifications. When enabled:

  • PostgreSQL/MySQL: Only SELECT, SHOW, and DESCRIBE queries are allowed
  • MongoDB: Only read operations (find, find_one, aggregate, count) are allowed

Write operations like INSERT, UPDATE, DELETE, DROP, CREATE, ALTER are blocked.

To disable read-only mode (allow all operations):

READ_ONLY_MODE=false

Usage

Running the Server

# Using uv (recommended)
uv run python main.py

# Using Python directly
python main.py

MCP Client Configuration

Add the server to your MCP client configuration:

Using uv (recommended for dependency isolation):

{
  "mcpServers": {
    "sql": {
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/database-mcp-server",
        "run",
        "python",
        "main.py"
      ]
    }
  }
}

Using Python directly:

{
  "mcpServers": {
    "sql": {
      "command": "python",
      "args": ["/path/to/database-mcp-server/main.py"]
    }
  }
}

Direct HTTP Connection:

The server exposes a Streamable HTTP endpoint. Configure your MCP client to connect directly:

{
  "mcpServers": {
    "sql": {
      "url": "http://localhost:8080/mcp"
    }
  }
}

Note: For direct HTTP connections, ensure the server is running and accessible at the specified URL.

Example Tool Calls

Common Examples (All Providers)

# List all databases
list_databases()

# List tables/collections in the configured database
list_tables()

# List tables/collections in a specific database
list_tables(database="another_db")

# Describe a table/collection schema
describe_table("users")

PostgreSQL / MySQL Examples (SQL)

For PostgreSQL and MySQL, use standard SQL syntax:

# Execute a SELECT query
execute_query("SELECT * FROM users WHERE active = true LIMIT 10")

# Execute with specific columns
execute_query("SELECT name, email FROM users WHERE age > 18")

# Get results in Markdown format
execute_query("SELECT name, email FROM users", format_type="markdown")

# INSERT, UPDATE, DELETE queries
execute_query("INSERT INTO users (name, email) VALUES ('John', 'john@example.com')")
execute_query("UPDATE users SET status = 'active' WHERE status = 'pending'")
execute_query("DELETE FROM users WHERE inactive = true")

# Join queries
execute_query("""
    SELECT u.name, o.order_id 
    FROM users u 
    JOIN orders o ON u.id = o.user_id 
    WHERE u.active = true
""")

MongoDB Examples (JSON)

For MongoDB, queries use JSON format with operation type:

# Find documents
execute_query('{"collection": "users", "operation": "find", "filter": {"active": true}, "limit": 10}')

# Find with projection (select specific fields)
execute_query('{"collection": "users", "operation": "find", "projection": {"name": 1, "email": 1}}')

# Find with sorting and pagination
execute_query('{"collection": "users", "operation": "find", "filter": {"active": true}, "sort": {"created_at": -1}, "skip": 0, "limit": 20}')

# Find one document
execute_query('{"collection": "users", "operation": "find_one", "filter": {"_id": "64f8a2b1c9d8e7f6a5b4c3d2"}}')

# Insert a document
execute_query('{"collection": "users", "operation": "insert_one", "document": {"name": "John", "email": "john@example.com", "created_at": "2024-01-15T10:30:00"}}')

# Insert multiple documents
execute_query('{"collection": "users", "operation": "insert_many", "documents": [{"name": "John"}, {"name": "Jane"}]}')

# Update documents
execute_query('{"collection": "users", "operation": "update_one", "filter": {"email": "john@example.com"}, "update": {"$set": {"status": "active"}}}')
execute_query('{"collection": "users", "operation": "update_many", "filter": {"status": "pending"}, "update": {"$set": {"status": "active"}}}')

# Delete documents
execute_query('{"collection": "users", "operation": "delete_one", "filter": {"email": "john@example.com"}}')
execute_query('{"collection": "users", "operation": "delete_many", "filter": {"inactive": true}}')

# Aggregation pipeline
execute_query('{"collection": "orders", "operation": "aggregate", "pipeline": [{"$group": {"_id": "$status", "count": {"$sum": 1}}}]}')

# Count documents
execute_query('{"collection": "users", "operation": "count", "filter": {"active": true}}')

Supported MongoDB Operations:

Operation Description
find Query documents with optional filter, projection, sort, skip, limit
find_one Query a single document by filter
insert_one Insert a single document
insert_many Insert multiple documents
update_one Update a single document matching filter
update_many Update multiple documents matching filter
delete_one Delete a single document matching filter
delete_many Delete multiple documents matching filter
aggregate Run aggregation pipeline
count Count documents matching filter

Query Format Reference:

{
  "collection": "collection_name",    // Required: Collection to query
  "operation": "find",                 // Required: Operation type (see above)
  "filter": {},                        // Optional: Query filter (MongoDB query syntax)
  "projection": {},                    // Optional: Fields to include/exclude
  "sort": {"field": 1},               // Optional: Sort order (1=asc, -1=desc)
  "skip": 0,                           // Optional: Number of documents to skip
  "limit": 10,                         // Optional: Maximum documents to return
  "document": {},                      // For insert_one: Document to insert
  "documents": [{}, {}],               // For insert_many: Array of documents
  "update": {"$set": {}},             // For update operations: Update operators
  "pipeline": [{}, {}]                 // For aggregate: Aggregation pipeline stages
}

Project Structure

database-mcp-server/
├── main.py                 # Entry point
├── pyproject.toml          # Project configuration
├── .env                    # Environment variables (create this)
└── src/
    ├── server.py           # MCP server and tool definitions
    ├── lib/
    │   ├── config.py       # Configuration management
    │   ├── utils.py        # Utility functions
    │   ├── middleware.py   # Middleware for MCP server
    │   └── mcp.py          # MCP-specific utilities
    └── database/
        ├── manager.py      # Database connection pool manager
        ├── formatter.py    # Result formatting (JSON/Markdown)
        ├── postgres_client.py  # PostgreSQL-specific operations
        ├── mysql_client.py     # MySQL-specific operations
        └── mongodb_client.py   # MongoDB-specific operations

Development

# Run in development mode with auto-reload (requires watching tool)
uv run watch -c "python main.py" .

Dependencies

Package Purpose
mcp>=1.26.0 Model Context Protocol framework
asyncpg PostgreSQL async driver
aiomysql MySQL async driver
pymongo>=4.9.0 MongoDB async driver
python-dotenv Environment variable management

Running Tests

Tests are not yet implemented.

License

MIT

Changelog

v1.2.0 (2026-02-19)

  • Added MCP middleware support (src/lib/middleware.py)
  • Added MCP utilities module (src/lib/mcp.py)
  • Added ruff code formatter and linter configuration

v1.1.0 (2026-02-14)

  • Added MongoDB support with PyMongo async driver
  • New mongodb_client.py with full CRUD operations (find, insert, update, delete, aggregate, count)
  • Schema inference for describe_table via document sampling
  • Connection pooling support for MongoDB
  • Updated documentation with provider-specific examples

v1.0.0 (2026-02-05)

  • Initial release
  • Added Streamable HTTP transport support for MCP
  • Separated HTTP host configuration into dedicated environment variable
  • Added comprehensive README documentation
  • Improved server module organization (moved server.py to src/server.py)
  • Added example .env configuration file

推荐服务器

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

官方
精选