forked from robwilde/obsidian-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobsidian_mcp_server.py
More file actions
279 lines (228 loc) · 9.5 KB
/
obsidian_mcp_server.py
File metadata and controls
279 lines (228 loc) · 9.5 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python3
"""
MCP Server for saving Claude Code responses to Obsidian
"""
import asyncio
import json
import os
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List
from urllib.parse import unquote, urlparse, parse_qs
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import Tool
def get_project_config() -> Dict[str, Any]:
"""Get project-specific Obsidian configuration from .claude/obsidian.json"""
try:
# Look for .claude/obsidian.json in current directory and parents
current_path = Path.cwd()
for path in [current_path] + list(current_path.parents):
config_file = path / ".claude" / "obsidian.json"
if config_file.exists():
with open(config_file, "r") as f:
return json.load(f)
# Default configuration if no project config found
return {
"folder": "Claude Code",
"templates": {
"report": "# {title}\n\n**Generated:** {timestamp}\n**Project:** {project}\n\n{content}",
"review": "# Code Review: {title}\n\n**Generated:** {timestamp}\n**Project:** {project}\n\n{content}",
"note": "# {title}\n\n**Created:** {timestamp}\n**Project:** {project}\n\n{content}"
}
}
except Exception:
# Fallback to default
return {
"folder": "Claude Code",
"templates": {
"report": "# {title}\n\n**Generated:** {timestamp}\n**Project:** {project}\n\n{content}",
"review": "# Code Review: {title}\n\n**Generated:** {timestamp}\n**Project:** {project}\n\n{content}",
"note": "# {title}\n\n**Created:** {timestamp}\n**Project:** {project}\n\n{content}"
}
}
def get_vault_path() -> Path:
"""Get Obsidian vault path from environment or config"""
vault_path = os.getenv("OBSIDIAN_VAULT_PATH")
if not vault_path:
vault_path = os.path.expanduser("~/Documents/ObsidianVault")
path = Path(vault_path)
if not path.exists():
path.mkdir(parents=True, exist_ok=True)
return path
def generate_frontmatter(tags: List[str]) -> str:
"""Generate YAML frontmatter for Obsidian note"""
timestamp = datetime.now().isoformat()
frontmatter = "---\n"
frontmatter += f"created: {timestamp}\n"
frontmatter += f"source: claude-code\n"
frontmatter += f"tags: {tags}\n"
frontmatter += "---\n\n"
return frontmatter
# Create the server
server = Server("obsidian-claude-code")
@server.list_tools()
async def handle_list_tools():
"""List available tools."""
return [
Tool(
name="save_to_obsidian",
description="Save content to Obsidian using project configuration. Use this when Claude Code should save reports, reviews, or other content to the user's Obsidian vault.",
inputSchema={
"type": "object",
"properties": {
"content": {
"type": "string",
"description": "The content to save"
},
"title": {
"type": "string",
"description": "Title for the document"
},
"type": {
"type": "string",
"enum": ["report", "review", "note"],
"description": "Type of content (determines template)",
"default": "note"
},
"tags": {
"type": "array",
"items": {"type": "string"},
"description": "Additional tags for the note",
"default": []
}
},
"required": ["content", "title"]
}
),
Tool(
name="read_obsidian_url",
description="Read a file from Obsidian using an Obsidian URL format (e.g., obsidian://open?vault=VaultName&file=path/to/file). Handles URL-encoded file paths with spaces and special characters.",
inputSchema={
"type": "object",
"properties": {
"url": {
"type": "string",
"description": "The Obsidian URL (e.g., obsidian://open?vault=MyVault&file=Notes%2FMy%20Note)"
}
},
"required": ["url"]
}
)
]
@server.call_tool()
async def handle_call_tool(name: str, arguments: dict):
"""Handle tool calls."""
if name == "save_to_obsidian":
return await save_to_obsidian(
arguments.get("content", ""),
arguments.get("title", "Untitled"),
arguments.get("type", "note"),
arguments.get("tags", [])
)
elif name == "read_obsidian_url":
return await read_obsidian_url(
arguments.get("url", "")
)
else:
raise ValueError(f"Unknown tool: {name}")
async def read_obsidian_url(url: str):
"""Read a file from Obsidian using an Obsidian URL"""
try:
# Parse the Obsidian URL
parsed = urlparse(url)
# Check if it's an Obsidian URL
if parsed.scheme != "obsidian":
raise ValueError(f"Not an Obsidian URL: {url}")
# Parse query parameters
params = parse_qs(parsed.query)
# Get vault name and file path
vault_name = params.get("vault", [""])[0]
file_path = params.get("file", [""])[0]
if not file_path:
raise ValueError("No file path specified in URL")
# URL decode the file path to handle spaces and special characters
file_path = unquote(file_path)
# Get the vault path from environment
vault_path = get_vault_path()
# Check if the vault name matches (optional - for validation)
# In practice, we'll use the configured vault path regardless
# Construct the full path to the file
# Replace forward slashes with OS-appropriate separators
file_path_parts = file_path.split('/')
full_path = vault_path
for part in file_path_parts:
full_path = full_path / part
# Add .md extension if not present
if not full_path.suffix:
full_path = full_path.with_suffix('.md')
# Check if file exists
if not full_path.exists():
raise FileNotFoundError(f"File not found: {full_path}")
# Read the file content
with open(full_path, "r", encoding="utf-8") as f:
content = f.read()
return [
{
"type": "text",
"text": f"Successfully read file from Obsidian\nVault: {vault_name}\nFile: {file_path}\nPath: {full_path}\n\n---\n\n{content}"
}
]
except Exception as e:
raise Exception(f"Failed to read Obsidian URL: {str(e)}")
async def save_to_obsidian(content: str, title: str, content_type: str, tags: List[str]):
"""Save content to Obsidian using project configuration and templates"""
try:
# Get configuration and paths
config = get_project_config()
vault_path = get_vault_path()
project_name = Path.cwd().name
# Prepare template variables
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Select template based on content type
template = config.get("templates", {}).get(content_type, "{content}")
# Format content using template
formatted_content = template.format(
title=title,
timestamp=timestamp,
project=project_name,
content=content
)
# Create filename (sanitize title for filesystem)
safe_title = "".join(c for c in title if c.isalnum() or c in (' ', '-', '_')).rstrip()
timestamp_prefix = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{timestamp_prefix}_{safe_title}.md"
# Determine target folder
project_folder = config.get("folder", "Claude Code")
target_path = vault_path / project_folder
# Ensure target folder exists
target_path.mkdir(parents=True, exist_ok=True)
# Create full file path
file_path = target_path / filename
# Combine all tags
all_tags = ["claude-code", content_type, project_name.lower()] + tags
# Generate frontmatter
frontmatter = generate_frontmatter(all_tags)
# Combine frontmatter and formatted content
full_content = frontmatter + formatted_content
# Write file
with open(file_path, "w", encoding="utf-8") as f:
f.write(full_content)
return [
{
"type": "text",
"text": f"Successfully saved {content_type} '{title}' to {file_path}\nProject: {project_name}\nFolder: {project_folder}"
}
]
except Exception as e:
raise Exception(f"Failed to save to Obsidian: {str(e)}")
async def main():
"""Main entry point for the MCP server"""
async with stdio_server() as streams:
await server.run(
streams[0],
streams[1],
server.create_initialization_options()
)
if __name__ == "__main__":
asyncio.run(main())