Spaces:
Runtime error
Runtime error
Create mcp_task_client.py
Browse files- mcp_task_client.py +36 -0
mcp_task_client.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from mcp import ClientSession, StdioServerParameters
|
| 2 |
+
from mcp.client.stdio import stdio_client
|
| 3 |
+
import json, asyncio
|
| 4 |
+
|
| 5 |
+
async def run():
|
| 6 |
+
# Connect to the task server
|
| 7 |
+
async with stdio_client(StdioServerParameters(command="python", args=["mcp_task_server.py"])) as (read, write):
|
| 8 |
+
async with ClientSession(read, write) as session:
|
| 9 |
+
await session.initialize()
|
| 10 |
+
|
| 11 |
+
# Get task description suggestion using prompt
|
| 12 |
+
prompt_result = await session.get_prompt("task_description", {"task_title": "Do shopping"})
|
| 13 |
+
print(f"\nSuggested description: {prompt_result.messages[0].content.text}")
|
| 14 |
+
|
| 15 |
+
# Display all tasks
|
| 16 |
+
response = await session.read_resource("tasks://list")
|
| 17 |
+
tasks = json.loads(json.loads(response.contents[0].text)['contents'][0]['text'])
|
| 18 |
+
for task in tasks['tasks']:
|
| 19 |
+
print(f"• {task['title']}: {task['description']}")
|
| 20 |
+
|
| 21 |
+
# Add a new task
|
| 22 |
+
await session.call_tool("add_task", {
|
| 23 |
+
"params": {
|
| 24 |
+
"task_title": "Order food",
|
| 25 |
+
"description": "Order lunch from the local restaurant"
|
| 26 |
+
}
|
| 27 |
+
})
|
| 28 |
+
|
| 29 |
+
# Display again all tasks
|
| 30 |
+
response = await session.read_resource("tasks://list")
|
| 31 |
+
tasks = json.loads(json.loads(response.contents[0].text)['contents'][0]['text'])
|
| 32 |
+
for task in tasks['tasks']:
|
| 33 |
+
print(f"• {task['title']}: {task['description']}")
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
asyncio.run(run())
|