-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlangchain_client.py
More file actions
27 lines (20 loc) · 962 Bytes
/
langchain_client.py
File metadata and controls
27 lines (20 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""
LangChain MultiServer MCP Client
1-1 connection between client and MCP server, inside LangChain we have multiple MCP clients to easily connect to multiple MCP servers.
"""
import asyncio
from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
load_dotenv()
llm = ChatOpenAI()
async def main():
async with MultiServerMCPClient({"math":{"command": "python", "args": ["/Users/junfanzhu/Desktop/MCP-AI-Infra-Real-Time-Agent/servers/math_server.py"]},
"weather":{"url":"http://localhost:8000/sse", "transport":"sse"}
}) as client:
agent = create_react_agent(llm, client.get_tools())
result = await agent.ainvoke({"message":"what's 1+1?"})
print(result["message"][-1].content)
if __name__ == "__main__":
asyncio.run(main())