Merge pull request #27 from thomato/add-delete-function

Implement 'Delete file' functionality
This commit is contained in:
Markus Pfundstein
2025-04-01 21:32:22 +02:00
committed by GitHub
4 changed files with 63 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ The server implements multiple tools to interact with Obsidian:
- search: Search for documents matching a specified text query across all files in the vault
- patch_content: Insert content into an existing note relative to a heading, block reference, or frontmatter field.
- append_content: Append content to a new or existing file in the vault.
- delete_file: Delete a file or directory from your vault.
### Example prompts

View File

@@ -140,6 +140,24 @@ class Obsidian():
return self._safe_call(call_fn)
def delete_file(self, filepath: str) -> Any:
"""Delete a file or directory from the vault.
Args:
filepath: Path to the file to delete (relative to vault root)
Returns:
None on success
"""
url = f"{self.get_base_url()}/vault/{filepath}"
def call_fn():
response = requests.delete(url, headers=self._get_headers(), verify=self.verify_ssl, timeout=self.timeout)
response.raise_for_status()
return None
return self._safe_call(call_fn)
def search_json(self, query: dict) -> Any:
url = f"{self.get_base_url()}/search/"

View File

@@ -47,6 +47,7 @@ add_tool_handler(tools.GetFileContentsToolHandler())
add_tool_handler(tools.SearchToolHandler())
add_tool_handler(tools.PatchContentToolHandler())
add_tool_handler(tools.AppendContentToolHandler())
add_tool_handler(tools.DeleteFileToolHandler())
add_tool_handler(tools.ComplexSearchToolHandler())
add_tool_handler(tools.BatchGetFileContentsToolHandler())
add_tool_handler(tools.PeriodicNotesToolHandler())

View File

@@ -286,6 +286,49 @@ class PatchContentToolHandler(ToolHandler):
text=f"Successfully patched content in {args['filepath']}"
)
]
class DeleteFileToolHandler(ToolHandler):
def __init__(self):
super().__init__("obsidian_delete_file")
def get_tool_description(self):
return Tool(
name=self.name,
description="Delete a file or directory from the vault.",
inputSchema={
"type": "object",
"properties": {
"filepath": {
"type": "string",
"description": "Path to the file or directory to delete (relative to vault root)",
"format": "path"
},
"confirm": {
"type": "boolean",
"description": "Confirmation to delete the file (must be true)",
"default": False
}
},
"required": ["filepath", "confirm"]
}
)
def run_tool(self, args: dict) -> Sequence[TextContent | ImageContent | EmbeddedResource]:
if "filepath" not in args:
raise RuntimeError("filepath argument missing in arguments")
if not args.get("confirm", False):
raise RuntimeError("confirm must be set to true to delete a file")
api = obsidian.Obsidian(api_key=api_key)
api.delete_file(args["filepath"])
return [
TextContent(
type="text",
text=f"Successfully deleted {args['filepath']}"
)
]
class ComplexSearchToolHandler(ToolHandler):
def __init__(self):