-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathexample.ts
256 lines (225 loc) · 6.84 KB
/
example.ts
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
// src/types.ts
export interface Tool {
name: string;
description: string;
inputSchema: {
type: string;
properties: Record<string, any>;
required?: string[];
};
handler: (args: any) => Promise<{
content: Array<{
type: string;
text: string;
}>;
}>;
}
export interface ToolProvider {
getTools(): Tool[];
}
// src/tools/note-tools.ts
import { z } from "zod";
import { Tool, ToolProvider } from "../types.js";
import { promises as fs } from "fs";
import path from "path";
const CreateNoteSchema = z.object({
filename: z.string(),
content: z.string(),
folder: z.string().optional()
});
export class NoteTools implements ToolProvider {
constructor(private vaultPath: string) {}
getTools(): Tool[] {
return [
{
name: "create-note",
description: "Create a new note in the vault",
inputSchema: {
type: "object",
properties: {
filename: {
type: "string",
description: "Name of the note (with .md extension)"
},
content: {
type: "string",
description: "Content of the note in markdown format"
},
folder: {
type: "string",
description: "Optional subfolder path"
}
},
required: ["filename", "content"]
},
handler: async (args) => {
const { filename, content, folder } = CreateNoteSchema.parse(args);
const notePath = await this.createNote(filename, content, folder);
return {
content: [
{
type: "text",
text: `Successfully created note: ${notePath}`
}
]
};
}
}
];
}
private async createNote(filename: string, content: string, folder?: string): Promise<string> {
if (!filename.endsWith(".md")) {
filename = `${filename}.md`;
}
const notePath = folder
? path.join(this.vaultPath, folder, filename)
: path.join(this.vaultPath, filename);
const noteDir = path.dirname(notePath);
await fs.mkdir(noteDir, { recursive: true });
try {
await fs.access(notePath);
throw new Error("Note already exists");
} catch (error) {
if (error.code === "ENOENT") {
await fs.writeFile(notePath, content);
return notePath;
}
throw error;
}
}
}
// src/tools/search-tools.ts
import { z } from "zod";
import { Tool, ToolProvider } from "../types.js";
import { promises as fs } from "fs";
import path from "path";
const SearchSchema = z.object({
query: z.string(),
path: z.string().optional(),
caseSensitive: z.boolean().optional()
});
export class SearchTools implements ToolProvider {
constructor(private vaultPath: string) {}
getTools(): Tool[] {
return [
{
name: "search-vault",
description: "Search for text across notes",
inputSchema: {
type: "object",
properties: {
query: {
type: "string",
description: "Search query"
},
path: {
type: "string",
description: "Optional path to limit search scope"
},
caseSensitive: {
type: "boolean",
description: "Whether to perform case-sensitive search"
}
},
required: ["query"]
},
handler: async (args) => {
const { query, path: searchPath, caseSensitive } = SearchSchema.parse(args);
const results = await this.searchVault(query, searchPath, caseSensitive);
return {
content: [
{
type: "text",
text: this.formatSearchResults(results)
}
]
};
}
}
];
}
private async searchVault(query: string, searchPath?: string, caseSensitive = false) {
// Implementation of searchVault method...
}
private formatSearchResults(results: any[]) {
// Implementation of formatSearchResults method...
}
}
// src/server.ts
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { Tool, ToolProvider } from "./types.js";
export class ObsidianServer {
private server: Server;
private tools: Map<string, Tool> = new Map();
constructor() {
this.server = new Server(
{
name: "obsidian-vault",
version: "1.0.0"
},
{
capabilities: {
tools: {}
}
}
);
this.setupHandlers();
}
registerToolProvider(provider: ToolProvider) {
for (const tool of provider.getTools()) {
this.tools.set(tool.name, tool);
}
}
private setupHandlers() {
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: Array.from(this.tools.values()).map(tool => ({
name: tool.name,
description: tool.description,
inputSchema: tool.inputSchema
}))
}));
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
const tool = this.tools.get(name);
if (!tool) {
throw new Error(`Unknown tool: ${name}`);
}
return tool.handler(args);
});
}
async start() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
console.error("Obsidian MCP Server running on stdio");
}
}
// src/main.ts
import { ObsidianServer } from "./server.js";
import { NoteTools } from "./tools/note-tools.js";
import { SearchTools } from "./tools/search-tools.js";
async function main() {
const vaultPath = process.argv[2];
if (!vaultPath) {
console.error("Please provide the path to your Obsidian vault");
process.exit(1);
}
try {
const server = new ObsidianServer();
// Register tool providers
server.registerToolProvider(new NoteTools(vaultPath));
server.registerToolProvider(new SearchTools(vaultPath));
await server.start();
} catch (error) {
console.error("Fatal error:", error);
process.exit(1);
}
}
main().catch((error) => {
console.error("Unhandled error:", error);
process.exit(1);
});