RAG 向け FAISS 搭載 MCP サーバー
このプロジェクトは、AI エージェントがベクター データベースを照会し、検索拡張生成 (RAG) に関連するドキュメントを取得できるようにする Machine Conversation Protocol (MCP) サーバーの概念実証実装を提供します。
特徴
- MCPエンドポイントを備えたFastAPIサーバー
- FAISSベクターデータベース統合
- ドキュメントのチャンク化と埋め込み
- GitHub Moveファイルの抽出と処理
- 完全な RAG ワークフローのための LLM 統合
- シンプルなクライアントの例
- サンプル文書
インストール
pipxの使用(推奨)
pipx は、分離された環境で Python アプリケーションをインストールして実行するのに役立つツールです。
- まず、pipx がインストールされていない場合はインストールします。
# On macOS
brew install pipx
pipx ensurepath
# On Ubuntu/Debian
sudo apt update
sudo apt install python3-pip python3-venv
python3 -m pip install --user pipx
python3 -m pipx ensurepath
# On Windows with pip
pip install pipx
pipx ensurepath
- プロジェクト ディレクトリから MCP サーバー パッケージを直接インストールします。
# Navigate to the directory containing the mcp_server folder
cd /path/to/mcp-server-project
# Install in editable mode
pipx install -e .
- (オプション) 環境変数を設定します。
.env.example
を.env
にコピーする- レート制限を上げるには、GitHub トークンを追加してください:
GITHUB_TOKEN=your_token_here
- RAG 統合用の OpenAI またはその他の LLM API キーを追加します:
OPENAI_API_KEY=your_key_here
手動インストール
pipx を使用したくない場合は、次の手順に従ってください。
- リポジトリをクローンする
- 依存関係をインストールします:
cd mcp_server
pip install -r requirements.txt
pipxでの使用
pipx でインストールすると、次のコマンドにアクセスできるようになります。
GitHubからMoveファイルをダウンロードする
# Download Move files with default settings
mcp-download --query "use sui" --output-dir docs/move_files
# Download with more options
mcp-download --query "module sui::coin" --max-results 50 --new-index --verbose
GitHub の検索とインデックスの改善(推奨)
# Search GitHub and index files with default settings
mcp-search-index --keywords "sui move"
# Search multiple keywords and customize options
mcp-search-index --keywords "sui move,move framework" --max-repos 30 --output-results --verbose
# Save search results and use a custom index location
mcp-search-index --keywords "sui coin,sui::transfer" --index-file custom/path/index.bin --output-results
mcp-search-index
コマンドは、強化された GitHub リポジトリ検索機能を提供します。
- 最初にリポジトリを検索し、次に再帰的にMoveファイルを抽出します
- 複数の検索キーワード(カンマ区切り)をサポート
- 「use sui」参照を含むMoveファイルをインテリジェントにフィルタリングします
- ダウンロード後にベクターデータベースを常に再構築します
インデックス作成ファイルの移動
# Index files in the default location
mcp-index
# Index with custom options
mcp-index --docs-dir path/to/files --index-file path/to/index.bin --verbose
ベクターデータベースのクエリ
# Basic query
mcp-query "What is a module in Sui Move?"
# Advanced query with options
mcp-query "How do I define a struct in Sui Move?" -k 3 -f
LLM統合によるRAGの使用
# Basic RAG query (will use simulated LLM if no API key is provided)
mcp-rag "What is a module in Sui Move?"
# Using with a specific LLM API
mcp-rag "How do I define a struct in Sui Move?" --api-key your_api_key --top-k 3
# Output as JSON for further processing
mcp-rag "What are the benefits of sui::coin?" --output-json > rag_response.json
サーバーの実行
# Start the server with default settings
mcp-server
# Start with custom settings
mcp-server --host 127.0.0.1 --port 8080 --index-file custom/path/index.bin
手動での使用(pipxなし)
サーバーの起動
cd mcp_server
python main.py
サーバーはhttp://localhost:8000で起動します。
GitHubからMoveファイルをダウンロードする
GitHub から Move ファイルをダウンロードし、ベクター データベースに入力するには:
# Download Move files with default query "use sui"
./run.sh --download-move
# Customize the search query
./run.sh --download-move --github-query "module sui::coin" --max-results 50
# Download, index, and start the server
./run.sh --download-move --index
Python スクリプトを直接使用することもできます。
python download_move_files.py --query "use sui" --output-dir docs/move_files
文書のインデックス作成
クエリを実行する前に、ドキュメントをインデックスする必要があります。テキストファイル(.txt)、Markdownファイル(.md)、またはMoveファイル(.move)をdocs
ディレクトリに配置できます。
ドキュメントにインデックスを付けるには、次のいずれかの方法があります。
--index
フラグ付きの実行スクリプトを使用します。
- インデックス スクリプトを直接使用します。
python index_move_files.py --docs-dir docs/move_files --index-file data/faiss_index.bin
ドキュメントのクエリ
ローカル クエリ スクリプトを使用できます。
python local_query.py "What is RAG?"
# With more options
python local_query.py -k 3 -f "How to define a struct in Sui Move?"
LLM統合によるRAGの使用
# Direct RAG query with an LLM
python rag_integration.py "What is a module in Sui Move?" --index-file data/faiss_index.bin
# With API key (if you have one)
OPENAI_API_KEY=your_key_here python rag_integration.py "How do coins work in Sui?"
MCP APIエンドポイント
MCP APIエンドポイントは/mcp/action
で利用可能です。これを使用して、さまざまなアクションを実行できます。
retrieve_documents
: クエリに関連する文書を取得するindex_documents
: ディレクトリからドキュメントをインデックスする
例:
curl -X POST "http://localhost:8000/mcp/action" -H "Content-Type: application/json" -d '{"action_type": "retrieve_documents", "payload": {"query": "What is RAG?", "top_k": 3}}'
完全なRAGパイプライン
完全な RAG (Retrieval-Augmented Generation) パイプラインは次のように機能します。
- 検索クエリ: ユーザーが質問を送信する
- 検索: システムはベクターデータベースから関連文書を検索します
- コンテキスト形成: 取得した文書はプロンプトにフォーマットされる
- LLM生成: プロンプトは取得されたコンテキストとともにLLMに送信される。
- 強化された応答: LLMは取得した情報に基づいて回答を提供します
このワークフローはrag_integration.py
モジュールに完全に実装されており、コマンドラインから、または独自のアプリケーションのライブラリとして使用できます。
GitHub 移動ファイルの抽出
このシステムは、検索クエリに基づいてGitHubからMoveファイルを抽出できます。以下の2つのメソッドを実装しています。
- GitHub API (推奨):より高いレート制限にはGitHubトークンが必要です
- Webスクレイピングフォールバック: APIメソッドが失敗した場合、またはトークンが提供されていない場合に使用されます。
GitHub トークンを設定するには、 .env
ファイルまたは環境変数として設定します。
GITHUB_TOKEN=your_github_token_here
プロジェクト構造
mcp_server/
├── __init__.py # Package initialization
├── main.py # Main server file
├── mcp_api.py # MCP API implementation
├── index_move_files.py # File indexing utility
├── local_query.py # Local query utility
├── download_move_files.py # GitHub Move file extractor
├── rag_integration.py # LLM integration for RAG
├── pyproject.toml # Package configuration
├── requirements.txt # Dependencies
├── .env.example # Example environment variables
├── README.md # This file
├── data/ # Storage for the FAISS index
├── docs/ # Sample documents
│ └── move_files/ # Downloaded Move files
├── models/ # Model implementations
│ └── vector_store.py # FAISS vector store implementation
└── utils/
├── document_processor.py # Document processing utilities
└── github_extractor.py # GitHub file extraction utilities
プロジェクトの拡張
この概念実証を拡張するには:
- 認証とセキュリティ機能を追加する
- より洗練されたドキュメント処理を実装する
- より多くのドキュメントタイプのサポートを追加
- 他のLLMプロバイダーとの統合
- 監視とログ記録を追加する
- より構造化されたデータ抽出のためにMove言語の解析を改善
ライセンス
マサチューセッツ工科大学