node-server #10
@@ -6,6 +6,7 @@
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"server": "node dist/http-server.js",
|
||||
"validate-manifest": "mcpb validate manifest.json",
|
||||
"pack": "npm run build && mcpb pack",
|
||||
"test": "jest",
|
||||
|
||||
114
src/http-server.ts
Normal file
114
src/http-server.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* HTTP Entry Point for Obsidian MCP Server
|
||||
* Exposes the MCP implementation over Streamable HTTP transport
|
||||
* per the MCP 2025-11-25 specification.
|
||||
*
|
||||
* Endpoint: POST/GET /mcp
|
||||
* Port: MCP_PORT env var (default: 3000)
|
||||
*/
|
||||
|
||||
import http, { IncomingMessage } from 'node:http';
|
||||
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
||||
import { ObsidianMCPServer } from './server.js';
|
||||
import { registerAllTools } from './tools/index.js';
|
||||
import { logger } from './utils/logger.js';
|
||||
|
||||
const PORT = parseInt(process.env.MCP_PORT ?? '3000', 10);
|
||||
|
||||
/**
|
||||
* Read and parse the JSON body from an incoming HTTP request.
|
||||
*/
|
||||
function readBody(req: IncomingMessage): Promise<unknown> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const chunks: Buffer[] = [];
|
||||
req.on('data', (chunk: Buffer) => chunks.push(chunk));
|
||||
req.on('end', () => {
|
||||
const raw = Buffer.concat(chunks).toString('utf8');
|
||||
if (!raw) {
|
||||
resolve(undefined);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
resolve(JSON.parse(raw));
|
||||
} catch {
|
||||
reject(new Error('Invalid JSON body'));
|
||||
}
|
||||
});
|
||||
req.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const vaultName = process.env.OBSIDIAN_VAULT;
|
||||
if (!vaultName) {
|
||||
logger.error('OBSIDIAN_VAULT environment variable not set');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
logger.info('Starting Obsidian MCP HTTP server', { vault: vaultName, port: PORT });
|
||||
|
||||
// Create and configure the MCP server
|
||||
const mcpServer = new ObsidianMCPServer();
|
||||
await registerAllTools(mcpServer);
|
||||
|
||||
// Stateless transport: no session management, each request is self-contained.
|
||||
// Use sessionIdGenerator: () => randomUUID() for stateful/multi-client mode.
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: undefined,
|
||||
});
|
||||
|
||||
await mcpServer.connect(transport);
|
||||
|
||||
const httpServer = http.createServer(async (req, res) => {
|
||||
// Only serve the /mcp endpoint
|
||||
if (req.url !== '/mcp') {
|
||||
res.writeHead(404, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Not Found. Use POST /mcp' }));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
let body: unknown;
|
||||
if (req.method === 'POST') {
|
||||
body = await readBody(req);
|
||||
}
|
||||
await transport.handleRequest(req, res, body);
|
||||
} catch (error) {
|
||||
logger.error('Error handling MCP request', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
if (!res.headersSent) {
|
||||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Internal server error' }));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
httpServer.listen(PORT, () => {
|
||||
logger.info(`MCP HTTP server ready`, { url: `http://localhost:${PORT}/mcp` });
|
||||
});
|
||||
|
||||
// Graceful shutdown
|
||||
const shutdown = async (signal: string) => {
|
||||
logger.info(`Received ${signal}, shutting down`);
|
||||
httpServer.close(async () => {
|
||||
try {
|
||||
await mcpServer.close();
|
||||
logger.info('Server closed');
|
||||
process.exit(0);
|
||||
} catch {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
process.on('SIGINT', () => shutdown('SIGINT'));
|
||||
process.on('SIGTERM', () => shutdown('SIGTERM'));
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
logger.error('Fatal error', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
||||
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
||||
import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
|
||||
import {
|
||||
CallToolRequestSchema,
|
||||
ListToolsRequestSchema,
|
||||
@@ -119,12 +120,12 @@ export class ObsidianMCPServer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to stdio transport
|
||||
* Connect using the provided transport, or stdio if none is given
|
||||
*/
|
||||
async connect(): Promise<void> {
|
||||
const transport = new StdioServerTransport();
|
||||
await this.server.connect(transport);
|
||||
logger.info('MCP server connected to stdio transport');
|
||||
async connect(transport?: Transport): Promise<void> {
|
||||
const t = transport ?? new StdioServerTransport();
|
||||
await this.server.connect(t);
|
||||
logger.info('MCP server connected', { transport: transport ? 'custom' : 'stdio' });
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user