amrita120724/mongodb-mcp-project
If you are the rightful owner of mongodb-mcp-project and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
The MongoDB MCP Server is a Node.js project that integrates MongoDB with the Model Context Protocol (MCP) to enable AI-powered database operations.
MongoDB MCP Server
A Node.js project that integrates MongoDB with Model Context Protocol (MCP) for AI-powered database operations.
Features
- MongoDB Integration: Full CRUD operations with MongoDB
- GitHub Integration: GitHub MCP server for repository management
- MCP Servers: Multiple Model Context Protocol servers for AI interactions
- REST API: Express.js API for database operations
- Error Handling: Comprehensive error handling and validation
- Security: Helmet.js for security headers
- Logging: Morgan for HTTP request logging
Prerequisites
- Node.js (v14 or higher)
- MongoDB (v4.4 or higher)
- npm or yarn
Installation
1. Install MongoDB
Option A: Using MongoDB Atlas (Cloud - Recommended)
- Go to MongoDB Atlas
- Create a free account and cluster
- Get your connection string
Option B: Local Installation
- Download MongoDB from mongodb.com
- Install and start MongoDB service
- Default connection:
mongodb://localhost:27017
2. Clone and Setup Project
# Clone the repository
git clone <your-repo-url>
cd mongodb-mcp-project
# Install dependencies
npm install
# Copy environment file
cp env.example .env
# Edit .env file with your MongoDB connection details
3. Configure Environment Variables
Edit the .env file:
# MongoDB Configuration
MONGODB_URI=mongodb://localhost:27017
# OR for MongoDB Atlas:
# MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/
DB_NAME=mongodb_mcp_db
# Server Configuration
PORT=3000
NODE_ENV=development
# MCP Configuration
MCP_SERVER_NAME=mongodb-mcp-server
MCP_SERVER_VERSION=1.0.0
Usage
Start the Express API Server
# Development mode (with auto-restart)
npm run dev
# Production mode
npm start
The server will start on http://localhost:3000
Start the MCP Server
npm run mcp
API Endpoints
Health Check
GET /health- Server health status
Database Operations
Find Documents
POST /api/database/find
Content-Type: application/json
{
"collection": "users",
"filter": { "age": { "$gte": 18 } },
"options": { "limit": 10 }
}
Insert Document
POST /api/database/insert
Content-Type: application/json
{
"collection": "users",
"document": {
"name": "John Doe",
"email": "john@example.com",
"age": 25
}
}
Update Documents
PUT /api/database/update
Content-Type: application/json
{
"collection": "users",
"filter": { "email": "john@example.com" },
"update": { "$set": { "age": 26 } }
}
Delete Documents
DELETE /api/database/delete
Content-Type: application/json
{
"collection": "users",
"filter": { "email": "john@example.com" }
}
List Collections
GET /api/database/collections
Get Collection Statistics
GET /api/database/stats/users
Get Database Info
GET /api/database/info
MCP Server Tools
The MCP server provides the following tools for AI interactions:
- find_documents - Find documents in a collection
- insert_document - Insert a new document
- update_documents - Update existing documents
- delete_documents - Delete documents
- list_collections - List all collections
- get_collection_stats - Get collection statistics
Project Structure
mongodb-mcp-project/
├── config/
│ └── database.js # MongoDB connection configuration
├── src/
│ ├── mcp/
│ │ └── server.js # MCP server implementation
│ └── routes/
│ └── database.js # API routes for database operations
├── app.js # Main Express server
├── mcp-server.js # MCP server entry point
├── package.json
├── env.example # Environment variables template
└── README.md
Development
Running in Development Mode
# Start with auto-restart
npm run dev
Testing the API
You can test the API using curl, Postman, or any HTTP client:
# Health check
curl http://localhost:3000/health
# List collections
curl http://localhost:3000/api/database/collections
# Insert a document
curl -X POST http://localhost:3000/api/database/insert \
-H "Content-Type: application/json" \
-d '{"collection": "test", "document": {"name": "Test", "value": 123}}'
Error Handling
The server includes comprehensive error handling:
- Database connection errors
- Invalid request parameters
- MongoDB operation errors
- Server errors with appropriate HTTP status codes
Security
- Helmet.js for security headers
- CORS enabled for cross-origin requests
- Input validation for all endpoints
- Environment variable protection
Logging
- Morgan for HTTP request logging
- Console logging for errors and important events
- Structured logging format
Troubleshooting
Common Issues
-
MongoDB Connection Failed
- Check if MongoDB is running
- Verify connection string in
.env - Ensure network connectivity
-
Port Already in Use
- Change PORT in
.envfile - Kill existing process using the port
- Change PORT in
-
Module Not Found
- Run
npm installto install dependencies - Check Node.js version compatibility
- Run
Debug Mode
Set NODE_ENV=development in your .env file for detailed error messages.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
License
This project is licensed under the ISC License.
Support
For issues and questions:
- Check the troubleshooting section
- Review MongoDB documentation
- Check MCP SDK documentation