-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_stdio.py
More file actions
33 lines (26 loc) · 1.05 KB
/
client_stdio.py
File metadata and controls
33 lines (26 loc) · 1.05 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
import sys
import asyncio
import nest_asyncio
import os
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
nest_asyncio.apply()
async def main():
# Get the directory where client_stdio.py is located
current_dir = os.path.dirname(os.path.abspath(__file__))
server_path = os.path.join(current_dir, "server_stdio.py")
server_params = StdioServerParameters(
command="python",
args=[server_path]
)
async with stdio_client(server_params) as (read_stream, write_stream):
async with ClientSession(read_stream,write_stream) as session:
await session.initialize()
tools_result = await session.list_tools()
print("Available Tools: ")
for tool in tools_result.tools:
print(f" -{tool.name}: {tool.description}")
result = await session.call_tool("add", arguments = {"num_1": 2, "num_2":7})
print("Result of add tool: ", result.content[0].text)
if __name__ == "__main__":
asyncio.run(main())