UNPKG

@vibe-kit/grok-cli

Version:

An open-source AI agent that brings the power of Grok directly into your terminal.

94 lines 3.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StdioTransport = void 0; const child_process_1 = require("child_process"); const transport_interface_1 = require("./transport-interface"); class StdioTransport extends transport_interface_1.McpTransport { constructor(options) { super(options); this.process = null; this.buffer = ''; this.command = options.command; this.args = options.args || []; } async connect() { return new Promise((resolve, reject) => { try { // Spawn the MCP server process this.process = (0, child_process_1.spawn)(this.command, this.args, { stdio: ['pipe', 'pipe', 'pipe'], env: { ...process.env, ...this.options.env } }); if (!this.process.stdout || !this.process.stdin || !this.process.stderr) { throw new Error('Failed to create process streams'); } // Handle stdout (JSON-RPC messages) this.process.stdout.on('data', (data) => { this.handleData(data.toString()); }); // Handle stderr (debug/error logs) this.process.stderr.on('data', (data) => { // Only show stderr in debug mode, headless mode, or when not in quiet mode if (!this.options.quiet && (process.env.MCP_DEBUG || process.argv.includes('--prompt'))) { console.warn(`MCP Server stderr: ${data.toString()}`); } }); // Handle process exit this.process.on('exit', (code) => { this.connected = false; this.emit('disconnect', code); }); // Handle process errors this.process.on('error', (error) => { this.connected = false; this.emit('error', error); reject(error); }); // Connection is established when process starts this.connected = true; this.emit('connect'); resolve(); } catch (error) { reject(error); } }); } handleData(data) { this.buffer += data; // Process complete JSON-RPC messages const lines = this.buffer.split('\n'); this.buffer = lines.pop() || ''; // Keep incomplete line in buffer for (const line of lines) { const trimmed = line.trim(); if (trimmed) { try { const message = JSON.parse(trimmed); this.emit('message', message); } catch (error) { console.warn('Failed to parse JSON-RPC message:', trimmed, error); } } } } async send(message) { if (!this.process || !this.process.stdin || !this.connected) { throw new Error('Stdio transport not connected'); } const messageJson = JSON.stringify(message) + '\n'; this.process.stdin.write(messageJson); } async disconnect() { if (this.process) { // Kill the process this.process.kill(); this.process = null; } this.connected = false; this.buffer = ''; this.emit('disconnect'); } } exports.StdioTransport = StdioTransport; //# sourceMappingURL=stdio-transport.js.map