order-management-mcp
Enables role-based order management operations such as viewing orders, checking refund eligibility, issuing refunds, deleting orders, and managing users through MCP tools with audit logging and permission checks.
README
Order Management API
A role-based order management backend built with FastAPI, the Model Context Protocol (MCP), and MySQL. Users authenticate with JWTs, and every action — viewing an order, issuing a refund, deleting an order, creating a manager — is authorized against a role-based permission matrix and written to an audit log.
Why this exists
Most "CRUD + JWT" portfolio projects stop at "does the token check pass?".
This one adds two things on top: a separate authorization layer that's
independently testable (permissions.py), and a tool-based execution
layer (MCP) that decouples "what can this user do" from "how the API is
shaped" — the same order-management logic could be driven by a REST API,
a CLI, or an LLM agent calling the same MCP tools.
Architecture
Client
│ HTTP + JWT
▼
FastAPI (main.py)
│ 1. Verifies JWT (auth.py)
│ 2. Calls MCP tool over HTTP (fastmcp Client)
▼
MCP Server (order_checking.py) ── binds to 127.0.0.1 only, see Security below
│ 3. Authorizes the action (permissions.py)
│ 4. Runs the DB operation (database.py)
│ 5. Writes an audit log entry (audit.py)
▼
MySQL (sql/schema.sql)
Authentication (who are you) and authorization (what are you allowed to
do) are deliberately split: FastAPI/auth.py only ever answers the first
question, permissions.py is the only place the second question gets
answered, and every MCP tool calls it before touching the database.
Roles & permissions
| Action | USER | MANAGER | ADMIN |
|---|---|---|---|
| View an order | own orders only | own orders + orders they manage | any |
| Check refund eligibility | own orders only | own orders + orders they manage | any |
| Refund an order | own orders only | own orders + orders they manage | any |
| Delete an order | ❌ | ❌ | ✅ |
| View user list | ❌ | users they manage | all |
| Create a manager | ❌ | ❌ | ✅ |
API endpoints
| Method | Path | Auth required | Description |
|---|---|---|---|
| GET | / |
– | Health check |
| POST | /login |
– | Exchange username/password for a JWT |
| GET | /order_status/{order_id} |
✅ | Get an order's status |
| GET | /refund_eligibility/{order_id} |
✅ | Check refund eligibility |
| POST | /refund/{order_id} |
✅ | Refund an order |
| DELETE | /order/{order_id} |
✅ | Delete an order (ADMIN only) |
| GET | /users |
✅ | List users visible to the caller |
| POST | /managers |
✅ | Create a new MANAGER (ADMIN only) |
Tech stack
FastAPI · FastMCP · SQLAlchemy · PyMySQL · PyJWT · bcrypt · Pydantic v2 · MySQL
Setup
- Install dependencies
pip install -r requirements.txt # or: uv sync - Configure environment
cp .env.example .env # fill in DATABASE_URL and JWT_SECRET - Create the database
mysql -u root -p < sql/schema.sql - Seed example data (creates ADMIN/MANAGER/USER accounts for testing)
python sql/seed.py - Run the MCP server
python order_checking.py - Run the API (separate terminal)
uvicorn main:app --reload - Log in via
POST /loginwith a seeded account (seesql/seed.pyfor credentials), then use the returnedaccess_tokenas a Bearer token on the protected routes.
Security design
- Passwords are hashed with bcrypt — never stored or logged in plaintext.
- JWTs carry
usernameandrole, are signed withJWT_SECRET, and expire afterJWT_EXPIRE_MINUTES(default 60). - The MCP server has no authentication of its own. Every tool in
order_checking.pytrusts theusername/roleit's handed by the caller — all real auth happens one layer up, in FastAPI. Because of that,config.pyhardcodesMCP_HOST = "127.0.0.1"rather than reading it from the environment, so the MCP server can never be exposed by a misconfigured.envor container setting. If you ever split the MCP server onto a different host from the API, add real authentication insideorder_checking.py's tools first — don't just relaxMCP_HOST. - SQL is 100% parameterized via SQLAlchemy's
text()— no string interpolation into queries anywhere. - Every tool call is audited: caller, role, action, target order,
allowed/denied, reason, and latency are written to
audit_logsand tologs/app.log, and a failure to write an audit log never crashes the request.
Known limitations
These are conscious scope cuts for a portfolio-sized project, not oversights:
- JWTs aren't re-checked against the DB, so a role change or account deactivation doesn't take effect until the caller's current token expires.
- No rate limiting on
/login(would sit better at a gateway/proxy layer than in the app anyway). refund_orderhas a small TOCTOU window — no row lock between the eligibility check and the update.- No CORS middleware; add it if this is ever called directly from a browser.
Project structure
main.py FastAPI app: routes, JWT-protected endpoints, MCP client
order_checking.py MCP server: tools + the RBAC/audit wiring around them
auth.py Password hashing, JWT issuing/verification
permissions.py The single source of truth for authorization decisions
database.py SQLAlchemy queries (users, orders, audit log)
audit.py Structured audit log writer
schemas.py Pydantic request/response models
config.py Environment-driven configuration
logger.py App-wide logging setup
sql/schema.sql Table definitions
sql/seed.py Example users/orders for local testing
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。