-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_client.py
52 lines (42 loc) · 2.23 KB
/
example_client.py
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
47
48
49
50
51
52
import asyncio
import json
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def main():
params = StdioServerParameters(command="python", args=["run.py"])
try:
async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
resources = await session.list_resources()
print(f"\nFound {len(resources.resources)} resources:")
for r in resources.resources:
print(f"- {r.uri}")
files_result = await session.read_resource("files://list")
try:
files_json = files_result.contents[0].text
files_data = json.loads(files_json)
print("\nAvailable files:")
if "error" in files_data:
print(f"Error: {files_data['error']}")
elif "files" in files_data:
for file in files_data["files"]:
print(f"- {file}")
if "test.txt" in files_data["files"]:
file_result = await session.read_resource("files://read/test.txt")
file_json = file_result.contents[0].text
file_data = json.loads(file_json)
print("\nFile content:")
if "error" in file_data:
print(f"Error: {file_data['error']}")
else:
print(f"Content: {file_data['content']}")
print(f"Size: {file_data['metadata']['size']} bytes")
print(f"Modified: {file_data['metadata']['modified']}")
except Exception as e:
print(f"Error parsing response: {e}")
print("Raw response:", files_result)
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
asyncio.run(main())