forked from Shubhamsaboo/awesome-llm-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotion_mcp_agent.py
More file actions
122 lines (104 loc) · 4.68 KB
/
notion_mcp_agent.py
File metadata and controls
122 lines (104 loc) · 4.68 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
122
import asyncio
import json
import os
import sys
import uuid
from textwrap import dedent
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.mcp import MCPTools
from agno.memory.v2 import Memory
from mcp import StdioServerParameters
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
NOTION_TOKEN = os.getenv("NOTION_API_KEY")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
async def main():
print("\n========================================")
print(" Notion MCP Terminal Agent")
print("========================================\n")
# Get configuration from environment or use defaults
notion_token = NOTION_TOKEN
openai_api_key = OPENAI_API_KEY
# Prompt for page ID first
if len(sys.argv) > 1:
# Use command-line argument if provided
page_id = sys.argv[1]
print(f"Using provided page ID from command line: {page_id}")
else:
# Ask the user for the page ID
print("Please enter your Notion page ID:")
print("(You can find this in your page URL, e.g., https://www.notion.so/workspace/Your-Page-1f5b8a8ba283...)")
print("The ID is the part after the last dash and before any query parameters")
user_input = input("> ")
# If user input is empty, use default
if user_input.strip():
page_id = user_input.strip()
print(f"Using provided page ID: {page_id}")
else:
print(f"Using default page ID: {page_id}")
# Generate unique user and session IDs for this terminal session
user_id = f"user_{uuid.uuid4().hex[:8]}"
session_id = f"session_{uuid.uuid4().hex[:8]}"
print(f"User ID: {user_id}")
print(f"Session ID: {session_id}")
print("\nConnecting to Notion MCP server...\n")
# Configure the MCP Tools
server_params = StdioServerParameters(
command="npx",
args=["-y", "@notionhq/notion-mcp-server"],
env={
"OPENAPI_MCP_HEADERS": json.dumps(
{"Authorization": f"Bearer {notion_token}", "Notion-Version": "2022-06-28"}
)
}
)
# Start the MCP Tools session
async with MCPTools(server_params=server_params) as mcp_tools:
print("Connected to Notion MCP server successfully!")
# Create the agent
agent = Agent(
name="NotionDocsAgent",
model=OpenAIChat(id="gpt-4o", api_key=openai_api_key),
tools=[mcp_tools],
description="Agent to query and modify Notion docs via MCP",
instructions=dedent(f"""
You are an expert Notion assistant that helps users interact with their Notion pages.
IMPORTANT INSTRUCTIONS:
1. You have direct access to Notion documents through MCP tools - make full use of them.
2. ALWAYS use the page ID: {page_id} for all operations unless the user explicitly provides another ID.
3. When asked to update, read, or search pages, ALWAYS use the appropriate MCP tool calls.
4. Be proactive in suggesting actions users can take with their Notion documents.
5. When making changes, explain what you did and confirm the changes were made.
6. If a tool call fails, explain the issue and suggest alternatives.
Example tasks you can help with:
- Reading page content
- Searching for specific information
- Adding new content or updating existing content
- Creating lists, tables, and other Notion blocks
- Explaining page structure
- Adding comments to specific blocks
The user's current page ID is: {page_id}
"""),
markdown=True,
show_tool_calls=True,
retries=3,
memory=Memory(), # Use Memory v2 for better multi-session support
add_history_to_messages=True, # Include conversation history
num_history_runs=5, # Keep track of the last 5 interactions
)
print("\n\nNotion MCP Agent is ready! Start chatting with your Notion pages.\n")
print("Type 'exit' or 'quit' to end the conversation.\n")
# Start interactive CLI session with memory and proper session management
await agent.acli_app(
user_id=user_id,
session_id=session_id,
user="You",
emoji="🤖",
stream=True,
markdown=True,
exit_on=["exit", "quit", "bye", "goodbye"]
)
if __name__ == "__main__":
asyncio.run(main())