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 框架之間的 gap。通過這個适配器,開發者可以:
隨著 AI 代理技術的快速發展,工具集成和互操作性將變得越來越重要。LangChain MCP 适配器作為連接不同 AI 工具生態系統的橋樑,將在未來的 AI 應用開發中發揮關鍵作用。它不僅簡化了當前的開發流程,更為構建更加智能、功能豐富的 AI 代理應用奠定了堅實的基礎。
無論您是 AI 應用開發者、企業技術決策者,還是研究人員,這個項目都值得深入了解和應用。它代表了 AI 工具集成領域的最佳實踐,將幫助您構建更加強大和靈活的 AI 解決方案。