11from textwrap import dedent
2- from typing import Optional
2+ from typing import Optional , Literal
33
44from loguru import logger
55from fastmcp import Context
@@ -152,8 +152,9 @@ async def delete_note(
152152 is_directory : bool = False ,
153153 project : Optional [str ] = None ,
154154 workspace : Optional [str ] = None ,
155+ output_format : Literal ["text" , "json" ] = "text" ,
155156 context : Context | None = None ,
156- ) -> bool | str :
157+ ) -> bool | str | dict :
157158 """Delete a note or directory from the knowledge base.
158159
159160 Permanently removes a note or directory from the specified project. For single notes,
@@ -174,6 +175,8 @@ async def delete_note(
174175 (without file extensions). Defaults to False.
175176 project: Project name to delete from. Optional - server will resolve using hierarchy.
176177 If unknown, use list_memory_projects() to discover available projects.
178+ output_format: "text" preserves existing behavior (bool/string). "json"
179+ returns machine-readable deletion metadata.
177180 context: Optional FastMCP context for performance caching.
178181
179182 Returns:
@@ -231,6 +234,15 @@ async def delete_note(
231234 if is_directory :
232235 try :
233236 result = await knowledge_client .delete_directory (identifier )
237+ if output_format == "json" :
238+ return {
239+ "deleted" : result .failed_deletes == 0 ,
240+ "is_directory" : True ,
241+ "identifier" : identifier ,
242+ "total_files" : result .total_files ,
243+ "successful_deletes" : result .successful_deletes ,
244+ "failed_deletes" : result .failed_deletes ,
245+ }
234246
235247 # Build success message for directory delete
236248 result_lines = [
@@ -288,18 +300,41 @@ async def delete_note(
288300```"""
289301
290302 # Handle single note deletes
303+ note_title = None
304+ note_permalink = None
305+ note_file_path = None
291306 try :
292307 # Resolve identifier to entity ID
293308 entity_id = await knowledge_client .resolve_entity (identifier )
309+ if output_format == "json" :
310+ entity = await knowledge_client .get_entity (entity_id )
311+ note_title = entity .title
312+ note_permalink = entity .permalink
313+ note_file_path = entity .file_path
294314 except ToolError as e :
295315 # If entity not found, return False (note doesn't exist)
296316 if "Entity not found" in str (e ) or "not found" in str (e ).lower ():
297317 logger .warning (f"Note not found for deletion: { identifier } " )
318+ if output_format == "json" :
319+ return {
320+ "deleted" : False ,
321+ "title" : None ,
322+ "permalink" : None ,
323+ "file_path" : None ,
324+ }
298325 return False
299326 # For other resolution errors, return formatted error message
300327 logger .error ( # pragma: no cover
301328 f"Delete failed for '{ identifier } ': { e } , project: { active_project .name } "
302329 )
330+ if output_format == "json" :
331+ return {
332+ "deleted" : False ,
333+ "title" : None ,
334+ "permalink" : None ,
335+ "file_path" : None ,
336+ "error" : str (e ),
337+ }
303338 return _format_delete_error_response ( # pragma: no cover
304339 active_project .name , str (e ), identifier
305340 )
@@ -312,14 +347,36 @@ async def delete_note(
312347 logger .info (
313348 f"Successfully deleted note: { identifier } in project: { active_project .name } "
314349 )
350+ if output_format == "json" :
351+ return {
352+ "deleted" : True ,
353+ "title" : note_title ,
354+ "permalink" : note_permalink ,
355+ "file_path" : note_file_path ,
356+ }
315357 return True
316358 else :
317359 logger .warning ( # pragma: no cover
318360 f"Delete operation completed but note was not deleted: { identifier } "
319361 )
362+ if output_format == "json" :
363+ return {
364+ "deleted" : False ,
365+ "title" : note_title ,
366+ "permalink" : note_permalink ,
367+ "file_path" : note_file_path ,
368+ }
320369 return False # pragma: no cover
321370
322371 except Exception as e : # pragma: no cover
323372 logger .error (f"Delete failed for '{ identifier } ': { e } , project: { active_project .name } " )
373+ if output_format == "json" :
374+ return {
375+ "deleted" : False ,
376+ "title" : note_title ,
377+ "permalink" : note_permalink ,
378+ "file_path" : note_file_path ,
379+ "error" : str (e ),
380+ }
324381 # Return formatted error message for better user experience
325382 return _format_delete_error_response (active_project .name , str (e ), identifier )
0 commit comments