mongodb-mcp-project

amrita120724/mongodb-mcp-project

3.2

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.

Tools
6
Resources
0
Prompts
0

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)
  1. Go to MongoDB Atlas
  2. Create a free account and cluster
  3. Get your connection string
Option B: Local Installation
  1. Download MongoDB from mongodb.com
  2. Install and start MongoDB service
  3. 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:

  1. find_documents - Find documents in a collection
  2. insert_document - Insert a new document
  3. update_documents - Update existing documents
  4. delete_documents - Delete documents
  5. list_collections - List all collections
  6. 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

  1. MongoDB Connection Failed

    • Check if MongoDB is running
    • Verify connection string in .env
    • Ensure network connectivity
  2. Port Already in Use

    • Change PORT in .env file
    • Kill existing process using the port
  3. Module Not Found

    • Run npm install to install dependencies
    • Check Node.js version compatibility

Debug Mode

Set NODE_ENV=development in your .env file for detailed error messages.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. 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