Provides MongoDB document operations including finding documents with filters, inserting new documents, listing collections, and handling ObjectId serialization for AI-powered database interactions.
Enables AI-accessible PostgreSQL database operations including querying data, listing tables and schemas, inserting records, and retrieving server information with built-in SQL validation and safety checks.
Database MCP Server
Query and manage databases through the Model Context Protocol.
Overview
The Database MCP Server provides AI-accessible database operations for PostgreSQL and MongoDB. It enables:
PostgreSQL queries and data management
MongoDB document operations
Automatic schema discovery
Network-wide database access through MCP Discovery Hub
Zero-configuration deployment with automatic broadcasting
Perfect for building AI applications that need to interact with databases safely and efficiently.
Features
PostgreSQL Support
Get server version and database info
List tables in any schema
Query data with configurable limits
Insert new records
SQL validation and safety checks
MongoDB Support
List collections
Find documents with filters
Insert documents
ObjectId handling and JSON serialization
Network Integration
Automatic multicast broadcasting for discovery
Multi-transport support (HTTP and streamable-http)
Compatible with MCP Discovery Hub
Zero-configuration networking
Installation
Prerequisites
Python 3.10+
PostgreSQL server (or MongoDB, or both)
uv
package manager (orpip
)
Setup
Configuration
Environment Variables
.env File
Create a .env
file in the project root:
Docker Example
Usage
Start in HTTP Mode (with broadcasting)
Start in Streamable-HTTP Mode
Start in Stdio Mode (for Claude)
Available Tools
PostgreSQL Tools
Get DB Version
Retrieve PostgreSQL server version information
List Tables
List all tables in a schema
Example:
List Rows
Query data from a table with limit
Example:
Insert Row
Insert a new record and return the inserted ID
Example:
MongoDB Tools
List Collections
Get all collection names in the database
Find Documents
Query documents from a collection
Example:
Insert Document
Insert a document into a collection
Example:
Integration with MCP Discovery Hub
Automatic Discovery
When broadcasting is enabled, the database server automatically registers:
Server broadcasts: Every 30 seconds on
239.255.255.250:5353
Hub discovers: Discovery hub receives and probes the server
Tools registered: All 7 database tools become available network-wide
Multi-Server Setup
Deploy multiple database servers for different purposes:
All servers discovered and available to AI automatically.
API Endpoints (When in HTTP Mode)
GET /
Server information
POST /mcp
MCP protocol endpoint
All MCP communication (initialize, tools/list, tools/call)
Use Cases
1. Data Analysis
AI-powered analysis of your database:
2. Automated Reporting
Generate reports from database data:
3. Data Entry and Updates
AI-assisted data entry:
4. Document Search and Retrieval
MongoDB document management:
5. System Monitoring
Database health and activity monitoring:
Safety Features
Input Validation
Table and column names validated against regex
SQL injection prevention through parameterized queries
Data type validation for inserts
Error Handling
Database connection errors caught and reported
Timeout protection (30 seconds default)
Clear error messages for debugging
Best Practices
Read-only operations first: Start with queries before modifying data
Use limits: Always set reasonable limits on queries
Monitor logs: Check
database_mcp.log
for issuesBackup data: Ensure backups before AI access to production
Audit trail: Log all database modifications from MCP
Performance Considerations
Query performance: Depends on query complexity and data size
Connection pooling: PostgreSQL pool_size=5 for concurrency
Broadcasting overhead: Minimal (30-byte UDP packets)
Timeout protection: 30-second limit on operations
Optimization Tips
Use
limit
parameter to reduce data transferFilter documents with
query
parameter in MongoDBCreate appropriate database indexes for common queries
Use
schema
parameter to narrow PostgreSQL searches
Logs
Server logs are written to database_mcp.log
:
Troubleshooting
PostgreSQL Connection Error
MongoDB Connection Error
Broadcasting Not Working
Port Already in Use
Performance Metrics
Typical response times:
Simple SELECT: 10-50ms
Database info queries: 5-20ms
MongoDB find operations: 20-100ms
Insert operations: 30-200ms (depending on triggers)
Network overhead (with broadcasting):
Broadcasting: 0.01% overhead
Discovery: One-time cost per server
Requirements
Python 3.10+
FastAPI
SQLAlchemy
PyMongo
FastMCP
python-dotenv
Contributing
Improvements welcome! Potential enhancements:
Additional database support (MySQL, SQLite)
Stored procedure execution
Transaction support
Advanced query builder
Connection pooling configuration
Database replication support
License
MIT License - See LICENSE file for details
Support
Issues: Report on GitHub
Documentation: See MCP Discovery Hub wiki
Examples: Check examples/ directory
Database docs: PostgreSQL and MongoDB official documentation
This server cannot be installed
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
Enables AI to query and manage PostgreSQL and MongoDB databases through natural language. Supports automatic schema discovery, safe data operations, and network-wide database access with zero-configuration deployment.