FastMCP Orders Server
FastMCP 订单服务器
一个基于 FastMCP 构建的模型上下文协议 (MCP) 服务器,提供了一个用于从 PostgreSQL 数据库查询客户订单的工具。
项目结构
├── mcp_server.py # FastMCP server — stdio transport (default), HTTP optional
├── mcp_client.py # Demo client — connects via stdio, calls the tool
├── .env.example # Template for DB credentials
├── requirements.txt # Python dependencies
├── schema.sql # DDL to create customers + orders tables
├── seed.sql # Sample data for local testing
└── README.md # This fileRelated MCP server: Mock Store MCP Server
先决条件
Python ≥ 3.10
PostgreSQL 实例,包含一个具有以下描述的数据库
数据库模式
服务器期望两张表:
表 | 列 |
|
|
|
|
运行提供的 SQL 文件来创建它们:
psql -d orders_db -f schema.sql
psql -d orders_db -f seed.sql设置
1. 安装依赖项
pip install -r requirements.txt2. 配置数据库凭据
复制环境变量模板并填写你的连接信息:
cp .env.example .env
nano .env # edit values to match your PostgreSQL instance运行服务器
Stdio 传输(默认)
python mcp_server.py这是本地 MCP 服务器的标准模式,适用于 Claude Desktop、MCP Inspector 以及任何兼容 stdio 的客户端。
HTTP 传输(可选——远程客户端)
python mcp_server.py --transport http --port 9000当客户端需要远程 HTTP 端点而不是本地进程时很有用。
运行演示客户端
捆绑的客户端通过 stdio 连接到服务器,并运行三个测试场景:
python mcp_client.py预期输出(加载种子数据后):
[TEST 1] Orders for customer_id=1 with min_amount=100.0
Order ID Date Amount (€) Status
--------------------------------------------------------
5 2025-06-15 180.75 € completed
3 2025-03-01 520.00 € shipped
1 2025-01-10 250.00 € completed
→ 3 order(s) found
[TEST 2] Orders for 'carlos@example.com' with min_amount=300.0
Order ID Date Amount (€) Status
--------------------------------------------------------
8 2025-05-10 890.00 € shipped
6 2025-02-01 340.00 € completed
→ 2 order(s) found
[TEST 3] Lookup nonexistent email (expecting error)
⚠ Customer not found (nobody@nowhere.com)使用 MCP Inspector 测试
MCP Inspector 是一个用于探索 MCP 服务器的交互式工具。
npx @modelcontextprotocol/inspector node mcp_server.js对于 Python 服务器:
npx @modelcontextprotocol/inspector python mcp_server.pyInspector 会在浏览器中打开一个窗口,地址为
http://localhost:6287。在 UI 中,找到 get_customer_orders_above_amount 位于 Tools 下。
填写参数(例如
{"customer_id": 1, "min_amount": 100})并点击 Call。检查结构化的 JSON 响应。
工具规范
get_customer_orders_above_amount
检索总金额超过指定欧元最小值的客户订单。
参数 | 类型 | 必需 | 描述 |
|
| 否 | 客户的主键(如果同时提供,则优先使用) |
|
| 否 | 用于替代查找的电子邮件地址 |
|
| 否 | 最小订单金额(欧元,包含,默认 0.0) |
必须至少提供 customer_id 或 customer_email 中的一个。
返回: list[dict]——每个字典包含键 order_id、order_date、total_amount、status。结果按 order_date 降序排列。
错误响应 返回为一个包含单个字典的列表,字典中有一个 "error" 键(原始的堆栈跟踪不会泄露给客户端)。
安全注意事项
没有硬编码的凭据——所有数据库参数都来自通过
python-dotenv加载的环境变量。仅使用参数化查询——通过在所有地方使用
$1、$2占位符来防止 SQL 注入。连接池——在服务器启动时创建一个
asyncpg连接池(2–10 个连接),并在关闭时销毁。
This server cannot be deployed
Maintenance
Related MCP Connectors
Query PostgreSQL databases in plain English — LLM-generated, safety-validated SQL.
Query Postgres, MySQL, SQL Server, Oracle, BigQuery, ClickHouse and Redshift from your AI client.
Deterministic safety, correctness & cost gate that vets Postgres SQL before your AI agent runs it.
Comprehensive PostgreSQL documentation and best practices, including ecosystem tools
Related MCP Servers
- -licenseNot gradedqualityNot gradedmaintenanceEnables connection to PostgreSQL databases for EC solution platforms to query and manage e-commerce data including orders, members, and other business information. Provides tools for direct SQL execution, table structure analysis, and specialized e-commerce data retrieval functions.-
- FlicenseNot gradedqualityDmaintenanceEnables AI agents to explore and query a mock e-commerce store's data including customers, products, inventory, and orders through conversational interactions backed by PostgreSQL.-
- AlicenseAqualityBmaintenanceEnables read-only interaction with PostgreSQL databases through natural language queries, supporting dynamic connections and secure query validation.3134 npm2MIT
- FlicenseNot gradedqualityDmaintenanceConnects LLMs to a PostgreSQL e-commerce database for sports supplements, enabling natural language product search, order management, and customer operations.-