langchain-mcp について
langchain-mcp は、LangChain に Model Context Protocol のツール呼び出し機能を追加する Python ライブラリです。MCP の ClientSession をツールキットに渡し、LangChain の BaseTool のリストを取得して利用します。READMEによると、LangChain には後継となる公式実装 langchain-mcp-adapters があります。
主な機能
- 01MCP の ClientSession を MCPToolkit で包む
- 02initialize と get_tools でツールを取得
- 03langchain_core の BaseTool リストを出力
- 04実行可能なデモスクリプトを同梱
活用シーン
- 01LangChain アプリケーションから MCP サーバーのツールを呼び出す
- 02ファイルシステム MCP サーバー経由で LLM にファイルを要約させる
MCP 対応
MCP クライアント側の機能で、ClientSession を通じて MCP サーバーに接続し、そのツールを LangChain 用のツールとして公開します。
設定
ローカルサーバーの設定はどのクライアントでも共通です。コピーしてクライアントの MCP 設定ファイルに貼り付けてください。
このサーバーのパッケージやリモートエンドポイントはまだ取得できていません。プロジェクトの README の手順に従ってください。
ツール
ローカルサーバーのツールはマシン上で起動して初めて取得できます。メンテナーはまだツール一覧を提供していません。
詳細
langchain-mcp
注:langchain には、より公式な実装である langchain-mcp-adapters が用意されています。
LangChain での Model Context Protocol のツール呼び出しをサポートします。
mcp.ClientSession を使用して langchain_mcp.MCPToolkit を作成し、
await toolkit.initialize() を実行した後、toolkit.get_tools() を呼び出すと、langchain_core.tools.BaseTool のリストを取得できます。
例:
デモ
Groq の llama-3.1-8b-instant を使用してデモを実行できます:
$ export GROQ_API_KEY=xxx
$ uv run tests/demo.py "Read and summarize the file ./LICENSE"
Secure MCP Filesystem Server running on stdio
Allowed directories: [ '/users/aw/projects/rectalogic/langchain-mcp' ]
The file ./LICENSE is a MIT License agreement. It states that the software is provided "as is" without warranty and that the authors and copyright holders are not liable for any claims, damages, or other liability arising from the software or its use.
コメント
AI アプリケーションが外部のツールやデータソースに統一的な方法で接続するためのオープンなプロトコルです。クライアントはモデルとの対話を担い、MCP サーバーはツール・リソース・プロンプトテンプレートを提供します。標準メッセージでやり取りするため、1 つのサーバーを複数のクライアントで再利用できます。