Skip to main content
Glama
masalinas

FastMCP Orders Server

by masalinas

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 file

Related MCP server: Mock Store MCP Server

前提条件

  • Python ≥ 3.10

  • PostgreSQL instance with a database containing the schema described below

データベーススキーマ

サーバーは2つのテーブルを想定しています:

テーブル

カラム

customers

id (主キー), name (テキスト), email (テキスト, 一意)

orders

id (主キー), customer_id (外部キー→customers), order_date, total_amount (数値, €), status

提供されたSQLファイルを実行して作成します:

psql -d orders_db -f schema.sql
psql -d orders_db -f seed.sql

セットアップ

1. 依存関係のインストール

pip install -r requirements.txt

2. データベース認証情報の設定

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.js

Pythonサーバーの場合:

npx @modelcontextprotocol/inspector python mcp_server.py
  1. インスペクタはhttp://localhost:6287でブラウザウィンドウを開きます。

  2. UIで、ツールの下にあるget_customer_orders_above_amountを見つけます。

  3. パラメータを入力し(例:{"customer_id": 1, "min_amount": 100})、Callをクリックします。

  4. 構造化されたJSONレスポンスを確認します。

ツール仕様

get_customer_orders_above_amount

合計がユーロで最低金額を超える顧客の注文を取得します。

パラメータ

型

必須

説明

customer_id

int | None

いいえ

顧客の主キー(両方指定された場合は優先)

customer_email

str | None

いいえ

代替検索用のメールアドレス

min_amount

float

いいえ

ユーロでの最低注文金額(含む、デフォルト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接続)がサーバー起動時に一度作成され、シャットダウン時に閉じられます。

Related MCP Connectors

Related MCP Servers

  • -
    license
    Not graded
    quality
    Not graded
    maintenance
    Enables 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.
    -
  • A
    license
    A
    quality
    B
    maintenance
    Enables read-only interaction with PostgreSQL databases through natural language queries, supporting dynamic connections and secure query validation.
    3
    134 npm
    2
    MIT