محولات LangChain MCP هي مكتبة تغليف خفيفة الوزن تهدف إلى دمج أدوات بروتوكول محتوى Anthropic (MCP) بسلاسة مع نظامي LangChain و LangGraph. يعالج هذا المشروع مشكلات التوافق بين أطر عمل أدوات الذكاء الاصطناعي المختلفة، مما يسمح للمطورين باستخدام أدوات MCP مباشرة في بيئات LangChain/LangGraph، لبناء تطبيقات وكيل ذكاء اصطناعي أكثر قوة ومرونة.
عنوان المشروع: https://github.com/langchain-ai/langchain-mcp-adapters
تطبيق LangChain/LangGraph
↓
محولات LangChain MCP
↓
تنفيذ عميل MCP
↓
خوادم MCP متعددة (الرياضيات، الطقس، إلخ)
# التثبيت الأساسي
pip install langchain-mcp-adapters
# بيئة تطوير كاملة
pip install langchain-mcp-adapters langgraph langchain-openai
# تعيين مفتاح واجهة برمجة تطبيقات OpenAI
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 هو بنية تحتية مهمة في نظام أدوات الذكاء الاصطناعي البيئي، وقد نجح في سد الفجوة بين بروتوكول MCP وإطار عمل LangChain. من خلال هذا المحول، يمكن للمطورين:
مع التطور السريع لتقنيات وكيل الذكاء الاصطناعي، سيصبح تكامل الأدوات وقابليتها للتشغيل البيني أكثر أهمية. ستلعب محولات LangChain MCP، باعتبارها جسرًا يربط بين أنظمة أدوات الذكاء الاصطناعي البيئية المختلفة، دورًا رئيسيًا في تطوير تطبيقات الذكاء الاصطناعي المستقبلية. فهي لا تبسط عمليات التطوير الحالية فحسب، بل تضع أيضًا أساسًا متينًا لبناء تطبيقات وكيل ذكاء اصطناعي أكثر ذكاءً وغنية بالميزات.
سواء كنت مطور تطبيقات ذكاء اصطناعي، أو صانع قرار تقني في مؤسسة، أو باحثًا، فإن هذا المشروع يستحق الدراسة والتطبيق المتعمقين. إنه يمثل أفضل الممارسات في مجال تكامل أدوات الذكاء الاصطناعي، وسيساعدك على بناء حلول ذكاء اصطناعي أكثر قوة ومرونة.