<?php
namespace boru\ai\Mcp;

class NativeTcpMcpConnection implements TcpMcpConnectionInterface
{
    /** @var resource */
    protected $stream;

    public function __construct($stream)
    {
        if (!is_resource($stream)) {
            throw new \InvalidArgumentException('TCP MCP connection requires a stream resource');
        }

        $this->stream = $stream;
    }

    public function writeLine($line)
    {
        fwrite($this->stream, rtrim((string)$line, "\r\n") . "\n");
    }

    public function readLine()
    {
        return fgets($this->stream);
    }

    public function close()
    {
        if (is_resource($this->stream)) {
            fclose($this->stream);
        }
    }
}
