MCP RAG Server

Integrations

  • Implements a server with MCP endpoints using FastAPI, providing a way to expose the vector database retrieval functionality via API.

  • Allows searching and extracting Move files from GitHub repositories based on search queries, with support for both GitHub API and web scraping fallback methods.

  • Enables integration with OpenAI models for the RAG (Retrieval-Augmented Generation) pipeline, allowing enhanced responses based on retrieved information from the vector database.

RAG용 FAISS가 포함된 MCP 서버

이 프로젝트는 AI 에이전트가 벡터 데이터베이스를 쿼리하고 검색 증강 생성(RAG)을 위해 관련 문서를 검색할 수 있도록 하는 MCP(Machine Conversation Protocol) 서버의 개념 증명 구현을 제공합니다.

특징

  • MCP 엔드포인트가 있는 FastAPI 서버
  • FAISS 벡터 데이터베이스 통합
  • 문서 청킹 및 임베딩
  • GitHub 이동 파일 추출 및 처리
  • 완전한 RAG 워크플로를 위한 LLM 통합
  • 간단한 클라이언트 예
  • 샘플 문서

설치

pipx 사용(권장)

pipx는 격리된 환경에서 Python 애플리케이션을 설치하고 실행하는 데 도움이 되는 도구입니다.

  1. 먼저 pipx가 없다면 설치하세요.

지엑스피1

  1. 프로젝트 디렉토리에서 MCP 서버 패키지를 직접 설치하세요.
# Navigate to the directory containing the mcp_server folder cd /path/to/mcp-server-project # Install in editable mode pipx install -e .
  1. (선택 사항) 환경 변수 구성:
    • .env.example``.env 로 복사합니다.
    • 더 높은 요금 한도를 위해 GitHub 토큰을 추가하세요: GITHUB_TOKEN=your_token_here
    • RAG 통합을 위해 OpenAI 또는 다른 LLM API 키를 추가하세요: OPENAI_API_KEY=your_key_here

수동 설치

pipx를 사용하지 않으려면:

  1. 저장소를 복제합니다
  2. 종속성 설치:
cd mcp_server pip install -r requirements.txt

pipx와 함께 사용

pipx로 설치한 후에는 다음 명령을 사용할 수 있습니다.

GitHub에서 이동 파일 다운로드

# 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 저장소 검색 기능을 제공합니다.

  • 먼저 저장소를 검색한 다음 재귀적으로 추출하여 파일을 이동합니다.
  • 여러 검색 키워드 지원(쉼표로 구분)
  • "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에서 이동 파일 다운로드

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), 마크다운 파일(.md) 또는 Move 파일(.move)을 docs 디렉터리에 저장할 수 있습니다.

문서를 색인하려면 다음 중 하나를 수행하세요.

  1. --index 플래그와 함께 실행 스크립트를 사용하세요.
./run.sh --index
  1. 인덱스 스크립트를 직접 사용하세요:
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(검색 증강 생성) 파이프라인은 다음과 같이 작동합니다.

  1. 검색어 : 사용자가 질문을 제출합니다.
  2. 검색 : 시스템은 벡터 데이터베이스에서 관련 문서를 검색합니다.
  3. 컨텍스트 형성 : 검색된 문서가 프롬프트로 포맷됩니다.
  4. LLM 생성 : 프롬프트는 검색된 컨텍스트와 함께 LLM으로 전송됩니다.
  5. 향상된 응답 : LLM은 검색된 정보를 기반으로 답변을 제공합니다.

이 워크플로는 rag_integration.py 모듈에 완전히 구현되어 있으며, 명령줄을 통해서나 사용자 애플리케이션의 라이브러리로 사용할 수 있습니다.

GitHub 이동 파일 추출

이 시스템은 검색 쿼리를 기반으로 GitHub에서 Move 파일을 추출할 수 있습니다. 이 시스템은 두 가지 메서드를 구현합니다.

  1. GitHub API (권장): 더 높은 속도 제한을 위해 GitHub 토큰이 필요합니다.
  2. 웹 스크래핑 폴백 : 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

프로젝트 확장

이 개념 증명을 확장하려면 다음을 수행하세요.

  1. 인증 및 보안 기능 추가
  2. 더욱 정교한 문서 처리를 구현하세요
  3. 더 많은 문서 유형에 대한 지원 추가
  4. 다른 LLM 제공자와 통합
  5. 모니터링 및 로깅 추가
  6. 더욱 구조화된 데이터 추출을 위해 Move 언어 구문 분석을 개선합니다.

특허

MIT

ID: 6iuvrbrfzb