Spaces:
Runtime error
Runtime error
File size: 1,819 Bytes
bd157ce 833c9cc bd157ce bb00a5e bd157ce bb00a5e bd157ce 833c9cc bb00a5e bd157ce bb00a5e 833c9cc bd157ce bb00a5e bd157ce |
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 |
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
import json, asyncio
async def run():
# Connect to the task server
async with stdio_client(StdioServerParameters(command="python", args=["mcp_task_server.py"])) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
# Get task description suggestion using prompt
print("Getting prompt...")
prompt_result = await session.get_prompt("task_description", {"task_title": "Do shopping"})
print(f"Suggested description: {prompt_result.messages[0].content.text}")
# Display all tasks
print("\nDisplaying all tasks...")
response = await session.read_resource("tasks://list")
tasks = json.loads(json.loads(response.contents[0].text)['contents'][0]['text'])
for task in tasks['tasks']:
print(f"• {task['title']}: {task['description']}")
print("\nAdding new task....")
# Add a new task
await session.call_tool("add_task", {
"params": {
"task_title": "Order food",
"description": "Order lunch from the local restaurant"
}
})
print("Task added!")
print("\nDisplaying all tasks again...")
# Display again all tasks
response = await session.read_resource("tasks://list")
tasks = json.loads(json.loads(response.contents[0].text)['contents'][0]['text'])
for task in tasks['tasks']:
print(f"• {task['title']}: {task['description']}")
if __name__ == "__main__":
asyncio.run(run()) |