<?php
namespace boru\ai\Mcp;

class ClientBackedMcpToolInvoker implements McpToolInvokerInterface
{
    /** @var McpServerRegistry */
    protected $serverRegistry;

    /** @var McpClientFactoryInterface */
    protected $clientFactory;

    public function __construct(McpServerRegistry $serverRegistry, McpClientFactoryInterface $clientFactory)
    {
        $this->serverRegistry = $serverRegistry;
        $this->clientFactory = $clientFactory;
    }

    public function invoke($serverName, $toolName, array $arguments)
    {
        $serverConfig = $this->serverRegistry->get($serverName);
        $client = $this->clientFactory->makeClient($serverConfig);
        $client->initialize($serverConfig);

        return $client->callTool($toolName, $arguments);
    }
}
