AI代理系统

一个基于FastMCP、LangGraph和Alembic的生产级AI代理系统,提供Web界面和数据库迁移功能,适用于产品管理和订单管理场景。

  • by yerakairzhan
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 yerakairzhan/ai-agent-mcp

使用说明

🤖 AI代理MCP项目

具有FastMCP、LangGraph、Alembic迁移和Web UI的生产就绪AI代理系统


🚀 快速开始

# Start everything
docker-compose up --build

# Open browser
http://localhost:8000

就是这样! web UI、API和数据库都已准备就绪。


🌐 网络界面

访问 终端风格UI 在根URL处(http://localhost:8000)

特性

  • ⚡ 实时代理查询
  • 🛠️ 单击以使用工具目录
  • 📊 12个MCP工具可用
  • 🎨 复古终端美学

查询示例

产品管理:

list all products
add product: Mouse Pad, price: 15.99, category: Accessories
update product 1 price 999
delete product 5
get statistics

订单管理:

order product 1 quantity 2
list all orders
update order 1 status completed
cancel order 1
get order statistics

🗄️ 数据库迁移

满的 蒸馏器 模式管理集成。

快速命令

# Check current version
docker exec -it ai-agent-mcp-api python migrate.py current

# Apply migrations
docker exec -it ai-agent-mcp-api python migrate.py upgrade

# Create new migration
docker exec -it ai-agent-mcp-api python migrate.py create "add field"

# View history
docker exec -it ai-agent-mcp-api python migrate.py history

添加新字段

1.编辑 database.py:

class Product(Base):
    # ... existing fields ...
    description = Column(String, nullable=True)  # NEW

2.创建迁移:

docker exec -it ai-agent-mcp-api python migrate.py create "add description"

3.申请:

docker exec -it ai-agent-mcp-api python migrate.py upgrade

📖 完整指南: 迁移.md


🏗️ 建筑

Web Browser (http://localhost:8000)
         │
         ▼
    FastAPI Server
    • Serves HTML UI at /
    • API at /api/v1/agent/query
         │
         ▼
   LangGraph Agent
   • MockLLM (intent parsing)
   • Tool executor
         │
    ┌────┴────┐
    ▼         ▼
MCP Product   MCP Order
Server        Server
(6 tools)     (6 tools)
    │         │
    └────┬────┘
         ▼
   SQLAlchemy ORM
         │
         ▼
  Alembic Migrations
         │
         ▼
   SQLite Database
   (/app/data/)

📡 API使用

健康检查

curl http://localhost:8000/health

代理查询

curl -X POST http://localhost:8000/api/v1/agent/query \
  -H "Content-Type: application/json" \
  -d '{"query": "list all products"}'

所有端点

端点 方法 描述
/ GET Web用户界面
/health GET 健康检查
/api/v1/agent/query POST 代理查询
/docs 获取 API文档

🛠️ MCP工具(共12个)

产品服务器(6个工具)

  • list_products -列出全部或按类别筛选
  • get_product -按ID获取产品
  • add_product -添加新产品
  • update_product -更新产品字段
  • delete_product -删除产品
  • get_statistics -产品统计

订单服务器(6个工具)

  • create_order -创建新订单
  • get_order -按ID获取订单
  • list_orders -列出全部或按状态筛选
  • update_order_status -更新状态
  • cancel_order -取消订单
  • get_order_statistics -订单统计

自定义代理工具(3个工具)

  • search_products_by_name -按名称搜索

总计:13个工具 (12 MCP+1自定义)


🧪 测试

# Run all tests (9 total)
pytest tests/ -v

# Run in Docker
docker exec -it ai-agent-mcp-api pytest tests/ -v

# With coverage
pytest tests/ --cov=. --cov-report=html

测试覆盖范围:

  • 3 API试验
  • 6工具执行测试
  • 3数据库测试

📁 项目结构

ai-agent-project/
├── api/
│   └── main.py             # FastAPI app
├── frontend/
│   └── index.html          # Web UI ⭐
├── agent/
│   ├── langgraph_agent.py  # LangGraph workflow
│   ├── mock_llm.py         # Intent parsing
│   ├── tool_executor.py    # Tool execution
│   ├── tools.py            # MCP implementations
│   └── custom_tools.py     # Custom tools
├── mcp_servers/
│   ├── product_server.py   # Product MCP (6 tools)
│   └── order_server.py     # Order MCP (6 tools)
├── alembic/                # Migrations ⭐
│   ├── versions/
│   │   └── 001_initial_migration.py
│   │  
│   ├── env.py
│   └── script.py.mako
├── tests/
│   └── test_all.py       # 9 tests
├── database.py             # SQLAlchemy models
├── migrate.py              # Migration CLI ⭐
├── Dockerfile
├── docker-compose.yml
├── requirements.txt
└── README.md           # This file

💻 发展

本地设置

python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python migrate.py init
uvicorn api.main:app --reload

Docker开发

# Rebuild
docker-compose up --build

# View logs
docker-compose logs -f

# Access container
docker exec -it ai-agent-mcp-api bash

# Stop
docker-compose down

🎯 主要特点

  1. 网络界面 -终端风格的用户界面,便于测试
  2. 迁移 -Alembic用于安全模式演化
  3. 双MCP -单独的产品和订单服务器
  4. 真实数据库 -所有操作都提交到SQLite
  5. Docker就绪 -单命令部署

🐛 故障排除

重置所有内容

docker-compose down
rm -rf data/
docker-compose up --build

检查迁移

docker exec -it ai-agent-mcp-api python migrate.py current
docker exec -it ai-agent-mcp-api python migrate.py history

Access 数据库

docker exec -it ai-agent-mcp-api sqlite3 /app/data/products.db
.tables
.schema products
.exit

🚀 现在试试

# 1. Start
docker-compose up --build

# 2. Open browser
http://localhost:8000

# 3. Try a query
"list all products"