#!/usr/bin/env node
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import express from 'express';
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';
/**
* HTTP-based MCP Server for remote access (mobile, web)
*/
class ObsidianHTTPServer {
private app: express.Application;
private mcpServers: Map<string, Server>;
private vault: GitVaultProvider;
private tools: Map<string, BaseTool>;
constructor() {
this.app = express();
this.mcpServers = new Map();
this.vault = new GitVaultProvider('./vault');
this.tools = new Map();
this.registerTools();
this.setupRoutes();
}
private registerTools(): void {
const toolInstances: BaseTool[] = [
new ReadNoteTool(),
new WriteNoteTool(),
new SearchNotesTool(),
new ListNotesTool(),
];
for (const tool of toolInstances) {
tool.setVault(this.vault);
this.tools.set(tool.name, tool);
}
console.log(`β
Registered ${this.tools.size} tools`);
}
private createMCPServer(): Server {
const server = new Server(
{
name: 'obsidian-mcp-server',
version: '1.0.0',
},
{
capabilities: {
tools: {},
},
}
);
// Handle tool listing
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: Array.from(this.tools.values()).map(tool => tool.toMCPTool()),
};
});
// Handle tool execution
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,
};
}
});
return server;
}
private setupRoutes(): void {
// Health check
this.app.get('/health', (req, res) => {
res.json({ status: 'ok', server: 'obsidian-mcp-server' });
});
// SSE endpoint for MCP connection
this.app.get('/sse', async (req, res) => {
console.log('π± New SSE connection established');
// Authentication check
const apiKey = req.query.apiKey || req.headers['x-api-key'];
if (config.apiKey && apiKey !== config.apiKey) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
const sessionId = Math.random().toString(36).substring(7);
const transport = new SSEServerTransport('/message', res);
const mcpServer = this.createMCPServer();
this.mcpServers.set(sessionId, mcpServer);
await mcpServer.connect(transport);
res.on('close', () => {
console.log('π± SSE connection closed');
this.mcpServers.delete(sessionId);
});
});
// Message endpoint for client requests
this.app.post('/message', express.json(), async (req, res) => {
// Authentication check
const apiKey = req.query.apiKey || req.headers['x-api-key'];
if (config.apiKey && apiKey !== config.apiKey) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
// Forward to appropriate server
// Note: In production, you'd want more sophisticated session management
console.log('π¨ Received message:', req.body);
res.json({ acknowledged: true });
});
}
async start(): Promise<void> {
console.log('π Starting Obsidian MCP HTTP Server...');
console.log(`π Environment: ${config.nodeEnv}`);
console.log(`π¦ Git Repository: ${config.gitRepoUrl}`);
// Initialize vault
await this.vault.initialize();
// Start HTTP server
const port = config.port;
this.app.listen(port, () => {
console.log(`β
HTTP Server listening on port ${port}`);
console.log(`π‘ SSE endpoint: http://localhost:${port}/sse`);
console.log(`π₯ Health check: http://localhost:${port}/health`);
if (config.apiKey) {
console.log('π API Key authentication enabled');
} else {
console.warn('β οΈ Warning: No API key configured - server is open to all requests!');
}
});
}
}
// Run the server
const server = new ObsidianHTTPServer();
server.start().catch((error) => {
console.error('β Fatal error:', error);
process.exit(1);
});