Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

MCP (Model Context Protocol)

What is mcp?

Claudeを開発するAnthropicによって、2024年11月にリリースされた規格. LLMに外部アクセスする機能を提供する.たとえば、ファイルやDB,ウェブ、アプリケーションなど.

MCPサーバー

AI(LLM)にアクセスさせたい外部のデータソースや実行できるアクションが定義されている.

MCPクライアント

AI(LLM)は必要に応じて、MCPサーバーと通信して、事前に定義されたアクションを実行する. たとえば、Claude Desktop など.

Claude Desktop

MCP の設定

「ファイル > 設定 > 開発者 > 構成を編集」で claude_desktop_config.json ファイルのある場所が開かれる. 通常は $env:appdata\claude の場所.

たとえば、filesystem という MCP サーバーを追加する場合、次の記述を追加する.

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-filesystem",
        "C:/Users/<user>/Downloads"
      ]
    }
  }
}

LibreChat

Gemini を使う場合、Claude Desktop の代わりに使う.

Setup

git clone https://github.com/danny-avila/LibreChat.git

# or

git clone git@github.com:danny-avila/LibreChat.git

.env.example をコピーして .env ファイルに. .env ファイルに必要な情報(APIキーなど)を設定.

docker compose up -d

起動が終わったら localhost:3080 にアクセス.ログイン画面になるので、新規登録してログイン.

debian + python + nodejs

LibreChatサーバーは node:alpine になっている.MCPなど利用しようとすると python などが必要になってくる. alpine では問題となるので、debian などを利用する.

MCP を使う

librechat.yaml ファイルを作成する.使用する MCP を記述する.

version: 1.2.1
cache: true
mcpServers:
  filesystem:
    command: npx
    args:
      - -y
      - "@modelcontextprotocol/server-filesystem"
      - /app/data  # Dockerコンテナ内のパス

次に、docker-compose.override.yml を作成して、filesystem用のディレクトリを設定

services:
  api:
    volumes:
    - type: bind
      source: ./librechat.yaml
      target: /app/librechat.yaml
    - type: bind
      source: ./data  # ホストマシン上のディレクトリ
      target: /app/data  # コンテナ内のマウントポイント

data ディレクトリを作成して、Docker コンテナを再構築、再起動

docker compose down
docker compose pull
docker compose up -d

LibreChat の Agents にする.サイドバーから Agent Builder を起動して、使用するモデルとツールを追加する. このツールで有効にする MCP の機能を選ぶ.

MCP

Memory

"memory": {
    "command": "npx",
    "args": ["-y", "@modelcontextprotocol/server-memory"]
}

Filesystem

"filesystem": {
    "command": "npx",
    "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed/files"]
},
ActionDescription
create_directoryCreate a new directory or ensure a directory exists.
directory_treeGet a recursive tree view of files and directories as a JSON structure.
edit_fileMake line-based edits to a text file.
get_file_infoRetrieve detailed metadata about a file or directory.
list_allowed_directoriesReturns the list of directories that this server is allowed to access.
list_directoryGet a detailed listing of all files and directories in a specified path.
move_fileMove or rename files and directories. Can move files between directories and rename them in a single operation.
read_fileRead the complete contents of a file from the file system.
read_multiple_filesRead the contents of multiple files simultaneously.
search_filesRecursively search for files and directories matching a pattern.
write_filesCreate a new file or completely overwrite an existing file with new content.

YouTube

"youtube": {
    "command": "npx",
    "args": [
        "-y",
        "@anaisbetts/mcp-youtube"
    ]
}

実行には yt-dlp が必要.

winget install yt-dlp

注意点:

  • 字幕の有無: 字幕が提供されていない動画は処理できません
  • 視覚情報: 画像や映像そのものは理解できず、字幕のテキストのみを処理します
  • 長い動画: 非常に長い動画は処理に時間がかかったり、トークンの制限にかかる場合があります
  • 多言語対応: 英語以外の言語でも動作しますが、精度は言語によって異なります

使用例 (Filesystem使用):

この動画を要約し、ドキュメントフォルダ/video-summary.md に保存してください:
https://www.youtube.com/watch?v=xxxxx

Web Fetch

"mcp-server-fetch": {
    "command": "uvx",
    "args": [
        "mcp-server-fetch"
    ]
},

Markdownify

"markdownify": {
    "command": "node",
    "args": [
        "<markdownify-mcp>/dist/index.js"
    ],
    "env": {
        "UV_PATH": "C:/Users/pixy/.cargo/bin"
    }
},

Playwright

"playwright": {
    "command": "npx",
    "args": [
        "@playwright/mcp@latest"
    ]
}

Tips

カスタムインストラクション

複数の MCP が存在する場合、どのMCPを使うか優先度を指定する.

## ウェブコンテンツ取得のMCP使い分け

ウェブからの情報取得が必要な場合は、以下の優先順位で対応してください:

1. 基本的には「fetch」MCPを優先して使用してください。
   - 単一ページの情報取得
   - 軽量で高速な動作が必要な場合
   - APIドキュメントの参照

2. 以下の場合のみ「perplexity-ask」MCPを確認の上使用してください:
   - 最新の情報やニュースが必要な場合
   - 特に時事問題や最新トレンドに関する調査
   - 複雑なリサーチクエリがある場合
   - 複数の情報源からの統合された情報が必要な場合
   - ※ `Perplexityで` や `パープレで` と指示がある場合は確認不要

3. 以下の場合のみ「markdownify」MCPを使用してください:
   - PDF、Office文書、画像、オーディオなどの変換が必要な場合
   - コンテンツの高度な構造化と再構成が必要な場合
   - YouTube字幕の取得が必要な場合

4. 以下の場合のみ「firecrawl-mcp」MCPを確認の上使用してください:
   - 複数ページにまたがる情報収集が必要な場合
   - ウェブサイト全体のクローリングが必要な場合
   - 検索機能を使った情報の絞り込みが必要な場合
   - ディープリサーチが必要な場合
   - YouTube字幕の取得が必要な場合
   - ※ `Firecrawlで` と指示がある場合は確認不要

特に指示がない限り、常に「fetch」MCP を最初に試してください。他のMCPは、fetch MCPで対応できない特殊なケースでのみ使用してください。特に「firecrawl-mcp」と「perplexity-ask」は実行前に使用するかユーザーに確認してください。

MODULE NOT FOUND

npm または npx のキャッシュをクリアする.

npm cache clean --force
npx clear-npx-cache

npx のキャッシュクリアは ~/.npm/_npx フォルダを削除でも同じ.

キャッシュの場所は以下のコマンド

npm config get cache