Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_client.py
More file actions
Latest commit
52 lines (42 loc) · 2.23 KB
/
Copy pathexample_client.py
File metadata and controls
52 lines (42 loc) · 2.23 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
47
48
49
50
51
52
importasyncio
importjson
frommcpimportClientSession, StdioServerParameters
frommcp.client.stdioimportstdio_client
asyncdefmain():
params=StdioServerParameters(command="python", args=["run.py"])
try:
asyncwithstdio_client(params) as (read, write):
asyncwithClientSession(read, write) assession:
awaitsession.initialize()
resources=awaitsession.list_resources()
print(f"\nFound {len(resources.resources)} resources:")
forrinresources.resources:
print(f"- {r.uri}")
files_result=awaitsession.read_resource("files://list")
try:
files_json=files_result.contents[0].text
files_data=json.loads(files_json)
print("\nAvailable files:")
if"error"infiles_data:
print(f"Error: {files_data['error']}")
elif"files"infiles_data:
forfileinfiles_data["files"]:
print(f"- {file}")
if"test.txt"infiles_data["files"]:
file_result=awaitsession.read_resource("files://read/some.py")
file_json=file_result.contents[0].text
file_data=json.loads(file_json)
print("\nFile content:")
if"error"infile_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']}")
exceptExceptionase:
print(f"Error parsing response: {e}")
print("Raw response:", files_result)
exceptExceptionase:
print(f"Error: {e}")
if__name__=="__main__":
asyncio.run(main())