Skip to main content
Glama

FastAPI + MCP によるエージェント型 RAG アシスタント

採用担当者が現在チェックしている4つの要素を組み合わせた社内ドキュメント用アシスタントです:

  • エージェント型 AI — LLM がナレッジベースを検索するか、計算を実行するか、直接回答するかを判断するツール使用ループ。

  • FastAPI — 自動生成される Swagger ドキュメントを備えたクリーンな REST バックエンド(/ingest、/query、/agent/chat)。

  • RAG — ドキュメントはチャンク化・インデックス化され、LLM が回答する前に意味的関連性によって検索されます。

  • MCP(Model Context Protocol) — 同じツール(RAG 検索+ビジネスロジック)が MCP サーバーとして公開され、この API だけでなく、MCP 互換の任意のクライアント(Claude Desktop、Claude Code など)が直接利用できます。

アーキテクチャ

                     ┌────────────────────┐
                     │   FastAPI Service   │
                     │  (app/main.py)      │
                     └─────────┬───────────┘
                               │
                     ┌─────────▼───────────┐
                     │   Agent Loop         │◄──── Groq API (tool use)
                     │  (app/agent.py)      │
                     └─────────┬───────────┘
                               │ calls
                 ┌─────────────┼──────────────┐
                 ▼                             ▼
        ┌────────────────┐           ┌──────────────────┐
        │  RAG Engine      │           │  Business Tools    │
        │  (app/rag.py)    │           │  (app/tools.py)     │
        └────────────────┘           └──────────────────┘
                 ▲                             ▲
                 └─────────────┬───────────────┘
                                │  same tools, exposed via
                     ┌──────────▼───────────┐
                     │   MCP Server           │
                     │  (app/mcp_server.py)   │
                     └────────────────────────┘

Related MCP server: mcp-docpilot-server

セットアップ

python -m venv venv
source venv/bin/activate       # Windows: venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env           # add your GROQ_API_KEY

API の実行

uvicorn app.main:app --reload --port 8000

対話型の Swagger ドキュメントは http://localhost:8000/docs を開いてください。

試してみる

# Pure retrieval, no LLM call
curl -X POST http://localhost:8000/query \
  -H "Content-Type: application/json" \
  -d '{"query": "how many days of leave do I get?"}'

# Full agentic chat — LLM decides which tool(s) to call
curl -X POST http://localhost:8000/agent/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "I joined in March and have taken 4 days off. How much leave do I have left, and what is the WFH policy?"}'

2回目の呼び出しはマルチツール推論を示しています。モデルが1つの会話の中で、WFH ポリシーについては rag_search を、計算については calculate_leave_balance を呼び出します。

MCP サーバーとして実行

python -m app.mcp_server

任意の MCP ホストを stdio 経由でこのスクリプトに接続すると(例:Claude Desktop の claude_desktop_config.json にカスタム MCP サーバーとして追加)、rag_search、get_current_datetime、calculate_leave_balance が呼び出し可能なツールとして公開されます。

面接でこのプロジェクトについて話すポイント

  • デフォルトで埋め込みではなく TF-IDF を使う理由:API キーも外部ダウンロードも不要でデモを実行可能に保てるため。VectorStore クラスは、FAISS/Chroma+実際の埋め込みへの差し替えが書き直しではなくドロップインで済むように設計されています。

  • ツールが1つのファイル(tools.py)にあり、2回公開される理由(agent.py と mcp_server.py):単一の情報源であり、HTTP パスと MCP パスの間でロジックの重複がありません。

  • エージェントループはツール使用パターンの手動実装であり(ブラックボックスフレームワークではありません)、すべてのステップを説明できます:モデルがツールを要求 → サーバーが実行 → 結果がフィードバックされる → モデルが続行または回答。

考えられる拡張(「どう改善しますか」への良い回答)

  • TF-IDF を実際の埋め込み(OpenAI/Voyage/ローカルの sentence-transformers)+永続的なベクターデータベースに置き換える。

  • ターン間の会話メモリを追加する(現在、各 /agent/chat 呼び出しはステートレス)。

  • Server-Sent Events によるストリーミング応答を追加する。

  • 公開デプロイ前に認証(API キーまたは JWT)を追加する。

  • Docker + docker-compose でコンテナ化し、ワンコマンドで起動できるようにする。

Related MCP Connectors

Related MCP Servers

  • F
    license
    Not graded
    quality
    B
    maintenance
    Enables any MCP-compatible AI assistant to search, filter, and retrieve information from a local document collection using a hybrid search pipeline with vector, BM25, reranking, and LLM enrichment.
    4
    -
  • F
    license
    Not graded
    quality
    D
    maintenance
    An MCP server that exposes document retrieval as tools (semantic search and source listing) for any LLM, using a vector index built from DocPilot's ingestion pipeline.
    -
  • F
    license
    Not graded
    quality
    D
    maintenance
    Enables local document question-answering and retrieval via MCP, supporting multi-turn conversation, intent recognition, and tools for document search, Q&A, and summarization.
    5
    -