Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MCPを活用した検索システムの作り方/How to implement search syst...

quiver
April 17, 2025

MCPを活用した検索システムの作り方/How to implement search systems with MCP #catalks

MCPを活用した検索システムの作り方。あるいは、MCPサーバーはつなぐだけ、検索機能は別物ということについて。

解説ブログ https://dev.classmethod.jp/articles/catalk-how-to-implement-search-systems-with-mcp/

イベント https://classmethod.connpass.com/event/352139/

quiver

April 17, 2025
Tweet

More Decks by quiver

Other Decks in Technology

Transcript

  1. ⾃⼰紹介 2 • 名前 ◦ George Yoshida • ブログ ◦

    https://dev.classmethod.jp/author/quiver/ • 部署 ◦ クラスメソッドクラウド事業本部エンジニア • 検索周り ◦ RAG検索案件担当中 ◦ DevelopersIOの⼀世代前のAlgoliaを使った ブログ検索のインデクシング周りを担当 • 所属チームの内製化⽀援事例 ◦ 三菱UFJ銀⾏さま ◦ H2Oさま
  2. AWS MCP Server群 7 • Core MCP Server • Amazon

    Bedrock Knowledge Base Retrieval MCP Server ※検索系 • AWS CDK MCP Server • Cost Analysis MCP Server • Amazon Nova Canvas MCP Server • AWS Documentation MCP Server ※検索系 • AWS Lambda MCP Server • AWS Diagram MCP Server • AWS Terraform MCP Server
  3. インストール:MCPサーバー 9 • MCPはクライアント‧サーバー‧アーキテクチャー • AWS Documentation MCP Serverは STDIO⽅式でプロセス間通信

    • クライアントとサーバーを同じ環境にインストール • AWS Documentation MCP ServerはPythonで実装 • 環境構築⽤プログラム uv をインストール • uvx の絶対パスを控える $ curl -LsSf https://astral.sh/uv/install.sh | sh $ which uvx /Users/ユーザー名 /.local/bin/uvx
  4. インストール:Claude Desktopの設定ファイル 11 { "mcpServers": { "awslabs.aws-documentation-mcp-server": { "command": "/Users/ユーザー名

    /.local/bin/uvx", "args": ["awslabs.aws-documentation-mcp-server@latest"], "env": { "FASTMCP_LOG_LEVEL": "ERROR" }, "disabled": false, "autoApprove": [] } } } 先ほど控えた uvx の絶対パスを転載 ※サーバーをコンテナ化して起動させることも多いです ※トランスポートはSTDIOなので、クライアントプログラムがcommand/argsを元にサーバープログラムをサブプロセス起動
  5. search_documentation:リモートサービスに検索 16 $ curl -X POST https://proxy.search.docs.aws.amazon.com/search \ -H "Content-Type:

    application/json" -d '{ "textQuery": { "input": "S3 bucket naming rules" }, "contextAttributes": [{ "key": "domain", "value": "docs.aws.amazon.com" }], "acceptSuggestionBody": "RawText", "locales": ["en_us"]}' { "queryId": "40dd0cc1-4e0f-42af-9385-a1878fe3cbb5", "suggestions": [ { "textExcerptSuggestion": { "link": "https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-tables-buckets-naming.html", "title": "Amazon S3 table bucket, table, and namespace naming rules - Amazon Simple Storage Service", "suggestionBody": "For general purpose bucket naming rules, see General purpose bucket naming rules...", … AWSのオフィシャルなドキュメント検索APIに無認証で検索しているだけ
  6. read_documentation:ドキュメント取得 17 @mcp.tool() async def search_documentation(...) -> List[SearchResult]: … request_body

    = { 'textQuery': { 'input': search_phrase, }, 'contextAttributes': [{'key': 'domain', 'value': 'docs.aws.amazon.com'}], 'acceptSuggestionBody': 'RawText', 'locales': ['en_us'], } async with httpx.AsyncClient() as client: try: response = await client.post( SEARCH_API_URL, json=request_body, headers={'Content-Type': 'application/json', 'User-Agent': DEFAULT_USER_AGENT}, timeout=30, ) https://github.com/awslabs/mcp/blob/main/src/aws-documentation-mcp-server/awslabs/aws_documentation_mcp_server/server.py `httpx` でお馴染みのドキュメントサイトからドキュメントを取得
  7. read_documentation:ドキュメント整形 18 def extract_content_from_html(html: str) -> str: … try: #

    First use BeautifulSoup to clean up the HTML from bs4 import BeautifulSoup # Parse HTML with BeautifulSoup soup = BeautifulSoup(html, 'html.parser') … # Use markdownify on the cleaned HTML content content = markdownify.markdownify( str(main_content), … ) … return content https://github.com/awslabs/mcp/blob/main/src/aws-documentation-mcp-server/awslabs/aws_documentation_mcp_server/util.py `beautifulsoup`と`markdownify` でドキュメントを整形
  8. MCPサーバーはライトウェイトなプログラム 24 • 公式ドキュメント(※)から ◦ MCP Servers: Lightweight programs that

    each expose specific capabilities through the standardized Model Context Protocol • 重厚な作りの検索MCPサーバーはなにか間違えている可能性 ※ https://modelcontextprotocol.io/introduction
  9. ⾃然⾔語処理の教科書 29 • 著者 : ⼩町 守 • 出版社 :

    技術評論社 • 発売⽇ : 2024/5/24 • ISBN- : 978-4297138639 • 概要 ◦ 検索によらず、社内情報のAI活⽤といった広義の⾃然⾔語処 理プロジェクトの様々なロールに向けられた本 • おすすめの章 ◦ 第1章:⾃然⾔語システムのデザイン ◦ 第5章:⾔語資源のつくり⽅
  10. 検索システム 30 • 著者 : 打⽥智⼦‧古澤智裕‧⼤⾕ 純‧加藤 遼‧鈴⽊翔吾‧河野晋策 • 出版社

    : ラムダノート • 発売⽇ : 2022/5/2 • ISBN- : 978-4908686139 • 概要 ◦ 実務者のための開発改善ガイドブック • おすすめの章 ◦ 第7章:よい検索とは ◦ 第8章:検索システムプロジェクトの始め⽅