支出跟踪FastAPI服务器
一个FastAPI应用程序,既可以用作REST API,也可以用作Claude Desktop的MCP(模型上下文协议)服务器。
📋 先决条件
🚀 安装
# Navigate to the project folder
cd fastapi-local-server
# Install dependencies
uv add fastapi uvicorn
🏃 运行服务器
选项1:作为FastAPI(REST API与文档一起运行)
uv run uvicorn main:app --reload --port 8000
| URL | 描述 |
|---|
| http://localhost:8000 | 根端点 |
| http://localhost:8000/docs | Swagger UI(交互式API文档) |
| http://localhost:8000/redoc | ReDoc(备选文档) |
选项2:作为MCP服务器运行(stdio)
uv run python server.py
选项3:安装到Claude桌面
uv run fastmcp install claude-desktop server.py
注: 安装后重新启动Claude Desktop。
📁 项目结构
fastapi-local-server/
├── main.py # FastAPI application with routes
├── server.py # MCP server wrapper (converts FastAPI → MCP)
├── pyproject.toml # Project dependencies
└── README.md # This file
🛠️ API终点
| 方法 | 端点 | 描述 |
|---|
| 得到 | / | Root-欢迎消息 |
| 职位 | /expenses | 添加新费用 |
| 得到 | /expenses | 列出所有费用 |
🔄 运作原理
┌─────────────────┐
│ FastAPI App │ ← REST API (main.py)
│ (main.py) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ FastMCP │ ← Converts FastAPI → MCP (server.py)
│ (server.py) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Claude Desktop │ ← Uses MCP tools
└─────────────────┘
📝 命令备忘单
| 任务 | 命令 |
|---|
| 运行FastAPI | uv run uvicorn main:app --reload |
| 运行MCP服务器 | uv run python server.py |
| 安装到Claude | uv run fastmcp install claude-desktop server.py |
| 添加依赖关系 | `uv add |
| ` |
| 同步依赖关系 | uv sync |
🔧 环境变量(可选)
要使用新的OpenAPI解析器并删除旧警告:
# PowerShell
$env:FASTMCP_EXPERIMENTAL_ENABLE_NEW_OPENAPI_PARSER = "true"
# Then run
uv run fastmcp install claude-desktop server.py
📚 相关链接