|
| 1 | +# src/server/mcp_hub/todoist/test_client.py |
| 2 | +import json |
| 3 | +from qwen_agent.agents import Assistant |
| 4 | + |
| 5 | +# --- Configuration --- |
| 6 | +llm_cfg = { |
| 7 | + 'model': 'qwen3:4b', |
| 8 | + 'model_server': 'http://localhost:11434/v1/', |
| 9 | + 'api_key': 'EMPTY', |
| 10 | +} |
| 11 | + |
| 12 | +# Todoist MCP Server Configuration |
| 13 | +mcp_server_url = "http://127.0.0.1:9021/sse" |
| 14 | + |
| 15 | +# IMPORTANT: Replace with a valid User ID from your MongoDB that has Todoist credentials |
| 16 | +USER_ID = "google-oauth2|115437244827618197332" |
| 17 | + |
| 18 | +# --- Agent Setup --- |
| 19 | +tools = [{ |
| 20 | + "mcpServers": { |
| 21 | + "todoist_server": { |
| 22 | + "url": mcp_server_url, |
| 23 | + "headers": {"X-User-ID": USER_ID}, |
| 24 | + } |
| 25 | + } |
| 26 | +}] |
| 27 | + |
| 28 | +print("Initializing Qwen agent for Todoist...") |
| 29 | +agent = Assistant( |
| 30 | + llm=llm_cfg, |
| 31 | + function_list=tools, |
| 32 | + name="TodoistAgent", |
| 33 | + description="An agent that can manage Todoist tasks.", |
| 34 | + system_message="You are a helpful Todoist assistant. Use the available tools to manage projects and tasks." |
| 35 | +) |
| 36 | + |
| 37 | +# --- Interactive Chat Loop --- |
| 38 | +def run_agent_interaction(): |
| 39 | + print("\n--- Todoist Agent Ready ---") |
| 40 | + print("You can now manage your Todoist tasks.") |
| 41 | + print("Type 'quit' or 'exit' to end the session.") |
| 42 | + print("\nExample commands:") |
| 43 | + print(" - list my projects") |
| 44 | + print(" - what are my tasks for today?") |
| 45 | + print(" - add a task 'Buy milk' to my Inbox project") |
| 46 | + print(" - complete the task with ID '...'") |
| 47 | + print("-" * 25) |
| 48 | + |
| 49 | + messages = [] |
| 50 | + while True: |
| 51 | + try: |
| 52 | + print("\nYou: ", end="") |
| 53 | + user_input = input() |
| 54 | + if user_input.lower() in ["quit", "exit", "q"]: |
| 55 | + print("\n👋 Goodbye!") |
| 56 | + break |
| 57 | + |
| 58 | + messages.append({'role': 'user', 'content': user_input}) |
| 59 | + print("\nAgent: ", end="", flush=True) |
| 60 | + |
| 61 | + last_assistant_text = "" |
| 62 | + final_response_from_run = None |
| 63 | + for response in agent.run(messages=messages): |
| 64 | + if isinstance(response, list) and response and response[-1].get("role") == "assistant": |
| 65 | + current_text = response[-1].get("content", "") |
| 66 | + if isinstance(current_text, str): |
| 67 | + delta = current_text[len(last_assistant_text):] |
| 68 | + print(delta, end="", flush=True) |
| 69 | + last_assistant_text = current_text |
| 70 | + final_response_from_run = response |
| 71 | + |
| 72 | + print() |
| 73 | + if final_response_from_run: |
| 74 | + messages = final_response_from_run |
| 75 | + else: |
| 76 | + print("I could not process that request.") |
| 77 | + messages.pop() |
| 78 | + |
| 79 | + except KeyboardInterrupt: |
| 80 | + print("\n👋 Goodbye!") |
| 81 | + break |
| 82 | + except Exception as e: |
| 83 | + print(f"\nAn error occurred: {e}") |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + run_agent_interaction() |
| 87 | + |
0 commit comments