MCP HubMCP Hub
kiliczsh

mcp-mongo-server

by: kiliczsh

A Model Context Protocol Server for MongoDB

143created 04/12/2024
Visit
MongoDB
Protocol

📌Overview

Purpose: The MCP MongoDB Server allows access to MongoDB databases, enabling large language models (LLMs) to inspect collection schemas and perform various MongoDB operations.

Overview: This server provides a structured interface for interacting with MongoDB, supporting both read-only and full access modes. It facilitates efficient MongoDB operations while ensuring safety and performance.

Key Features:

  • Read-Only Mode: Connect in read-only mode to prevent write operations, enhancing safety for production databases and utilizing MongoDB's secondary read preference for better read performance.

  • Comprehensive Toolset: Includes tools for executing MongoDB queries, aggregations, updates, inserts, and more, each with detailed input requirements and outputs, allowing users to manage data effectively.

  • Collection Resources: Automatically provides schema information for each collection, including field names and data types in a JSON format, assisting users in understanding data structures quickly.


MCP MongoDB Server

A Model Context Protocol server that provides access to MongoDB databases. This server enables LLMs to inspect collection schemas and execute MongoDB operations.

Features

Read-Only Mode

  • Connect to MongoDB in read-only mode using --read-only or -r flag
  • Prevents write operations (update, insert, createIndex)
  • Uses MongoDB's secondary read preference for optimal read performance

Resources

  • List and access collections via mongodb:// URIs

Tools

  • query: Execute MongoDB queries
  • aggregate: Execute MongoDB aggregation pipelines
  • update: Update documents in a collection
  • serverInfo: Get MongoDB server information
  • insert: Insert documents into a collection
  • createIndex: Create indexes on a collection
  • count: Count documents matching a query

Prompts

  • analyze_collection: Analyze collection structure and contents

Development

Install dependencies:

npm install

Build the server:

npm run build

For development with auto-rebuild:

npm run watch

Installation for Development

Using Claude Desktop

Add the server config to claude_desktop_config.json:

{
  "mcpServers": {
    "mongodb": {
      "command": "node",
      "args": [
        "~/mcp-mongo-server/build/index.js",
        "mongodb://user:password@mongodb.localhost/namespace"
      ]
    },
    "mongodb-readonly": {
      "command": "node",
      "args": [
        "~/mcp-mongo-server/build/index.js",
        "mongodb://user:password@mongodb.localhost/namespace",
        "--read-only"
      ]
    }
  }
}

Debugging

Use the MCP Inspector for debugging:

npm run inspector

Usage with Claude Desktop

Add the following configuration to claude_desktop_config.json:

{
  "mcpServers": {
    "mongodb": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-mongo-server",
        "mongodb://user:password@mongodb.localhost/sample_namespace"
      ]
    },
    "mongodb-readonly": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-mongo-server",
        "mongodb://user:password@mongodb.localhost/sample_namespace",
        "--read-only"
      ]
    }
  }
}

Installing via Smithery

npx -y @smithery/cli install mcp-mongo-server --client claude

Installing via mcp-get

npx @michaellatman/mcp-get@latest install mcp-mongo-server

Using Read-Only Mode

Connect to MongoDB in read-only mode:

npx mcp-mongo-server mongodb://user:password@mongodb.example.com/database --read-only

When in read-only mode:

  1. All write operations are blocked
  2. The server connects using MongoDB's secondary read preference

License

This MCP server is licensed under the MIT License. You are free to use, modify, and distribute the software, subject to the terms of the MIT License.