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解决方案。