UNPKG

@tb.p/terminai

Version:

MCP (Model Context Protocol) server for secure SSH remote command execution. Enables AI assistants like Claude, Cursor, and VS Code to execute commands on remote servers via SSH with command validation, history tracking, and web-based configuration UI.

63 lines (50 loc) 1.74 kB
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs'; import { resolve, dirname } from 'path'; import { homedir } from 'os'; import { ConfigSchema } from './schema.js'; import { DEFAULT_CONFIG } from './defaults.js'; function expandPath(filePath) { if (filePath.startsWith('~/')) { return resolve(homedir(), filePath.slice(2)); } return resolve(filePath); } export function getConfigPath(customPath) { if (customPath) { return expandPath(customPath); } if (process.env.SSH_MCP_CONFIG) { return expandPath(process.env.SSH_MCP_CONFIG); } const defaultDir = resolve(homedir(), '.config', 'terminai'); return resolve(defaultDir, 'config.json'); } export function loadConfig(customPath) { const configPath = getConfigPath(customPath); if (!existsSync(configPath)) { const configDir = dirname(configPath); if (!existsSync(configDir)) { mkdirSync(configDir, { recursive: true }); } writeFileSync(configPath, JSON.stringify(DEFAULT_CONFIG, null, 2), 'utf-8'); return DEFAULT_CONFIG; } try { const rawConfig = JSON.parse(readFileSync(configPath, 'utf-8')); const validatedConfig = ConfigSchema.parse(rawConfig); return validatedConfig; } catch (error) { throw new Error(`Failed to load config from ${configPath}: ${error.message}`); } } export function saveConfig(config, customPath) { const configPath = getConfigPath(customPath); const validatedConfig = ConfigSchema.parse(config); const configDir = dirname(configPath); if (!existsSync(configDir)) { mkdirSync(configDir, { recursive: true }); } writeFileSync(configPath, JSON.stringify(validatedConfig, null, 2), 'utf-8'); return configPath; } export { expandPath };