Skip to main content
Glama
qualiteg

sales-db

by qualiteg

売上データベース MCP サーバー チュートリアル

「先月いちばん売れた GPU はどれ?」とふつうの日本語で聞くと、AI が自分で SQL を組み立てて 売上データベースに問い合わせ、集計結果まで答えてくれる——それを実現する MCP サーバーのサンプルです。

Python + FastMCP で書かれていて、git clone から 3 コマンドで動きます。 解説記事は Qualiteg Blog に掲載しています。

クイックスタート

git clone https://github.com/qualiteg/mcp-server-tutorial.git
cd mcp-server-tutorial
pip install -r requirements.txt
python db_setup.py                  # ダミーデータ入りの SQLite を生成
python mcp_server_sales.py          # MCP サーバーを起動(stdio)

db_setup.py は固定シードで架空のデータを生成します(実在の企業・人物・価格とは関係ありません)。 誰が実行しても同じデータになるので、記事の実行結果と一致します。

  • customers … 顧客マスター 500 人

  • products … 商品マスター 72 種類(PC パーツ 8 カテゴリ)

  • sales_transactions … 売上明細 4,000 件(2024-01-01 〜 2026-12-31)

Related MCP server: JVLink MCP Server

AI から使う

Claude Code

claude mcp add sales-db -- python /path/to/mcp-server-tutorial/mcp_server_sales.py

Claude Desktop

claude_desktop_config.json に追記します。

{
  "mcpServers": {
    "sales-db": {
      "command": "python",
      "args": ["/path/to/mcp-server-tutorial/mcp_server_sales.py"]
    }
  }
}

登録できたら、こんなふうに話しかけてみてください。

  • 「職業別の売上トップ3を教えて」

  • 「2025年に発売されたハイエンドGPUで、売上が多い順に並べて」

  • 「月別の売上推移を出して、いちばん売れた月は?」

AI はまず get_database_stats でテーブル構造を確認し、そのうえで execute_sql_query に SQL を 渡して答えます。SQL を書いたことがない人でも、売上データベースに"話しかけられる"ようになります。

提供しているツール

ツール

役割

get_database_stats

テーブル構造・件数・データ期間・カテゴリ別売上の要約を返す(AI が最初に呼ぶ)

execute_sql_query

SELECT 文を実行して結果を返す

業務ごとに「売上集計ツール」「顧客分析ツール」と関数を並べる設計もできますが、SQL を書けるのは AI 側なので、汎用の SQL 実行ツールを 1 本渡すほうが応用が利きます。そのぶん安全側の手当ては サーバーの責任になるので、次の 3 つを実装しています。

  1. 読み取り専用: SQLite を mode=ro で開き、加えて SELECT 以外の文と書き換え系キーワードを拒否

  2. 実行時間の上限: 10 秒を超えるクエリは中断(set_progress_handler

  3. 返却行数の上限: 50 行まで。AI は平気で SELECT * FROM sales_transactions を投げてくるので、 AI に返す量はサーバー側で制御します

HTTP サーバーとして動かす

社内サーバーに常駐させて複数人・複数 AI から使う場合は Streamable HTTP で起動します。

python mcp_server_sales.py --http --port 9904
  • MCP エンドポイント: http://<サーバー>:9904/mcp

  • ヘルスチェック: http://<サーバー>:9904/health

Web 版の ChatGPT・Claude から使いたい場合

Web 版の ChatGPT や Claude のコネクタに登録するには、①インターネットから到達できる HTTPS の URL と、 ②MCP Authorization 仕様に沿った OAuth 認証が必要です。社内サーバーをそのまま公開するわけにもいかず、 OAuth を自前で実装するのも簡単ではありません。

この部分は WireCanal を使うと、受信ポートを開けずに解決できます。 MCP canal と OAuth 認証はフリープラン(無料・期間制限なし)でも利用できるので、このサンプルを そのまま Web 版 ChatGPT・Claude から試せます。手順は解説記事の後編で紹介しています。

動作環境

  • Python 3.10 以降

  • fastmcp 2.0 以降(動作確認: 3.4.5)

ライセンス

MIT License. サンプルコードなので自由に改変してお使いください。

A
license - permissive license
-
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • F
    license
    B
    quality
    B
    maintenance
    Enables natural language queries and analysis of Japanese horse racing data from JRA-VAN without writing SQL. Supports analyzing race results, jockey performance, breeding trends, and track conditions through conversation with Claude.
    Last updated
    25
    13
  • F
    license
    -
    quality
    D
    maintenance
    Provides a suite of business tools for interacting with a SQLite sales database, including SQL query execution, KPI calculations, and report generation. It enables AI agents to analyze sales data across customers, products, and orders using the Model Context Protocol.
    Last updated

View all related MCP servers

Related MCP Connectors

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/qualiteg/mcp-server-tutorial'

If you have feedback or need assistance with the MCP directory API, please join our Discord server