[Client] Feat: Implement MCP client component (#192)

* feat(client): add complete MCP client SDK with STDIO and HTTP transports

Add Client, Builder, ClientProtocol, ClientSession, and transport
implementations for communicating with MCP servers. Supports tools, resources, prompts, and real-time progress/logging notifications.

* refactor(examples): reorganize examples into server/ and client/ folders

* feat: add SamplingRequestHandler for handling server sampling requests

* feat: add setLoggingLevel method to control server logging verbosity

* refactor: make progress token dependent on request ID

* docs: Update server IP in usage instructions and add notes on sampling server requirements.

* feat: add complete() method for completion/complete requests

* refactor: properly parse and type initialize result

* refactor: standardize method names

* fix: revert changes to server related components

* fix: revert changes to server related components

* refactor: Restructure client transports and handlers into dedicated namespaces.

* fix: cs and php-stan errors fixes(partial)

* feat: Refactor request handlers to return `Response` or `Error` objects directly, adding a `SamplingException` and error logging for sampling requests.

* feat: Replace `ClientTransportInterface` with `TransportInterface` and move response result deserialization to client methods.

* refactor: remove redundant timeout check for intialize on transports

The processFiber that executes per tick while fiber is suspended already handles timeouts while waiting

* feat: broaden symfony/http-client dependency to support Symfony 5.4 to 8.0

* Update examples/client/README.md

Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>

* Update examples/client/README.md

Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>

* refactor: remove outgoing message queue from session and simplify transport send method to directly dispatch messages.

* refactor: Replace ClientSession with ClientState for clearer intent on runtime state management

* refactor: rename connectAndInitialize to connect for consistency

* chore: cs fix

* chore: use SamplingCallbackInterface instead of a closure for sampling request handler

---------

Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>
This commit is contained in:
Kyrian Obikwelu 2026-03-10 17:22:40 +01:00 • committed by GitHub
commit 3ef864c444
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

170
src/Client/Builder.php Normal file
View file

@ -0,0 +1,170 @@
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Client;
use Mcp\Client;
use Mcp\Client\Handler\Notification\NotificationHandlerInterface;
use Mcp\Client\Handler\Request\RequestHandlerInterface;
use Mcp\Schema\ClientCapabilities;
use Mcp\Schema\Enum\ProtocolVersion;
use Mcp\Schema\Implementation;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* Fluent builder for creating Client instances.
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
final class Builder
{
private string $name = 'mcp-php-client';
private string $version = '1.0.0';
private ?string $description = null;
private ?ProtocolVersion $protocolVersion = null;
private ?ClientCapabilities $capabilities = null;
private int $initTimeout = 30;
private int $requestTimeout = 120;
private int $maxRetries = 3;
private ?LoggerInterface $logger = null;
/** @var NotificationHandlerInterface[] */
private array $notificationHandlers = [];
/** @var RequestHandlerInterface<mixed>[] */
private array $requestHandlers = [];
/**
* Set the client name and version.
*/
public function setClientInfo(string $name, string $version, ?string $description = null): self
{
$this->name = $name;
$this->version = $version;
$this->description = $description;
return $this;
}
/**
* Set the protocol version to use.
*/
public function setProtocolVersion(ProtocolVersion $protocolVersion): self
{
$this->protocolVersion = $protocolVersion;
return $this;
}
/**
* Set client capabilities.
*/
public function setCapabilities(ClientCapabilities $capabilities): self
{
$this->capabilities = $capabilities;
return $this;
}
/**
* Set initialization timeout in seconds.
*/
public function setInitTimeout(int $seconds): self
{
$this->initTimeout = $seconds;
return $this;
}
/**
* Set request timeout in seconds.
*/
public function setRequestTimeout(int $seconds): self
{
$this->requestTimeout = $seconds;
return $this;
}
/**
* Set maximum retry attempts for failed connections.
*/
public function setMaxRetries(int $retries): self
{
$this->maxRetries = $retries;
return $this;
}
/**
* Set the logger.
*/
public function setLogger(LoggerInterface $logger): self
{
$this->logger = $logger;
return $this;
}
/**
* Add a notification handler for server notifications.
*/
public function addNotificationHandler(NotificationHandlerInterface $handler): self
{
$this->notificationHandlers[] = $handler;
return $this;
}
/**
* Add a request handler for server requests (e.g., sampling).
*
* @param RequestHandlerInterface<mixed> $handler
*/
public function addRequestHandler(RequestHandlerInterface $handler): self
{
$this->requestHandlers[] = $handler;
return $this;
}
/**
* Build the client instance.
*/
public function build(): Client
{
$logger = $this->logger ?? new NullLogger();
$clientInfo = new Implementation(
$this->name,
$this->version,
$this->description,
);
$config = new Configuration(
clientInfo: $clientInfo,
capabilities: $this->capabilities ?? new ClientCapabilities(),
protocolVersion: $this->protocolVersion ?? ProtocolVersion::V2025_06_18,
initTimeout: $this->initTimeout,
requestTimeout: $this->requestTimeout,
maxRetries: $this->maxRetries,
);
$protocol = new Protocol(
requestHandlers: $this->requestHandlers,
notificationHandlers: $this->notificationHandlers,
logger: $logger,
);
return new Client($protocol, $config, $logger);
}
}