使用说明
FastAPI Todo应用程序
一个现代的RESTful API,用于管理使用FastAPI和SQLite构建的todo项。此应用程序为todo管理提供了一个完整的CRUD接口,并为AI驱动的交互提供了集成的模型上下文协议(MCP)支持。
概述
该项目演示了一个生产就绪的FastAPI应用程序,包括:
- todo项目的完整CRUD操作
- 具有自动模式初始化功能的SQLite数据库后端
- 用于请求/响应验证的Pydantic模型
- 通过Swagger UI和ReDoc提供交互式API文档
- 用于AI代理交互的MCP服务器集成
- 正确的错误处理和HTTP状态代码
特性
- 创建全部:添加新的待办事项,包括内容和完成状态
- 阅读全部:检索所有待办事项或按ID获取特定待办事项
- 更新待办事项:部分更新待办事项(内容和/或完成状态)
- 删除待办事项:从数据库中删除todo项目
- MCP集成:通过AI代理的模型上下文协议公开API操作
- 汽车文档:交互式API文档可在
/docs和/redoc - 类型安全:完整的类型提示和Pydantic验证
- 数据库管理:自动架构初始化和连接处理
技术栈
- 快速API:用于构建API的现代快速web框架
- SQLite:轻量级关系数据库
- 派丹蒂克:使用Python类型注释进行数据验证
- Uvicorn:用于运行应用程序的ASGI服务器
- FastAPI MCP:用于FastAPI应用程序的MCP服务器集成
安装
先决条件
- Python 3.8或更高版本
- pip或uv包管理器
设置
- 克隆存储库:
git clone <repository-url>
cd fastapi-mcp-todo
- 安装依赖项:
pip install -r requirements.txt
或使用紫外线:
uv pip install -r requirements.txt
- 运行应用程序:
python main.py
或者直接使用uvicorn:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
API将于 http://localhost:8000
API文档
服务器运行后,您可以访问:
- Swagger用户界面:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc - 根端点:
http://localhost:8000/
API终点
根
获取 /
返回包含API信息的欢迎消息。
答复:
{
"message": "Welcome to the Todo API",
"version": "1.0.0",
"docs": "/docs"
}
获取所有待办事项
获取 /todos
检索所有待办事项的列表。
答复: 200 OK
[
{
"todo_id": 1,
"content": "Complete project documentation",
"completed": false
},
{
"todo_id": 2,
"content": "Review code changes",
"completed": true
}
]
按ID获取待办事项
获取 /todos/{todo_id}
按ID检索特定待办事项。
参数:
todo_id(path):todo项目的整数ID
答复: 200 OK
{
"todo_id": 1,
"content": "Complete project documentation",
"completed": false
}
错误响应: 404 Not Found
{
"detail": "Todo with id {todo_id} not found"
}
创建待办事项
发布 /todos
创建一个新的待办事项。
请求正文:
{
"content": "New todo item",
"completed": false
}
答复: 201 Created
{
"todo_id": 3,
"content": "New todo item",
"completed": false
}
更新待办事项
放 /todos/{todo_id}
更新现有待办事项。支持部分更新。
参数:
todo_id(path):todo项目的整数ID
请求正文:
{
"content": "Updated content",
"completed": true
}
这两个字段都是可选的。您可以仅更新 content 或者只是 completed.
答复: 200 OK
{
"todo_id": 1,
"content": "Updated content",
"completed": true
}
错误响应: 404 Not Found
{
"detail": "Todo with id {todo_id} not found"
}
删除待办事项
删除 /todos/{todo_id}
按ID删除待办事项。
参数:
todo_id(path):todo项目的整数ID
答复: 204 No Content
错误响应: 404 Not Found
{
"detail": "Todo with id {todo_id} not found"
}
MCP集成
该应用程序包括模型上下文协议(MCP)服务器集成,允许AI代理通过标准化的MCP操作与API交互。以下操作已公开:
get_all_todos:检索所有待办事项get_todo:按ID获取特定待办事项create_todo:创建新的待办事项update_todo:更新现有待办事项delete_todo:删除待办事项
应用程序启动时,MCP服务器会自动挂载。
数据库模式
该应用程序使用具有以下模式的SQLite:
CREATE TABLE todos (
todo_id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL,
completed BOOLEAN NOT NULL DEFAULT 0
)
数据库文件(todos.db)在第一次运行时自动创建。架构在应用程序启动期间初始化。
项目结构
fastapi-mcp-todo/
├── main.py # Main application file with all endpoints
├── requirements.txt # Python dependencies
├── render.yaml # Render deployment configuration
├── todos.db # SQLite database (created automatically)
└── README.md # This file
发展
以开发模式运行
对于自动重新加载的开发:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
代码结构
- 数据库管理:用于SQLite连接的上下文管理器,具有适当的事务处理功能
- Pydantic模型:
TodoCreate,TodoUpdate,以及Todo用于请求/响应验证 - API终点:具有适当HTTP状态代码和错误处理的RESTful端点
- MCP服务器:用于AI代理交互的FastAPI MCP集成
主要特点
- 在整个代码库中键入提示
- 全面的文档字符串
- 正确处理HTTP异常
- 通过上下文管理器实现数据库连接池
- 错误时自动回滚事务
部署
该项目包括 render.yaml 用于在Render.com上部署的配置文件。该应用程序可以部署到支持Python和ASGI应用程序的任何平台。
渲染部署
这 render.yaml 文件配置:
- Web服务类型
- Python环境
- 使用uv构建命令
- 使用uvicorn启动命令
- 免费套餐
测试API
使用curl
# Get all todos
curl http://localhost:8000/todos
# Get a specific todo
curl http://localhost:8000/todos/1
# Create a todo
curl -X POST http://localhost:8000/todos \
-H "Content-Type: application/json" \
-d '{"content": "Test todo", "completed": false}'
# Update a todo
curl -X PUT http://localhost:8000/todos/1 \
-H "Content-Type: application/json" \
-d '{"completed": true}'
# Delete a todo
curl -X DELETE http://localhost:8000/todos/1
使用Python请求
import requests
# Get all todos
response = requests.get("http://localhost:8000/todos")
todos = response.json()
print(todos)
# Create a todo
new_todo = {
"content": "New todo item",
"completed": False
}
response = requests.post("http://localhost:8000/todos", json=new_todo)
print(response.json())
许可证
该项目是开源的,可用于教育目的。
作者
阿努巴夫
版本
1.0.0