add obsidian_put_content tool

This commit is contained in:
Cyprian Zdebski
2025-04-17 22:51:41 +02:00
parent 5cf106e30d
commit 7a8b723485
3 changed files with 57 additions and 0 deletions

View File

@@ -139,6 +139,22 @@ class Obsidian():
return None
return self._safe_call(call_fn)
def put_content(self, filepath: str, content: str) -> Any:
url = f"{self.get_base_url()}/vault/{filepath}"
def call_fn():
response = requests.put(
url,
headers=self._get_headers() | {'Content-Type': 'text/markdown'},
data=content,
verify=self.verify_ssl,
timeout=self.timeout
)
response.raise_for_status()
return None
return self._safe_call(call_fn)
def delete_file(self, filepath: str) -> Any:
"""Delete a file or directory from the vault.

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.PutContentToolHandler())
add_tool_handler(tools.DeleteFileToolHandler())
add_tool_handler(tools.ComplexSearchToolHandler())
add_tool_handler(tools.BatchGetFileContentsToolHandler())

View File

@@ -286,6 +286,46 @@ class PatchContentToolHandler(ToolHandler):
text=f"Successfully patched content in {args['filepath']}"
)
]
class PutContentToolHandler(ToolHandler):
def __init__(self):
super().__init__("obsidian_put_content")
def get_tool_description(self):
return Tool(
name=self.name,
description="Update content of a new or existing file in the vault.",
inputSchema={
"type": "object",
"properties": {
"filepath": {
"type": "string",
"description": "Path to the file (relative to vault root)",
"format": "path"
},
"content": {
"type": "string",
"description": "Content of the file you would like to update"
}
},
"required": ["filepath", "content"]
}
)
def run_tool(self, args: dict) -> Sequence[TextContent | ImageContent | EmbeddedResource]:
if "filepath" not in args or "content" not in args:
raise RuntimeError("filepath and content arguments required")
api = obsidian.Obsidian(api_key=api_key, host=obsidian_host)
api.put_content(args.get("filepath", ""), args["content"])
return [
TextContent(
type="text",
text=f"Successfully updated content in {args['filepath']}"
)
]
class DeleteFileToolHandler(ToolHandler):
def __init__(self):