Skip to content

cisco-open/mongodb-nl-query-ai-agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

πŸš€ MongoDB Natural Language Query AI Agent

Version: 1.0.0
License: MIT
Package: mongodb-nl-query-ai-agent
GitHub: https://github.com/cisco-open/mongodb-nl-query-ai-agent

A powerful AI-powered agent for querying MongoDB databases using natural language. Built with LangGraph and supporting multiple LLM providers.


πŸ“¦ What's Inside?

mongodb-nl-query-ai-agent/
β”œβ”€β”€ README.md                          # This file
β”œβ”€β”€ LICENSE                            # MIT License
β”œβ”€β”€ QUICK_START.md                     # 5-minute setup guide
β”œβ”€β”€ pyproject.toml                     # Project configuration
β”œβ”€β”€ setup.py                           # Setup configuration
β”œβ”€β”€ .env.template                      # Configuration template
β”œβ”€β”€ src/                               # Source code (src layout)
β”‚   └── mongodb_agent/                 # Main package
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ agent.py                   # Core agent
β”‚       β”œβ”€β”€ api.py                     # REST API
β”‚       β”œβ”€β”€ cli.py                     # CLI interface
β”‚       β”œβ”€β”€ config.py                  # Configuration
β”‚       β”œβ”€β”€ graph.py                   # LangGraph workflow
β”‚       β”œβ”€β”€ state.py                   # State management
β”‚       β”œβ”€β”€ nodes/                     # Graph nodes
β”‚       β”œβ”€β”€ prompts/                   # LLM prompts
β”‚       β”œβ”€β”€ semantic_models/           # Model loaders
β”‚       β”œβ”€β”€ services/                  # External services
β”‚       └── utils/                     # Utilities
β”œβ”€β”€ tests/                             # Test suite
β”‚   β”œβ”€β”€ conftest.py                    # Test fixtures
β”‚   β”œβ”€β”€ test_agent.py                  # Agent tests
β”‚   β”œβ”€β”€ test_query_executor.py         # Query tests
β”‚   β”œβ”€β”€ test_router.py                 # Router tests
β”‚   └── ...                            # More tests
β”œβ”€β”€ docs/                              # Documentation (HTML)
β”‚   β”œβ”€β”€ index.html                     # Main docs
β”‚   β”œβ”€β”€ api-reference.html             # API reference
β”‚   └── images/                        # Documentation images
β”œβ”€β”€ scripts/                           # Utility scripts
β”‚   β”œβ”€β”€ setup.sh                       # Automated setup
β”‚   └── start_server.sh                # Unified server launcher
β”œβ”€β”€ examples/                          # Example scripts
β”‚   β”œβ”€β”€ basic_query.py                 # Simple query
β”‚   └── test_connection.py             # Connection test
β”œβ”€β”€ semantic_models/                   # YAML semantic models
β”‚   └── example_collection.yaml        # Example model
β”œβ”€β”€ server.py                          # Main server (API + Docs)
└── start_server.sh                    # Quick server launcher

⚑ Quick Start (3 Steps)

Step 1: Install

# Install from source
pip install -e .

# Or build and install
python -m build
pip install dist/mongodb_nl_query_ai_agent-1.0.0-py3-none-any.whl

Step 2: Configure

# Copy template and add your credentials
cp .env.template .env
nano .env  # Edit with your LLM and MongoDB credentials

Step 3: Run

# Start MCP server (for Claude Desktop integration)
python3 -m mongodb_agent.cli server --port 8000

# OR start REST API server (for HTTP/API access)
python3 -m mongodb_agent.cli server --port 8000 --mode rest

πŸ“š Documentation

View HTML Documentation

For the complete interactive documentation with navigation:

# Option 1: Quick start (from project root)
./start_server.sh

# Option 2: Direct Python
python3 server.py

# Option 3: Using scripts folder
./scripts/start_server.sh

Then open in your browser:

Quick Documentation Links


🎯 Features

  • πŸ€– Natural Language Queries - Ask questions in plain English
  • πŸ”Œ Multiple LLM Providers - OpenAI, Azure OpenAI, Anthropic Claude
  • πŸ“Š Semantic Models - Define your MongoDB schema in YAML (local files or Weaviate)
  • πŸš€ Model Context Protocol (MCP) - Integrate with Claude Desktop
  • 🌐 REST API - HTTP endpoints for easy integration
  • πŸ’Ύ Token Caching - Efficient OAuth token management
  • 🎨 Flexible Configuration - Environment variables or config files
  • πŸ”€ Dual MongoDB Modes - Direct PyMongo or MCP Protocol

πŸ“‹ Requirements

  • Python: 3.9 or higher
  • MongoDB: MongoDB instance (local or remote)
  • LLM API: OpenAI, Azure OpenAI, or Anthropic API key
  • Optional: MCP-compatible client (e.g., Claude Desktop)

πŸ”§ Configuration

LLM Providers

The agent supports multiple LLM providers:

OpenAI (Easiest)

LLM_PROVIDER=openai
OPENAI_API_KEY=sk-your-key-here
OPENAI_MODEL=gpt-4o-mini

Azure OpenAI

LLM_PROVIDER=azure
AZURE_OPENAI_ENDPOINT=https://your-endpoint.openai.azure.com
AZURE_OPENAI_API_KEY=your-key
AZURE_OPENAI_DEPLOYMENT_NAME=gpt-4o-mini

Anthropic Claude

LLM_PROVIDER=anthropic
ANTHROPIC_API_KEY=your-key

MongoDB Connection

Via MCP (Recommended)

MONGODB_MCP_ENDPOINT=http://localhost:3000/mongodb/query
MONGODB_CLIENT_ID=your-client-id
MONGODB_CLIENT_SECRET=your-client-secret

Direct Connection

MONGODB_URI=mongodb://localhost:27017
MONGODB_DATABASE=your_database

πŸ“š Documentation


πŸ’‘ Example Usage

Python API

from mongodb_agent import MongoDBAgent, Config

# Configure
config = Config(
    llm_provider="openai",
    openai_api_key="your-key",
    mongodb_mcp_endpoint="http://localhost:3000/mongodb/query"
)

# Create agent
agent = MongoDBAgent(config)

# Query
result = agent.query(
    question="Show me all orders from last month",
    yaml_file_name="orders_semantic_model.yaml"
)

print(result["query_result"])

REST API

curl -X POST http://localhost:8000/api/mongodb \\
  -H "Content-Type: application/json" \\
  -d '{
    "question": "Show me all orders from last month",
    "yaml_file_name": "orders_semantic_model.yaml"
  }'

Claude Desktop Integration

{
  "mcpServers": {
    "mongodb-agent": {
      "command": "python3",
      "args": ["-m", "mongodb_agent.cli", "server", "--port", "8000"]
    }
  }
}

πŸ› οΈ Semantic Models

Define your MongoDB schema in YAML format:

collection_info:
  database: "myapp"
  schema_name: "orders"
  
collections:
  orders:
    name: orders
    description: "Customer orders"
    fields:
      _id:
        data_type: "ObjectId"
        description: "Order ID"
      orderDate:
        data_type: "date"
        description: "Order date"
      totalAmount:
        data_type: "number"
        description: "Total order amount"

See semantic_models/example_collection.yaml for a complete example.


🀝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Submit a pull request

πŸ“„ License

MIT License - see LICENSE file for details


πŸ†˜ Support

  • Issues: GitHub Issues
  • Documentation: See docs/ folder
  • Examples: See examples/ folder

πŸŽ‰ Acknowledgments

Built with:


πŸ“Š Architecture

flowchart TB
    User["πŸ‘€ User Query<br/>(Natural Language)"]
    
    subgraph Agent["πŸ€– MongoDB Agent (LangGraph State Machine)"]
        Ingress["πŸšͺ Ingress<br/>(Initialize State)"]
        Selector["πŸ“‹ Selector<br/>(Load Schema + Generate Query)"]
        Executor["▢️ Query Executor<br/>(Execute MongoDB Query)"]
        Router{"πŸ”€ Router<br/>(Check Result)"}
        Refiner["πŸ”§ Query Refiner<br/>(Fix Query via LLM)"]
        Parser["πŸ“ Output Parser<br/>(Format to Natural Language)"]
    end
    
    subgraph SemanticSource["πŸ“š Semantic Model Source"]
        LocalYAML["πŸ“„ Local YAML Files<br/>(Default)"]
        Weaviate["πŸ” Weaviate Vector DB<br/>(Optional)"]
    end
    
    subgraph LLM["🧠 LLM Provider"]
        OpenAI["OpenAI"]
        AzureOpenAI["Azure OpenAI"]
        Claude["Anthropic Claude"]
    end
    
    subgraph MongoRouter["πŸ”„ MongoDB Connection"]
        MCPClient["πŸ” MCP Client<br/>(OAuth2)"]
        DirectClient["πŸ”— Direct Client<br/>(PyMongo)"]
    end
    
    MongoDB[("πŸƒ MongoDB<br/>Database")]
    Result["βœ… Final Result<br/>(Natural Language)"]
    
    User --> Ingress
    Ingress --> Selector
    Selector --> Executor
    Executor --> Router
    Router -->|"βœ… Success"| Parser
    Router -->|"❌ Error"| Refiner
    Router -->|"πŸ’€ Fatal Error"| Result
    Refiner --> Executor
    Parser --> Result
    
    Selector -.->|"1. Load Schema<br/>2. Generate Query"| SemanticSource
    Selector -.->|"Generate Pipeline"| LLM
    Refiner -.->|"Fix Query"| LLM
    Parser -.->|"Format Output"| LLM
    
    Executor --> MongoRouter
    MCPClient --> MongoDB
    DirectClient --> MongoDB
    
    style User fill:#e1f5ff
    style Agent fill:#fff4e1
    style SemanticSource fill:#f0f0f0
    style LLM fill:#e8f5e9
    style MongoRouter fill:#fce4ec
    style MongoDB fill:#c8e6c9
    style Result fill:#e1f5ff
Loading

Architecture Flow

  1. User Query β†’ Ingress - Initialize agent state with user question
  2. Ingress β†’ Selector - Loads semantic model (schema) from Local YAML or Weaviate, then uses LLM to generate MongoDB aggregation pipeline
  3. Selector β†’ Query Executor - Executes the generated MongoDB query via:
    • MCP Client (OAuth2) - For Model Context Protocol integration
    • Direct Client (PyMongo) - For direct MongoDB connection
  4. Query Executor β†’ Router - Checks execution result:
    • βœ… Success β†’ Go to Output Parser
    • ❌ Error (recoverable) β†’ Go to Query Refiner
    • πŸ’€ Fatal Error β†’ Return error to user
  5. Query Refiner β†’ Query Executor - LLM fixes the query and retries (max retry limit: 1)
  6. Output Parser β†’ Result - LLM converts raw MongoDB results to natural language
  7. Return - Final answer delivered to user

Made with ❀️ by the MongoDB Agent Team

About

AI-powered agent for querying MongoDB databases using natural language

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors