Files
crm.clientright.ru/aiassist/mcp_proxy.php

104 lines
3.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* MCP Proxy - прокси для взаимодействия с MCP сервером Typebot
*/
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => '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()]);
}
?>