MongoDB CRUD系统

一个集成了FastAPI后端、Streamlit前端和MCP协议的MongoDB CRUD系统,支持AI工具发现。

  • by Likith26-MO
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Likith26-MO/MongoDB-MCP-Tool

使用说明

MongoDB-MCP-Tool(中文可译为):MongoDB MCP 工具

全面的 MongoDB CRUD系统 和;与;带有 FastAPI 后端, Streamlit 前端,以及 MCP(模型上下文协议) 人工智能工具发现的集成。


🚀 特性

  • **RESTful API(Representational State Transfer(表述性状态转移)应用程序编程接口)**通过FastAPI实现完整的CRUD操作
  • 网页界面基于Streamlit的控制面板
  • MongoDB 集成直接数据库操作
  • MCP 支持通过模型上下文协议发现AI工具
  • 测试脚本用于API测试的PowerShell脚本
  • 样本数据自动填充150条学生记录

📋 先决条件

  • Python 3.8及以上版本
  • MongoDB(本地安装)
  • 所需的Python包:
pip install fastapi uvicorn pymongo streamlit requests pydantic python-multipart

🛠 安装

  1. 克隆仓库:
git clone https://github.com/Likith26-MO/MongoDB-MCP-Tool.git
cd MongoDB-MCP-Tool
  1. 确保 MongoDB 正在运行:
# Start MongoDB manually
mongod

# Or start as a service (Linux)
sudo systemctl start mongod
  1. 使用示例数据填充数据库:
python populate_mongo.py

🏃‍♂️ 运行应用程序

1️⃣ 启动FastAPI后端

uvicorn main:app --reload --host 0.0.0.0 --port 8000

2️⃣ 启动Streamlit前端

streamlit run mcp.py

📁 项目结构

MongoDB-MCP-Tool/
├── mongo_crud.py          # MongoDB CRUD operations
├── main.py                # FastAPI app with MCP endpoints
├── api_server.py          # Alternative FastAPI implementation
├── mongo_mcp_tool.py      # Complete MCP tool implementation
├── mcp.py                 # Streamlit interface + MCP router
├── populate_mongo.py      # Sample data generator
├── test_mongo_mcp.ps1     # PowerShell API tests
└── lan.py                  # LangGraph integration example

🔧 API 接口

CRUD 操作

  • POST /students – 创建新学生
  • GET /students – 列出学生(带分页)
  • GET /students/{id} – 通过MongoDB ID获取学生信息
  • PATCH /students/{id} – 更新学生信息
  • DELETE /students/{id} – 删除学生

MCP Discovery(可译为)“MCP发现”

  • GET /mcp – 发现可用的工具和功能

🎯 使用示例

创建一个学生

curl -X POST "http://localhost:8000/students" \
-H "Content-Type: application/json" \
-d '{
  "student_id": "S999",
  "first_name": "John",
  "last_name": "Doe",
  "age": 22,
  "scores": {"math": 95, "cs": 88, "db": 92},
  "active": true
}'

列出学生名单

curl "http://localhost:8000/students?limit=5&skip=0"

更新学生信息

curl -X PATCH "http://localhost:8000/students/{mongo_id}" \
-H "Content-Type: application/json" \
-d '{"age": 23}'

🖥 Streamlit 界面

  • 列出、创建、搜索、更新、删除学生信息
  • 分页和表单验证
streamlit run mcp.py

🧪 测试

  • PowerShell 测试:
.\test_mongo_mcp.ps1

🔍 MCP 集成

from mcp import MCPClient

mongo_mcp = MCPClient("http://127.0.0.1:8000/mcp")
tools = mongo_mcp.discover()
  • 使AI工具能够发现并与您的MongoDB CRUD系统进行交互。

🐛 故障排除

  • MongoDB 连接错误确保MongoDB正在运行并进行检查 mongo_crud.py 连接字符串
  • 端口已被占用更改端口或终止现有进程
  • Streamlit连接问题确保FastAPI服务器已首先运行

数据库重置

mongo
use classroom
db.students.deleteMany({})
python populate_mongo.py

📊 样本数据结构

{
  "student_id": "S001",
  "first_name": "First1",
  "last_name": "Last1",
  "age": 19,
  "scores": {"math": 61, "cs": 56, "db": 51},
  "active": true
}

🤝 贡献

  1. 为仓库创建分支
  2. 创建一个特性分支
  3. 提交您的更改
  4. 推送到分支
  5. 创建一个拉取请求

⚡ 快速入门概览

  1. 开始 MongoDB(译为“蒙哥达数据库”或直接音译为“蒙戈数据库”,但通常直接称为“MongoDB”)
  2. 开始 FastAPI 后端:
uvicorn main:app --reload
  1. 开始 Streamlit 前端:
streamlit run mcp.py
  1. 访问APIs于 http://localhost:8000/docs 翻译为中文是:“本地主机的8000端口文档页面”。不过,通常我们不会直接翻译网址,而是解释其含义或用途。在这个上下文中,可以理解为“访问本地运行的服务器上8000端口的文档页面”
  2. 访问 Streamlit 用户界面,地址为 http://localhost:8501 翻译为中文是:“本地主机上的8501端口”