-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_tools_client.py
More file actions
46 lines (39 loc) · 1.55 KB
/
Copy pathstring_tools_client.py
File metadata and controls
46 lines (39 loc) · 1.55 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Create server parameters for stdio connection
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
import asyncio
from dotenv import load_dotenv
import os
load_dotenv()
model = ChatOpenAI(
model="z-ai/glm-5.2",
api_key=os.getenv("NVIDIA_API_KEY"),
base_url="https://integrate.api.nvidia.com/v1"
)
server_params = StdioServerParameters(
command="python",
# Make sure to update to the full absolute path to your math_server.py file
args=["string_tools_mcp_server.py"],
)
async def run_agent():
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# Initialize the connection
await session.initialize()
# Get tools
tools = await load_mcp_tools(session)
# Create and run the agent
agent = create_react_agent(model, tools)
agent_response_1 = await agent.ainvoke({"messages": "Reverse the string 'HelloWorld!"})
agent_response_2 = await agent.ainvoke({"messages": "How many words are in the 'Arman is an AI Engineer'?"})
for m in agent_response_1['messages']:
m.pretty_print()
print(f"Response 2 {'=' * 40}")
for m in agent_response_2['messages']:
m.pretty_print()
# Run the async function
if __name__ == "__main__":
asyncio.run(run_agent())