MCP HubMCP Hub
mongodb-developer

mongodb-mcp-server

by: mongodb-developer

mongodb mcp server

20created 13/12/2024
Visit
mongodb

📌Overview

Purpose: To provide a Model Context Protocol server that facilitates read-only access to MongoDB databases for inspecting collection schemas and executing aggregation pipelines.

Overview: The MongoDB MCP Server enables seamless integration with MongoDB, allowing large language models (LLMs) to perform queries and retrieve schema information effectively. This read-only server is designed for executing aggregation pipelines and obtaining execution plans, ensuring a secure and efficient querying experience.

Key Features:

  • Aggregation Execution: Execute MongoDB aggregation pipelines with various customization options such as disk usage allowance and execution time limits.

  • Schema Information Access: Provides inferred JSON schema information for each collection, including field names and data types, derived from sampling collection documents.


MongoDB MCP Server

A Model Context Protocol server providing read-only access to MongoDB databases. It enables LLMs to inspect collection schemas and execute aggregation pipelines.

Components

Tools

  • aggregate

    • Execute MongoDB aggregation pipelines on the connected database.
    • Input parameters:
      • collection (string): The collection to query.
      • pipeline (array): MongoDB aggregation pipeline stages.
      • options (object): Optional aggregation settings.
        • allowDiskUse (boolean): Allow operations requiring disk usage.
        • maxTimeMS (number): Maximum execution time in milliseconds.
        • comment (string): Comment to identify the operation.
    • Features:
      • Default limit of 1000 documents if no limit stage is specified.
      • Default timeout of 30 seconds.
  • explain

    • Retrieve execution plans for aggregation pipelines.
    • Input parameters:
      • collection (string): The collection to analyze.
      • pipeline (array): MongoDB aggregation pipeline stages.
      • verbosity (string): Detail level of the explanation.
        • Options: "queryPlanner", "executionStats", "allPlansExecution".
        • Default: "queryPlanner".

Resources

  • Collection Schemas
    • Provides inferred JSON schema information for each collection.
    • Includes field names and data types.
    • Schema is derived from sampling collection documents.

Usage with Claude Desktop

Add the following configuration to the mcpServers section of your claude_desktop_config.json:

"mongodb": {
  "command": "npx",
  "args": [
    "-y",
    "@pash1986/mcp-server-mongodb"
  ],
  "env": {
    "MONGODB_URI": "mongodb+srv://<yourcluster>" // or 'mongodb://localhost:27017'
  }
}

Replace <yourcluster> with your connection string and adjust the database name as needed.

Example Usage

Basic Aggregation

{
  "collection": "users",
  "pipeline": [
    { "$match": { "age": { "$gt": 21 } } },
    { "$group": {
      "_id": "$city",
      "avgAge": { "$avg": "$age" },
      "count": { "$sum": 1 }
    }},
    { "$sort": { "count": -1 } },
    { "$limit": 10 }
  ],
  "options": {
    "allowDiskUse": true,
    "maxTimeMS": 60000,
    "comment": "City-wise user statistics"
  }
}

Query Explanation

{
  "collection": "users",
  "pipeline": [
    { "$match": { "age": { "$gt": 21 } } },
    { "$sort": { "age": 1 } }
  ],
  "verbosity": "executionStats"
}

Safety Features

  • Automatic limit of 1000 documents if no limit is specified.
  • Default timeout of 30 seconds for operations.
  • Read-only operations only.
  • Safe schema inference from collection samples.

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. See the LICENSE file in the project repository for details.