UNPKG

@light-merlin-dark/vssh

Version:

MCP-native SSH proxy for AI agents. CLI & MCP Server, plugin system, AI safety guards.

154 lines 5.44 kB
#!/usr/bin/env node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.MCPClient = void 0; exports.callMCP = callMCP; const child_process_1 = require("child_process"); const readline = __importStar(require("readline")); class MCPClient { constructor(command, args = []) { this.command = command; this.args = args; this.rl = null; this.responseHandlers = new Map(); this.nextId = 1; } async connect() { return new Promise((resolve, reject) => { this.process = (0, child_process_1.spawn)(this.command, this.args, { stdio: ['pipe', 'pipe', 'pipe'] }); this.rl = readline.createInterface({ input: this.process.stdout, crlfDelay: Infinity }); this.rl.on('line', (line) => { try { const response = JSON.parse(line); const handler = this.responseHandlers.get(response.id); if (handler) { handler(response); this.responseHandlers.delete(response.id); } } catch (error) { console.error('Failed to parse MCP response:', line); } }); this.process.on('error', reject); this.process.on('exit', (code) => { if (code !== 0) { reject(new Error(`MCP server exited with code ${code}`)); } }); // Give the process a moment to start setTimeout(resolve, 100); }); } async call(method, params = {}) { const id = this.nextId++; const request = { jsonrpc: '2.0', id, method, params }; return new Promise((resolve, reject) => { this.responseHandlers.set(id, (response) => { if (response.error) { reject(new Error(`MCP Error: ${response.error.message}`)); } else { resolve(response.result); } }); this.process.stdin.write(JSON.stringify(request) + '\n'); }); } async close() { if (this.rl) { this.rl.close(); } if (this.process) { this.process.kill(); } } } exports.MCPClient = MCPClient; // Convenience function for one-off calls async function callMCP(serverPath, method, params = {}, serverArgs = []) { const client = new MCPClient(serverPath, serverArgs); try { await client.connect(); // Initialize if not already initialized if (method !== 'initialize') { await client.call('initialize', { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'vssh-call-mcp', version: '1.0.0' } }); } const result = await client.call(method, params); return result; } finally { await client.close(); } } // CLI interface when run directly if (require.main === module) { const args = process.argv.slice(2); if (args.length < 2) { console.error('Usage: call-mcp <server-path> <method> [params-json]'); console.error('Example: call-mcp ./dist/src/mcp-server.js tools/list'); console.error('Example: call-mcp ./dist/src/mcp-server.js tools/call \'{"name":"run_command","arguments":{"command":"ls"}}\''); process.exit(1); } const [serverPath, method, paramsJson] = args; const params = paramsJson ? JSON.parse(paramsJson) : {}; callMCP(serverPath, method, params) .then(result => { console.log(JSON.stringify(result, null, 2)); }) .catch(error => { console.error('Error:', error.message); process.exit(1); }); } //# sourceMappingURL=call-mcp.js.map