LangChain MCPアダプターは、Anthropicモデルコンテンツプロトコル(MCP)ツールをLangChainおよびLangGraphエコシステムとシームレスに統合することを目的とした軽量なラッパーライブラリです。このプロジェクトは、異なるAIツールフレームワーク間の互換性の問題を解決し、開発者がLangChain/LangGraph環境でMCPツールを直接使用して、より強力で柔軟なAIエージェントアプリケーションを構築できるようにします。
プロジェクトアドレス: https://github.com/langchain-ai/langchain-mcp-adapters
LangChain/LangGraph Application
↓
LangChain MCP Adapters
↓
MCP Client Implementation
↓
Multiple MCP Servers (Math, Weather, etc.)
# 基本インストール
pip install langchain-mcp-adapters
# 完全な開発環境
pip install langchain-mcp-adapters langgraph langchain-openai
# OpenAI APIキーの設定
export OPENAI_API_KEY=<your_api_key>
# math_server.py
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Math")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b
@mcp.tool()
def multiply(a: int, b: int) -> int:
"""Multiply two numbers"""
return a * b
if __name__ == "__main__":
mcp.run(transport="stdio")
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from langchain_mcp_adapters.tools import load_mcp_tools
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
# モデルの初期化
model = ChatOpenAI(model="gpt-4o")
# サーバーパラメータの設定
server_params = StdioServerParameters(
command="python",
args=["/path/to/math_server.py"],
)
# エージェントの作成と実行
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
# MCPツールのロード
tools = await load_mcp_tools(session)
# エージェントの作成
agent = create_react_agent(model, tools)
# クエリの実行
response = await agent.ainvoke({
"messages": "what's (3 + 5) x 12?"
})
# weather_server.py
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Weather")
@mcp.tool()
async def get_weather(location: str) -> str:
"""Get weather for location."""
return f"It's always sunny in {location}"
if __name__ == "__main__":
mcp.run(transport="sse")
from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4o")
# マルチサーバー構成
async with MultiServerMCPClient({
"math": {
"command": "python",
"args": ["/path/to/math_server.py"],
"transport": "stdio",
},
"weather": {
"url": "http://localhost:8000/sse",
"transport": "sse",
}
}) as client:
# エージェントの作成
agent = create_react_agent(model, client.get_tools())
# 数学演算
math_response = await agent.ainvoke({
"messages": "what's (3 + 5) x 12?"
})
# 天気クエリ
weather_response = await agent.ainvoke({
"messages": "what is the weather in NYC?"
})
# graph.py
from contextlib import asynccontextmanager
from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent
from langchain_anthropic import ChatAnthropic
model = ChatAnthropic(model="claude-3-5-sonnet-latest")
@asynccontextmanager
async def make_graph():
async with MultiServerMCPClient({
"math": {
"command": "python",
"args": ["/path/to/math_server.py"],
"transport": "stdio",
},
"weather": {
"url": "http://localhost:8000/sse",
"transport": "sse",
}
}) as client:
agent = create_react_agent(model, client.get_tools())
yield agent
{
"dependencies": ["."],
"graphs": {
"agent": "./graph.py:make_graph"
}
}
LangChain MCPアダプタープロジェクトは、AIツールエコシステムにおける重要なインフラストラクチャであり、MCPプロトコルとLangChainフレームワーク間のギャップをうまく埋めました。このアダプターを通じて、開発者は次のことが可能になります。
AIエージェント技術の急速な発展に伴い、ツール統合と相互運用性がますます重要になります。LangChain MCPアダプターは、異なるAIツールエコシステムを接続するブリッジとして、将来のAIアプリケーション開発において重要な役割を果たすでしょう。現在の開発プロセスを簡素化するだけでなく、よりインテリジェントで機能豊富なAIエージェントアプリケーションを構築するための強固な基盤を築きます。
AIアプリケーション開発者、企業の技術意思決定者、研究者のいずれであっても、このプロジェクトは深く理解し、応用する価値があります。これはAIツール統合分野におけるベストプラクティスを表しており、より強力で柔軟なAIソリューションの構築に役立ちます。