11from mcp .server .fastmcp import FastMCP
22from notion_client import AsyncClient
33import os
4- from typing import List
54
6- token = "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 "
5+ token = "ntn_token "
76mcp = FastMCP ("notion_bd_server" )
87brain = AsyncClient (auth = token )
98
9+ # The server has tools for doing CRUD operations on the Brain Dump database
10+
1011
1112@mcp .tool ()
1213async def add_task (task : str ):
13- """Add a task to the Notion database and returns the new page ID"""
14+ """Add a task to the Notion database and return the new page ID"""
1415 dump_db = "1d784ade96ac80a3b7ecf54f3eae5f49"
1516 newpage = {
1617 "TaskTitle" : {"title" : [{"text" : {"content" : task }}]},
@@ -58,6 +59,37 @@ async def get_all_database_items():
5859 return text_conv
5960
6061
62+ @mcp .resource ("notion://dumpdb/{task_text}" )
63+ async def search_db (task_text : str ) -> str :
64+ """Get the filtered task from the Notion database and returns the data as text"""
65+ dump_db = "1d784ade96ac80a3b7ecf54f3eae5f49"
66+ search_results = await brain .databases .query (
67+ database_id = dump_db ,
68+ filter = {
69+ "property" : "TaskTitle" ,
70+ "title" : {
71+ "equals" : task_text ,
72+ },
73+ },
74+ )
75+ results = search_results .get ("results" , [])
76+ text_conv = ""
77+ for result in results :
78+ page_id = result ["id" ]
79+ created_time = result ["created_time" ]
80+ last_edited_time = result ["last_edited_time" ]
81+ # getting the properties now
82+ task_title = result ["properties" ]["TaskTitle" ]["title" ][0 ]["text" ]["content" ]
83+ task_status = result ["properties" ]["TaskStatus" ]["select" ]
84+ area = result ["properties" ]["Area" ]["rich_text" ]
85+ resource = result ["properties" ]["Resource" ]["rich_text" ]
86+ due_date = result ["properties" ]["DueDate" ]["date" ]
87+ url = result ["url" ]
88+ text_conv += f"Task: { task_title } \t Status: { task_status } \t Area: { area } \t Resource: { resource } \t Due Date: { due_date } \t URL: { url } \t Page ID: { page_id } \t Last Edited Time: { last_edited_time } \t "
89+ # return results[0]["text"]
90+ return text_conv
91+
92+
6193@mcp .tool ()
6294async def list_task ():
6395 """Lists the tasks in the Notion database as text"""
@@ -85,8 +117,8 @@ async def remove_task(task_text: str):
85117
86118
87119@mcp .tool ()
88- async def add_resource ( task_text : str , resource_text : str ):
89- """Search for the task from the Notion database and add resource to it """
120+ async def change_title ( task_title : str , new_title : str ):
121+ """Change title of the task """
90122
91123 dump_db = "1d784ade96ac80a3b7ecf54f3eae5f49"
92124
@@ -95,7 +127,7 @@ async def add_resource(task_text: str, resource_text: str):
95127 filter = {
96128 "property" : "TaskTitle" ,
97129 "title" : {
98- "equals" : task_text ,
130+ "equals" : task_title ,
99131 },
100132 },
101133 )
@@ -104,104 +136,51 @@ async def add_resource(task_text: str, resource_text: str):
104136 page_id = results [0 ]["id" ]
105137 prop = {}
106138
107- prop ["Resource " ] = {
108- "type" : "rich_text " ,
109- "rich_text " : [{"text" : {"content" : resource_text }}],
139+ prop ["TaskTitle " ] = {
140+ "type" : "title " ,
141+ "title " : [{"text" : {"content" : new_title }}],
110142 }
111143
112144 updt = await brain .pages .update (page_id = page_id , properties = prop )
113- return f"Updated the resource to { updt ['id' ]} "
145+ return f"Updated the title of the page { updt ['id' ]} "
114146
115147
116148@mcp .tool ()
117- async def add_area ( task_text : str , area_text : str ):
118- """Search for the task from the Notion database and add area to it """
119-
149+ async def add_page_content ( task_title : str , task_data : str ):
150+ """Append the task data into the page of given task title """
151+ # First find the task and get its page id
120152 dump_db = "1d784ade96ac80a3b7ecf54f3eae5f49"
121153
122154 search_results = await brain .databases .query (
123155 database_id = dump_db ,
124156 filter = {
125157 "property" : "TaskTitle" ,
126158 "title" : {
127- "equals" : task_text ,
159+ "equals" : task_title ,
128160 },
129161 },
130162 )
131163 results = search_results .get ("results" , [])
132-
133164 page_id = results [0 ]["id" ]
134- prop = {}
135-
136- if area_text :
137- prop ["Area" ] = {
138- "type" : "rich_text" ,
139- "rich_text" : [{"text" : {"content" : area_text }}],
140- }
141-
142- updt = await brain .pages .update (page_id = page_id , properties = prop )
143- return f"Updated the area to { updt ['id' ]} "
144-
145-
146- @mcp .prompt ()
147- def task_steps (task_text : str ) -> str :
148- """Prompt to get the steps to complete the tasks"""
149- return f"Provide the step by approach required to complete the { task_text } "
150-
151-
152- @mcp .prompt ()
153- def task_analysis (tasks : str ) -> str :
154- """Prompt to analyse the tasks"""
155- return f"Analyse the { tasks } and provide the steps to complete the task"
156-
157-
158- @mcp .resource ("notion://dumpdb/{task_text}" )
159- async def search_db (task_text : str ) -> str :
160- """Get the filtered task from the Notion database and returns the data as text"""
161- dump_db = "1d784ade96ac80a3b7ecf54f3eae5f49"
162- search_results = await brain .databases .query (
163- database_id = dump_db ,
164- filter = {
165- "property" : "TaskTitle" ,
166- "title" : {
167- "contains" : task_text ,
168- },
169- },
170- )
171- results = search_results .get ("results" , [])
172- text_conv = ""
173- for result in results :
174- page_id = result ["id" ]
175- created_time = result ["created_time" ]
176- last_edited_time = result ["last_edited_time" ]
177- # getting the properties now
178- task_title = result ["properties" ]["TaskTitle" ]["title" ][0 ]["text" ]["content" ]
179- task_status = result ["properties" ]["TaskStatus" ]["select" ]
180- area = result ["properties" ]["Area" ]["rich_text" ]
181- resource = result ["properties" ]["Resource" ]["rich_text" ]
182- due_date = result ["properties" ]["DueDate" ]["date" ]
183- url = result ["url" ]
184- text_conv += f"Task: { task_title } \t Status: { task_status } \t Area: { area } \t Resource: { resource } \t Due Date: { due_date } \t URL: { url } \t Page ID: { page_id } \t Last Edited Time: { last_edited_time } \t "
185- # return results[0]["text"]
186- return text_conv
187165
166+ # Then update the data into the page using append method
167+ page_data = {
168+ "object" : "block" ,
169+ "type" : "paragraph" ,
170+ "paragraph" : {"rich_text" : [{"text" : {"content" : task_data }}]},
171+ }
188172
189- # @mcp.tool()
190- async def analyse_tasks (task_kw : str ) -> str :
191- """Searches the database for the task, and gets the steps to complete the task"""
192- task_info = await mcp .read_resource (f"notion://dumpdb/{ task_kw } " )
193- assembled_prompt = await mcp .get_prompt (
194- "task_analysis" ,
195- arguments = {
196- "tasks" : task_info [0 ].content ,
197- },
173+ await brain .blocks .children .append (
174+ block_id = page_id ,
175+ children = [page_data ],
198176 )
199- return assembled_prompt .messages [0 ].content .text
177+ # Inform the LLM that the work has been done
178+ return f"The content has been added to { page_id } "
200179
201180
202181@mcp .tool ()
203- async def update_task (task_title : str , task_data : str ):
204- """Update the task in the database with given task title"""
182+ async def read_page_content (task_title : str ):
183+ """Append the task data into the page of given task title"""
205184 # First find the task and get its page id
206185 dump_db = "1d784ade96ac80a3b7ecf54f3eae5f49"
207186
@@ -216,23 +195,14 @@ async def update_task(task_title: str, task_data: str):
216195 )
217196 results = search_results .get ("results" , [])
218197 page_id = results [0 ]["id" ]
219-
220- # Then update the data into the page using append method
221- page_data = {
222- "object" : "block" ,
223- "type" : "paragraph" ,
224- "paragraph" : {"rich_text" : [{"text" : {"content" : task_data }}]},
225- }
226-
227- await brain .blocks .children .append (
228- block_id = page_id ,
229- children = [page_data ],
230- )
231- # Inform the LLM that the work has been done
232- return f"The page with { page_id } has been updated"
198+ # getting the page blocks
199+ page_blocks = await brain .blocks .children .list (block_id = page_id )
200+ page_text = ""
201+ for block in page_blocks ["results" ]:
202+ page_text += block ["paragraph" ]["rich_text" ][0 ]["text" ]["content" ]
203+ return f"The content of the page is: { page_text } "
233204
234205
235- # "TaskTitle": {"id": "title", "type": "title", "title": [{"type": "text", "text": {"content": "test", "link": null}, "annotations": {"bold": false, "italic": false, "strikethrough": false, "underline": false, "code": false, "color": "default"}, "plain_text": "test", "href": null}] [ "properties": {"TaskStatus": {"id": "IJax", "type": "select", "select": null}, "Area": {"id": "c%3C%60%7C", "type": "rich_text", "rich_text": []}, "DueDate": {"id": "ce%3DU", "type": "date", "date": null}, "Resource": {"id": "kUxZ", "type": "rich_text", "rich_text": []}, }}, "url": "https://www.notion.so/test-1d784ade96ac81a38f7bdd1eccaa13c4", "public_url": null}]',]
236206if __name__ == "__main__" :
237207 print ("Starting MCP server..." )
238208 mcp .run ()
0 commit comments