#!/usr/bin/env node
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 { GitVaultProvider } from './vault/git.js';
import { BaseTool } from './tools/base.tool.js';
import { config } from './config.js';
// Import all tools
import { ReadNoteTool } from './tools/read-note.js';
import { WriteNoteTool } from './tools/write-note.js';
import { SearchNotesTool } from './tools/search-notes.js';
import { ListNotesTool } from './tools/list-notes.js';
/**
* Main MCP Server for Obsidian Vault Integration
*/
class ObsidianMCPServer {
private server: Server;
private vault: GitVaultProvider;
private tools: Map<string, BaseTool>;
constructor() {
// Initialize MCP server
this.server = new Server(
{
name: 'obsidian-mcp-server',
version: '1.0.0',
},
{
capabilities: {
tools: {},
},
}
);
// Initialize vault provider
this.vault = new GitVaultProvider('./vault');
// Initialize tool registry
this.tools = new Map();
this.registerTools();
this.setupHandlers();
}
/**
* Register all available tools
* To add a new tool: just import it and add to this array!
*/
private registerTools(): void {
const toolInstances: BaseTool[] = [
new ReadNoteTool(),
new WriteNoteTool(),
new SearchNotesTool(),
new ListNotesTool(),
// π Add your custom tools here! They'll be automatically registered.
];
// Inject vault provider and register each tool
for (const tool of toolInstances) {
tool.setVault(this.vault);
this.tools.set(tool.name, tool);
}
console.log(`β
Registered ${this.tools.size} tools`);
}
/**
* Set up MCP protocol handlers
*/
private setupHandlers(): void {
// Handle tool listing
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: Array.from(this.tools.values()).map(tool => tool.toMCPTool()),
};
});
// Handle tool execution
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const toolName = request.params.name;
const tool = this.tools.get(toolName);
if (!tool) {
throw new Error(`Unknown tool: ${toolName}`);
}
try {
console.log(`π§ Executing tool: ${toolName}`);
const result = await tool.execute(request.params.arguments ?? {});
return {
content: [
{
type: 'text',
text: JSON.stringify(result, null, 2),
},
],
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
console.error(`β Tool execution failed: ${errorMessage}`);
return {
content: [
{
type: 'text',
text: JSON.stringify({ success: false, error: errorMessage }, null, 2),
},
],
isError: true,
};
}
});
}
/**
* Start the server
*/
async start(): Promise<void> {
console.log('π Starting Obsidian MCP Server...');
console.log(`π Environment: ${config.nodeEnv}`);
console.log(`π¦ Git Repository: ${config.gitRepoUrl}`);
// Initialize vault
await this.vault.initialize();
// Start stdio transport (for Claude Desktop)
const transport = new StdioServerTransport();
await this.server.connect(transport);
console.log('β
Server started successfully');
console.log('π‘ Listening for MCP requests via stdio...');
}
}
// Run the server
const server = new ObsidianMCPServer();
server.start().catch((error) => {
console.error('β Fatal error:', error);
process.exit(1);
});