-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
121 lines (101 loc) · 3.65 KB
/
agent.py
File metadata and controls
121 lines (101 loc) · 3.65 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
import json
from datetime import datetime
from dotenv import load_dotenv
from groq import Groq
from rich.console import Console
from rich.panel import Panel
from rich.prompt import Prompt
from mcp_client import MCP_TOOLS, dispatch_tool
load_dotenv()
groq = Groq(api_key=os.environ["GROQ_API_KEY"])
console = Console()
SYSTEM_PROMPT = """You are NotionMind Agent — an AI assistant with access to the
user's Notion workspace via MCP tools.
You have these tools:
- mcp_search_notes: search notes by keyword
- mcp_create_note: create a new note
- mcp_list_all_notes: list all notes (limit must be an integer, default 20)
Rules:
- Always summarise tool results in plain English — never show raw JSON to the user
- For questions about recent work, use mcp_list_all_notes
- For specific topic searches, use mcp_search_notes
- Be concise and friendly
Today's date is: """ + datetime.now().strftime("%Y-%m-%d")
def run_agent(user_message: str):
"""Run the MCP agent with tool calling loop"""
console.print(f"\n[dim]Agent thinking...[/]")
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_message}
]
# agentic loop — keeps running until no more tool calls
while True:
response = groq.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=messages,
tools=MCP_TOOLS,
tool_choice="auto",
max_tokens=1000
)
message = response.choices[0].message
# no tool calls — final answer
if not message.tool_calls:
console.print(Panel(
f"[bold white]{message.content}[/]",
title="[cyan]NotionMind Agent[/]"
))
break
# process tool calls
messages.append({
"role": "assistant",
"content": message.content or "",
"tool_calls": [
{
"id": tc.id,
"type": "function",
"function": {
"name": tc.function.name,
"arguments": tc.function.arguments
}
}
for tc in message.tool_calls
]
})
for tc in message.tool_calls:
tool_name = tc.function.name
tool_args = json.loads(tc.function.arguments)
console.print(f"[dim]→ calling tool: [cyan]{tool_name}[/] with {tool_args}[/]")
result = dispatch_tool(tool_name, tool_args)
messages.append({
"role": "tool",
"tool_call_id": tc.id,
"content": json.dumps(result)
})
def interactive_agent():
console.print(Panel(
"[bold cyan]NotionMind Agent[/] — MCP-powered Notion AI\n\n"
"[dim]Ask me anything about your notes.\n"
"Examples:\n"
" 'what did I work on this week?'\n"
" 'create a note about my meeting today'\n"
" 'show me all notes tagged Python'\n"
" 'summarise everything I did in March'\n\n"
"Type 'quit' to exit[/]",
title="Welcome"
))
while True:
mode = Prompt.ask("\n[bold cyan]Input mode[/]", choices=["text", "voice"], default="text")
if mode == "voice":
from voice import listen
user_input = listen()
if not user_input:
continue
else:
user_input = Prompt.ask("[bold cyan]You[/]")
if user_input.lower() == "quit":
console.print("[dim]Goodbye![/]")
break
run_agent(user_input)
if __name__ == "__main__":
interactive_agent()