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.
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
# 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# Copy template and add your credentials
cp .env.template .env
nano .env # Edit with your LLM and MongoDB credentials# 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 restFor 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.shThen open in your browser:
- π HTML Docs: http://127.0.0.1:8001/
- π API Docs: http://127.0.0.1:8001/docs
- β€οΈ Health Check: http://127.0.0.1:8001/health
- Getting Started - First-time setup
- Installation Guide - Detailed installation
- Configuration - All configuration options
- Usage Examples - Real-world examples
- API Reference - Complete API documentation
- Architecture - System architecture
- Troubleshooting - Common issues and solutions
- π€ 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
- 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)
The agent supports multiple LLM providers:
OpenAI (Easiest)
LLM_PROVIDER=openai
OPENAI_API_KEY=sk-your-key-here
OPENAI_MODEL=gpt-4o-miniAzure 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-miniAnthropic Claude
LLM_PROVIDER=anthropic
ANTHROPIC_API_KEY=your-keyVia MCP (Recommended)
MONGODB_MCP_ENDPOINT=http://localhost:3000/mongodb/query
MONGODB_CLIENT_ID=your-client-id
MONGODB_CLIENT_SECRET=your-client-secretDirect Connection
MONGODB_URI=mongodb://localhost:27017
MONGODB_DATABASE=your_database- QUICK_START.md - Get started in 5 minutes
- docs/USER_GUIDE.md - Complete usage guide
- docs/API_REFERENCE.md - API endpoints
- docs/CONFIGURATION.md - Configuration options
- docs/TROUBLESHOOTING.md - Common issues
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"])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"
}'{
"mcpServers": {
"mongodb-agent": {
"command": "python3",
"args": ["-m", "mongodb_agent.cli", "server", "--port", "8000"]
}
}
}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.
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Submit a pull request
MIT License - see LICENSE file for details
- Issues: GitHub Issues
- Documentation: See
docs/folder - Examples: See
examples/folder
Built with:
- LangGraph - Agent orchestration
- LangChain - LLM framework
- Model Context Protocol - Claude integration
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
- User Query β Ingress - Initialize agent state with user question
- Ingress β Selector - Loads semantic model (schema) from Local YAML or Weaviate, then uses LLM to generate MongoDB aggregation pipeline
- Selector β Query Executor - Executes the generated MongoDB query via:
- MCP Client (OAuth2) - For Model Context Protocol integration
- Direct Client (PyMongo) - For direct MongoDB connection
- Query Executor β Router - Checks execution result:
- β Success β Go to Output Parser
- β Error (recoverable) β Go to Query Refiner
- π Fatal Error β Return error to user
- Query Refiner β Query Executor - LLM fixes the query and retries (max retry limit: 1)
- Output Parser β Result - LLM converts raw MongoDB results to natural language
- Return - Final answer delivered to user
Made with β€οΈ by the MongoDB Agent Team