FastMCP Orders Server
FastMCP Orders Server
A Model Context Protocol (MCP) server built with FastMCP that exposes a tool for querying customer orders from a PostgreSQL database.
プロジェクト構造
├── 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 instance with a database containing the schema described below
データベーススキーマ
サーバーは2つのテーブルを想定しています:
テーブル | カラム |
|
|
|
|
提供されたSQLファイルを実行して作成します:
psql -d orders_db -f schema.sql
psql -d orders_db -f seed.sqlセットアップ
1. 依存関係のインストール
pip install -r requirements.txt2. データベース認証情報の設定
envテンプレートをコピーし、接続詳細を記入してください:
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経由でサーバーに接続し、3つのテストシナリオを実行します:
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.jsPythonサーバーの場合:
npx @modelcontextprotocol/inspector python mcp_server.pyインスペクタは
http://localhost:6287でブラウザウィンドウを開きます。UIで、ツールの下にあるget_customer_orders_above_amountを見つけます。
パラメータを入力し(例:
{"customer_id": 1, "min_amount": 100})、Callをクリックします。構造化されたJSONレスポンスを確認します。
ツール仕様
get_customer_orders_above_amount
合計がユーロで最低金額を超える顧客の注文を取得します。
パラメータ | 型 | 必須 | 説明 |
|
| いいえ | 顧客の主キー(両方指定された場合は優先) |
|
| いいえ | 代替検索用のメールアドレス |
|
| いいえ | ユーロでの最低注文金額(含む、デフォルト0.0) |
customer_idまたはcustomer_emailの少なくとも一方を指定する必要があります。
戻り値: list[dict] — 各dictはキーorder_id、order_date、total_amount、statusを持ちます。結果はorder_dateの降順で並べられます。
エラーレスポンスは、"error"キーを持つ単一のdictを含むリストとして返されます(生のスタックトレースはクライアントに漏れません)。
セキュリティに関する注意
ハードコードされた認証情報はありません — すべてのDBパラメータは
python-dotenvを介して読み込まれた環境変数から取得されます。パラメータ化されたクエリのみ — SQLインジェクションは、
$1、$2プレースホルダーを全体で使用することで防止されます。コネクションプーリング —
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.-