<?php

/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

namespace app\mcp;

use app\mcp\McpService;
use support\Log;

class process
{
    public function __construct()
    {
    }

    public function onWorkerStart()
    {
        try {
            $config = config('mcp');
            $transport = $config['transport'] ?? 'sse';
            $host = $config['host'] ?? '127.0.0.1';
            $port = (int)($config['port'] ?? 8080);
            $path = $config['path'] ?? 'mcp';

            $service = new McpService();

            switch ($transport) {
                case 'stdio':
                    Log::channel('mcp')->info('Starting MCP with STDIO transport');
                    $service->startWithStdio();
                    break;
                case 'http':
                    Log::channel('mcp')->info("Starting MCP with HTTP transport at http://{$host}:{$port}/{$path}");
                    $service->startWithHttp($host, $port, $path);
                    break;
                case 'sse':
                default:
                    Log::channel('mcp')->info("Starting MCP with SSE transport at http://{$host}:{$port}/{$path}");
                    $service->startWithSse($host, $port, $path);
                    break;
            }
        } catch (\Throwable $e) {
            Log::channel('mcp')->error('MCP process start failed: ' . $e->getMessage(), [
                'file' => $e->getFile(),
                'line' => $e->getLine(),
            ]);
        }
    }
}
