UNPKG

roam-research-mcp

Version:
93 lines (92 loc) • 4.29 kB
import * as dotenv from 'dotenv'; import { dirname, join } from 'path'; import { existsSync } from 'fs'; // Get the project root from the script path const scriptPath = process.argv[1]; // Full path to the running script const projectRoot = dirname(dirname(scriptPath)); // Go up two levels from build/index.js // Try to load .env from project root const envPath = join(projectRoot, '.env'); if (existsSync(envPath)) { dotenv.config({ path: envPath }); } // HTTP server configuration const HTTP_STREAM_PORT = process.env.HTTP_STREAM_PORT || '8088'; // Host to bind the HTTP transport to. Only applied in --server (daemon) mode. // Defaults to loopback so a shared server is not exposed beyond this machine. const HTTP_STREAM_HOST = process.env.HTTP_STREAM_HOST || '127.0.0.1'; // Optional transport-level bearer token (authentication). When set, every HTTP // MCP request must send `Authorization: Bearer <token>` (the /health probe stays // open). Unset = open, i.e. unchanged. This is the perimeter lock; it is separate // from ROAM_SYSTEM_WRITE_KEY, which is per-graph write authorization. const HTTP_AUTH_TOKEN = process.env.HTTP_AUTH_TOKEN; const CORS_ORIGINS = (process.env.CORS_ORIGIN || 'http://localhost:5678,https://roamresearch.com') .split(',') .map(origin => origin.trim()); // Re-export for backwards compatibility with single-graph mode // These are still used by CLI commands and for validation const API_TOKEN = process.env.ROAM_API_TOKEN; const GRAPH_NAME = process.env.ROAM_GRAPH_NAME; // Multi-graph mode configuration const ROAM_GRAPHS = process.env.ROAM_GRAPHS; const ROAM_DEFAULT_GRAPH = process.env.ROAM_DEFAULT_GRAPH; /** * Check if we're in multi-graph mode */ export function isMultiGraphMode() { return !!ROAM_GRAPHS; } /** * Validate that either multi-graph or single-graph configuration is provided * Called during server/CLI initialization */ export function validateEnvironment() { if (ROAM_GRAPHS) { // Multi-graph mode if (!ROAM_DEFAULT_GRAPH) { throw new Error('ROAM_DEFAULT_GRAPH is required when using ROAM_GRAPHS.\n' + 'Set it to the key of the graph to use by default.'); } // Validate JSON try { JSON.parse(ROAM_GRAPHS); } catch (e) { throw new Error(`Invalid JSON in ROAM_GRAPHS: ${e.message}`); } } else { // Single-graph mode - require legacy env vars if (!API_TOKEN || !GRAPH_NAME) { const missingVars = []; if (!API_TOKEN) missingVars.push('ROAM_API_TOKEN'); if (!GRAPH_NAME) missingVars.push('ROAM_GRAPH_NAME'); throw new Error(`Missing required environment variables: ${missingVars.join(', ')}\n\n` + 'Please configure these variables either:\n' + '1. In your MCP settings file:\n' + ' - For Cline: ~/Library/Application Support/Code/User/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json\n' + ' - For Claude: ~/Library/Application Support/Claude/claude_desktop_config.json\n\n' + ' Example configuration:\n' + ' {\n' + ' "mcpServers": {\n' + ' "roam-research": {\n' + ' "command": "node",\n' + ' "args": ["/path/to/roam-research-mcp/build/index.js"],\n' + ' "env": {\n' + ' "ROAM_API_TOKEN": "your-api-token",\n' + ' "ROAM_GRAPH_NAME": "your-graph-name"\n' + ' }\n' + ' }\n' + ' }\n' + ' }\n\n' + '2. Or in a .env file in the roam-research directory:\n' + ' ROAM_API_TOKEN=your-api-token\n' + ' ROAM_GRAPH_NAME=your-graph-name\n\n' + '3. Or use multi-graph mode:\n' + ' ROAM_GRAPHS=\'{"personal": {"token": "...", "graph": "..."}}\'\n' + ' ROAM_DEFAULT_GRAPH=personal'); } } } export { API_TOKEN, GRAPH_NAME, HTTP_STREAM_PORT, HTTP_STREAM_HOST, HTTP_AUTH_TOKEN, CORS_ORIGINS, ROAM_GRAPHS, ROAM_DEFAULT_GRAPH };