MongoDB MCP Server

1.9.0 · active · verified Sun Apr 19

The MongoDB MCP Server is the official implementation of the Model Context Protocol (MCP) designed to enable AI agents and large language models (LLMs) to interact with MongoDB databases using natural language. It connects various MongoDB deployments (Atlas, Community Edition, Enterprise Advanced) to MCP-supported AI clients such as Cursor, GitHub Copilot, and Claude Desktop. The server provides a rich set of tools for data exploration, database operations (including querying, indexing, and CRUD operations), Atlas management tasks, and performance optimization via integrated Performance Advisor tools. The current stable version is 1.9.0, with a cadence of frequent pre-releases and regular minor version updates. Key differentiators include its seamless integration with MongoDB Atlas, support for local Atlas deployments (requiring Docker), a simplified setup utility (`npx mongodb-mcp-server setup`), and advanced features like automatic vector embedding generation when configured with a Voyage AI API key.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically start the MongoDB MCP Server, configure it with a connection string, and handle graceful shutdowns. It also mentions the easier CLI-based setup.

import { startServer } from 'mongodb-mcp-server';

// To run the server programmatically, you must provide a MongoDB connection string.
// This can be via environment variables (MONGODB_URI, ATLAS_CLIENT_ID, ATLAS_PRIVATE_KEY)
// or passed directly in the configuration object.

// Ensure you have a MongoDB connection string set as an environment variable
// or replace process.env.MONGODB_URI with your actual connection string.
// For Atlas API credentials, set ATLAS_CLIENT_ID and ATLAS_PRIVATE_KEY.

const connectionString = process.env.MONGODB_URI ?? 'mongodb://localhost:27017/mcp-db';

const serverConfig = {
  connectionString: connectionString,
  port: 3000,
  readOnly: false, // Set to true to disable write operations
  logLevel: 'info' // 'debug', 'info', 'warn', 'error'
};

async function main() {
  try {
    console.log('Starting MongoDB MCP Server...');
    const server = await startServer(serverConfig);
    console.log(`MongoDB MCP Server listening on port ${serverConfig.port}`);

    // Example: Graceful shutdown
    process.on('SIGINT', async () => {
      console.log('SIGINT received, shutting down server...');
      await server.stop();
      process.exit(0);
    });
    process.on('SIGTERM', async () => {
      console.log('SIGTERM received, shutting down server...');
      await server.stop();
      process.exit(0);
    });
  } catch (error) {
    console.error('Failed to start MongoDB MCP Server:', error);
    process.exit(1);
  }
}

main();

// Alternative quick setup via CLI (recommended for initial setup):
// npx mongodb-mcp-server setup
// This command guides you through configuration and starts the server.

view raw JSON →