Get started with MongoDB Agent in 5 minutes!
✅ Python 3.9 or higher
✅ MongoDB instance (local or remote)
✅ LLM API key (OpenAI, Azure, or Anthropic)
The setup script will guide you through the installation:
# Run interactive setup
./setup.shYou'll be asked to choose:
- MCP Protocol - For Claude Desktop / AI assistant integrations
- Direct Connection - For standalone use (simpler setup)
- Requires MCP server running separately
- Best for AI assistant integrations
- See: https://github.com/modelcontextprotocol/servers
- No MCP server needed
- Connects directly via PyMongo
- Best for local development and testing
After setup, edit .env with your credentials:
nano .env # or use your favorite editorOption A: Direct MongoDB Connection (Simplest)
# MongoDB Connection
MONGODB_CONNECTION_TYPE=direct
MONGODB_URI=mongodb://localhost:27017
MONGODB_DATABASE=your_database_name
# LLM Provider
LLM_PROVIDER=openai
OPENAI_API_KEY=sk-your-key-hereOption B: MCP Protocol Connection
# MongoDB Connection
MONGODB_CONNECTION_TYPE=mcp
MONGODB_MCP_ENDPOINT=http://localhost:3000/mongodb/query
# LLM Provider
LLM_PROVIDER=openai
OPENAI_API_KEY=sk-your-key-hereOption C: Azure OpenAI + Direct MongoDB
# MongoDB Connection
MONGODB_CONNECTION_TYPE=direct
MONGODB_URI=mongodb://localhost:27017
MONGODB_DATABASE=your_database_name
# LLM Provider
LLM_PROVIDER=azure
AZURE_OPENAI_ENDPOINT=https://your-endpoint.openai.azure.com
AZURE_OPENAI_API_KEY=your-key
AZURE_OPENAI_DEPLOYMENT_NAME=gpt-4o-miniStart the server to access the web interface and API:
# 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 access:
- 📖 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
Create semantic_models/my_collection.yaml:
collection_info:
database: "myapp"
schema_name: "orders"
collections:
orders:
name: orders
description: "Customer orders collection"
fields:
_id:
data_type: "ObjectId"
description: "Unique order ID"
orderDate:
data_type: "date"
description: "Order creation date"
customerName:
data_type: "string"
description: "Customer name"
totalAmount:
data_type: "number"
description: "Total order amount"
status:
data_type: "string"
description: "Order status"
sample_values: ["pending", "shipped", "delivered"]
verified_queries:
- name: "RecentOrders"
question: "Show me orders from last 30 days"
mongodb_query: 'db.orders.find({"orderDate": {"$gte": new Date(Date.now() - 30*24*60*60*1000)}})'python3 -m mongodb_agent.cli server --port 8000python3 -m mongodb_agent.cli server --port 8000 --mode restfrom mongodb_agent import MongoDBAgent, Config
# Load config from .env
config = Config.from_env()
# Create agent
agent = MongoDBAgent(config)
# Run a query
result = agent.query(
question="Show me all orders from last month",
yaml_file_name="my_collection.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": "my_collection.yaml"
}'Add to your Claude Desktop config (~/.config/Claude/claude_desktop_config.json):
{
"mcpServers": {
"mongodb-agent": {
"command": "python3",
"args": ["-m", "mongodb_agent.cli", "server", "--port", "8000"]
}
}
}Then in Claude: "Using mongodb-agent, show me all orders from last month"
If everything is set up correctly, you should see:
✅ Server started on http://127.0.0.1:8000
✅ MongoDB Agent initialized
✅ LLM provider: openai
✅ Vector DB: local
✅ Ready to accept queries
Try these natural language questions:
- "How many orders were placed last week?"
- "Show me the top 10 customers by total order value"
- "What is the average order amount for each month?"
- "Find all pending orders older than 7 days"
- "Group orders by status and count them"
# Check if port is in use
lsof -i :8000
# Use a different port
python3 -m mongodb_agent.cli server --port 8001# Check MCP endpoint is running
curl http://localhost:3000/health
# Verify MongoDB connection
mongo --eval "db.adminCommand('ping')"# Test your API key
curl https://api.openai.com/v1/models \\
-H "Authorization: Bearer $OPENAI_API_KEY"# Reinstall package
pip uninstall mongodb-agent
pip install mongodb_agent-1.0.0-py3-none-any.whl --force-reinstall- Read the full USER_GUIDE.md for advanced features
- Check API_REFERENCE.md for API details
- See examples/ for more code examples
- Review CONFIGURATION.md for all config options
- Use semantic models - They dramatically improve query accuracy
- Add verified queries - Pre-define common queries for best results
- Enable token caching - Speeds up repeated queries
- Start with simple queries - Test basic functionality first
- Check the logs - Located in
logs/mongodb_agent.log
- Documentation: See
docs/folder - Examples: See
examples/folder - Issues: GitHub Issues
- Troubleshooting: TROUBLESHOOTING.md
Happy querying! 🎉