php-sdk/src/Schema/Implementation.php
Christopher Hertel c8ed07bba2
[Server] Handle malformed JSON-RPC input graceful (#412)
* Never let a throwable escape Protocol::processInput()

* Validate payload types in the schema hydration methods

* Stop leaking handler exception messages to the client
2026-08-14 22:34:01 +02:00

109 lines
3.1 KiB
PHP

<?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\Schema;
use Mcp\Exception\InvalidArgumentException;
/**
* Describes the name and version of an MCP implementation.
*
* @phpstan-import-type IconData from Icon
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
class Implementation implements \JsonSerializable
{
/**
* @param ?Icon[] $icons
*/
public function __construct(
public readonly string $name = 'app',
public readonly string $version = 'dev',
public readonly ?string $description = null,
public readonly ?array $icons = null,
public readonly ?string $websiteUrl = null,
) {
}
/**
* @param array{
* name: string,
* version: string,
* description?: string,
* icons?: IconData[],
* websiteUrl?: string,
* } $data
*/
public static function fromArray(array $data): self
{
if (!isset($data['name']) || !\is_string($data['name']) || '' === $data['name']) {
throw new InvalidArgumentException('Invalid or missing "name" in Implementation data.');
}
if (!isset($data['version']) || !\is_string($data['version']) || '' === $data['version']) {
throw new InvalidArgumentException('Invalid or missing "version" in Implementation data.');
}
if (isset($data['icons'])) {
if (!\is_array($data['icons'])) {
throw new InvalidArgumentException('Invalid "icons" in Implementation data; expected an array.');
}
$data['icons'] = Icon::listFromArray($data['icons'], 'Implementation');
}
if (isset($data['description']) && !\is_string($data['description'])) {
throw new InvalidArgumentException('Invalid "description" in Implementation data.');
}
if (isset($data['websiteUrl']) && !\is_string($data['websiteUrl'])) {
throw new InvalidArgumentException('Invalid "websiteUrl" in Implementation data.');
}
return new self(
$data['name'],
$data['version'],
$data['description'] ?? null,
$data['icons'] ?? null,
$data['websiteUrl'] ?? null,
);
}
/**
* @return array{
* name: string,
* version: string,
* description?: string,
* icons?: Icon[],
* websiteUrl?: string,
* }
*/
public function jsonSerialize(): array
{
$data = [
'name' => $this->name,
'version' => $this->version,
];
if (null !== $this->description) {
$data['description'] = $this->description;
}
if (null !== $this->icons) {
$data['icons'] = $this->icons;
}
if (null !== $this->websiteUrl) {
$data['websiteUrl'] = $this->websiteUrl;
}
return $data;
}
}