MongoDB Intelligence MCP Server

MongoDB Intelligence MCP Server

An autonomous MCP server that enables LLMs to intelligently query and analyze MongoDB databases by reverse-engineering schemas, proving relationships, and enforcing security safeguards like PII masking and query limits.

Category
访问服务器

README

<div align="center"> <h1>🧠 MongoDB Intelligence Server (v2.0 Enterprise)</h1> <p><b>An Autonomous, Self-Healing Model Context Protocol (MCP) Server for LLMs</b></p>

Python Version Framework Database Security </div>


📖 The "Blind AI" Problem

Large Language Models (like Claude, GPT-4, and Cursor) are incredibly smart at writing SQL because SQL has a strict INFORMATION_SCHEMA. AI can instantly read the table structures.

MongoDB is Schemaless. If you point an AI at a raw MongoDB database using the official MCP, the AI is essentially "blind." It has to guess field names, it doesn't know what collections link to each other (no Foreign Keys), and it will constantly hallucinate broken pymongo queries that crash your app.

💡 The Solution

This MCP injects a Dynamic Intelligence Layer between your database and the AI. Before the AI even asks a question, this server:

  1. Reverse-Engineers the Schema: Scans all collections to infer exact data types, nullability, and Enums.
  2. Proves Foreign Keys: Uses mathematical heuristics and $in queries to physically prove relationships between collections.
  3. Anonymizes Data: Detects and masks PII (Personally Identifiable Information) so your sensitive data never leaves your local machine.

🏗️ Architectural Overview

The server is decoupled into three pluggable layers:

graph TD
    A[AI Agent / LLM] <-->|MCP Protocol via stdio| B[server.py - 14 Exposed Tools]
    
    subgraph "MongoDB Intelligence Server"
    B <--> C[Intelligence Layer]
    C <--> D[Data Access Layer]
    C <--> E[Presentation Layer]
    end
    
    D <-->|Safe, Paginated Queries| F[(Raw MongoDB)]
    
    C --> G[Discovery Engine]
    C --> H[Knowledge Graph]
    C --> I[Security Analyzers]

🛡️ Enterprise Security & Hardening

Standard AI connections are dangerous. We engineered 4 critical safeguards:

  1. Memory Protection (OOM Safety Ceiling): If an AI hallucinates a {"$limit": 5000000} query, it will crash your RAM. The MCP dynamically parses all incoming MQL pipelines. If a limit exceeds 1,000, it hard-caps it and returns a Limit enforced: custom (max 1000) flag to the LLM.
  2. PII Anonymization: The engine actively scans field names. If it detects password, ssn, email, or phone, it replaces the string payloads with ***MASKED*** before the data hits the AI's context window.
  3. Strict Mutation Gating: The server includes Write tools (insert, update, delete, create_index), but they are completely disabled by default via the .env READ_ONLY=true flag.
  4. Threaded Concurrency: Reverse-engineering a 5,000-collection ERP database sequentially takes minutes. We use ThreadPoolExecutor (10 workers) to map massive architectures in under ~1.5 seconds.

🛠️ The 14-Tool Arsenal

The MCP exposes exactly 14 functions to the LLM. The AI is strictly forbidden from bypassing these tools.

🔍 Core Data Operations

Tool Name Description
execute_aggregation_pipeline The flagship query engine. Executes complex MQL natively. Automatically capped at 1000 rows.
execute_find Dedicated high-speed query tool supporting projection, sort, and skip.
execute_create_index Allows the AI to act as a DBA and create compound indexes to fix slow queries natively.
execute_insert, update, delete Document mutation tools. (Blocked unless READ_ONLY=false).
execute_drop_collection, database High-risk structural deletion tools. (Blocked unless READ_ONLY=false).

🧠 Autonomous Intelligence

Tool Name Description
chat_with_database The RAG engine. The AI asks a natural language question, and the MCP answers it using its cached Knowledge Graph.
explain_collection Generates a deep-dive dossier on a single collection's lifecycle, dependencies, and Enums.
suggest_test_cases Generates strict QA scenarios based on discovered schema anomalies.
full_intelligence_pipeline The ultimate 360-audit. Runs all discovery and analysis algorithms simultaneously.

📊 Multi-Modal Output Generators

Tool Name Description
generate_dashboard Bypasses plain text and generates a premium Vanilla CSS/HTML interactive dashboard.
generate_executive_report Compiles database findings into a formatted PDF via ReportLab.
export_demo_package Generates a full markdown documentation bundle (architecture, onboarding, DBA recommendations).

🚀 Installation & Setup

1. Prerequisites

  • Python 3.11+
  • A running MongoDB instance (Local or Atlas)

2. Clone & Install

git clone https://github.com/YOUR_USERNAME/mongodb-intelligence-mcp.git
cd mongodb-intelligence-mcp
pip install -r requirements.txt

3. Configuration

Copy the .env.example file to a new .env file:

cp .env.example .env

Open .env and set your variables:

MONGODB_URI="mongodb://localhost:27017"
# Set to false ONLY if you want the AI to mutate data
READ_ONLY=true 
# Use glob patterns to restrict analysis to specific databases (e.g., tenant_*)
DATABASE_FILTER=

4. Run the Server

python server.py

Note: The server runs on stdio by default, awaiting an MCP Client connection.


🤖 The .cursorrules (Critical for LLM Usage)

If you are using Cursor, Claude Desktop, or Antigravity, you must use the provided .cursorrules file.

This file acts as a strict "Constitution" for the AI. It physically forbids the AI from writing slow, dangerous PyMongo scripts, and forces it to use the MCP tools.

The Auto-Dashboard Rule: The rules file contains an AUTOMATIC REPORT GENERATION RULE. If you ask the AI:

"Find the top 5 highest-paid employees."

The AI will not just print the JSON. Because of the rules, it will autonomously query the MCP, write a background Python script, and generate a beautiful HTML dashboard without you ever explicitly asking for one.


🏗️ Project Structure

d:\MCP\
├── server.py                 # FastMCP entry point (Registers all 14 tools)
├── .cursorrules              # The AI "Constitution"
├── src/
│   ├── data_access/          # Pluggable Layer (PyMongo adapters)
│   ├── intelligence/         # The Brain (Discovery, Graph Theory, Caching, PII)
│   │   └── analyzers/        # Pluggable Security & Compliance scanners
│   └── presentation/         # HTML/PDF/Markdown generators
└── outputs/                  # Timestamped artifacts (gitignored)

🤝 Extensibility

Want to add a new security compliance check (e.g., HIPAA scanning)? You don't need to rewrite the server. Simply drop a new Python script into src/intelligence/analyzers/ by subclassing BaseAnalyzer. The MCP will automatically pick it up, run it during discovery, and inject the findings into the LLM context.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选