'Method not allowed']); exit(); } $input = file_get_contents('php://input'); $data = json_decode($input, true); if (!$data) { http_response_code(400); echo json_encode(['error' => 'Invalid JSON data']); exit(); } $method = $data['method'] ?? ''; $params = $data['params'] ?? []; error_log("MCP Proxy: Method={$method}, Params=" . json_encode($params)); try { // Формируем JSON-RPC запрос $jsonRpcRequest = [ 'jsonrpc' => '2.0', 'id' => 1, 'method' => $method, 'params' => $params ]; // Команда для запуска MCP сервера $mcpCommand = 'ssh dev@5.129.228.142 "cd /home/dev/mcp-typebot && TYPEBOT_TOKEN=iTTDoxKe0pUwNJ7pNUOEEaO2 TYPEBOT_WORKSPACE_ID=cmfhzzsec0000qj1c8apfigkj TYPEBOT_API_URL=https://typebot.klientprav.tech npx ts-node src/index.ts"'; // Создаем временный файл для запроса $requestFile = tempnam(sys_get_temp_dir(), 'mcp_request_'); file_put_contents($requestFile, json_encode($jsonRpcRequest) . "\n"); // Выполняем команду с таймаутом $descriptorspec = [ 0 => ['file', $requestFile, 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w'] ]; $process = proc_open($mcpCommand, $descriptorspec, $pipes); if (!is_resource($process)) { throw new Exception('Не удалось запустить MCP процесс'); } // Читаем ответ $output = stream_get_contents($pipes[1]); $error = stream_get_contents($pipes[2]); // Закрываем процесс fclose($pipes[1]); fclose($pipes[2]); $returnValue = proc_close($process); // Удаляем временный файл unlink($requestFile); if ($returnValue !== 0) { error_log("MCP Proxy: Process error (code {$returnValue}): " . $error); http_response_code(500); echo json_encode(['success' => false, 'error' => 'MCP Process Error', 'details' => $error]); exit(); } // Парсим ответ $response = json_decode($output, true); if (!$response) { error_log("MCP Proxy: Invalid JSON response: " . $output); http_response_code(500); echo json_encode(['success' => false, 'error' => 'Invalid JSON response', 'raw_output' => $output]); exit(); } echo json_encode(['success' => true, 'data' => $response, 'timestamp' => date('Y-m-d H:i:s')]); } catch (Exception $e) { error_log("MCP Proxy: Exception: " . $e->getMessage()); http_response_code(500); echo json_encode(['success' => false, 'error' => 'Internal Server Error', 'details' => $e->getMessage()]); } ?>