| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Client\Roots\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\JsonRpc\DTO\JSONRPCNotification; |
| 8 |
use WP\McpSchema\Common\JsonRpc\DTO\NotificationParams; |
| 9 |
use WP\McpSchema\Common\Protocol\Union\ClientNotificationInterface; |
| 10 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 11 |
|
| 12 |
/** |
| 13 |
* A notification from the client to the server, informing it that the list of roots has changed. |
| 14 |
* This notification should be sent whenever the client adds, removes, or modifies any root. |
| 15 |
* The server should then request an updated list of roots using the ListRootsRequest. |
| 16 |
* |
| 17 |
* @since 2024-11-05 |
| 18 |
* @last-updated 2025-11-25 (modified property: params) |
| 19 |
* |
| 20 |
* @mcp-domain Client |
| 21 |
* @mcp-subdomain Roots |
| 22 |
* @mcp-version 2025-11-25 |
| 23 |
*/ |
| 24 |
class RootsListChangedNotification extends JSONRPCNotification implements ClientNotificationInterface |
| 25 |
{ |
| 26 |
use ValidatesRequiredFields; |
| 27 |
|
| 28 |
public const METHOD = 'notifications/roots/list_changed'; |
| 29 |
|
| 30 |
public const DISCRIMINATOR_FIELD = 'method'; |
| 31 |
public const DISCRIMINATOR_VALUE = 'notifications/roots/list_changed'; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var \WP\McpSchema\Common\JsonRpc\DTO\NotificationParams|null |
| 35 |
*/ |
| 36 |
protected ?NotificationParams $typedParams; |
| 37 |
|
| 38 |
/** |
| 39 |
* @param '2.0' $jsonrpc @since 2025-11-25 |
| 40 |
* @param \WP\McpSchema\Common\JsonRpc\DTO\NotificationParams|null $params @since 2024-11-05 |
| 41 |
*/ |
| 42 |
public function __construct( |
| 43 |
string $jsonrpc, |
| 44 |
?NotificationParams $params = null |
| 45 |
) { |
| 46 |
parent::__construct(self::METHOD, $jsonrpc, null); |
| 47 |
$this->typedParams = $params; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Creates an instance from an array. |
| 52 |
* |
| 53 |
* @param array{ |
| 54 |
* jsonrpc: '2.0', |
| 55 |
* method: 'notifications/roots/list_changed', |
| 56 |
* params?: array<string, mixed>|\WP\McpSchema\Common\JsonRpc\DTO\NotificationParams|null |
| 57 |
* } $data |
| 58 |
* @phpstan-param array<string, mixed> $data |
| 59 |
* @return self |
| 60 |
*/ |
| 61 |
public static function fromArray(array $data): self |
| 62 |
{ |
| 63 |
self::assertRequired($data, ['jsonrpc']); |
| 64 |
|
| 65 |
/** @var '2.0' $jsonrpc */ |
| 66 |
$jsonrpc = self::asString($data['jsonrpc']); |
| 67 |
|
| 68 |
/** @var \WP\McpSchema\Common\JsonRpc\DTO\NotificationParams|null $params */ |
| 69 |
$params = isset($data['params']) |
| 70 |
? (is_array($data['params']) |
| 71 |
? NotificationParams::fromArray(self::asArray($data['params'])) |
| 72 |
: $data['params']) |
| 73 |
: null; |
| 74 |
|
| 75 |
return new self( |
| 76 |
$jsonrpc, |
| 77 |
$params |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Converts the instance to an array. |
| 83 |
* |
| 84 |
* @return array<string, mixed> |
| 85 |
*/ |
| 86 |
public function toArray(): array |
| 87 |
{ |
| 88 |
$result = parent::toArray(); |
| 89 |
|
| 90 |
if ($this->typedParams !== null) { |
| 91 |
$result['params'] = $this->typedParams->toArray(); |
| 92 |
} |
| 93 |
|
| 94 |
return $result; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @return \WP\McpSchema\Common\JsonRpc\DTO\NotificationParams|null |
| 99 |
*/ |
| 100 |
public function getTypedParams(): ?NotificationParams |
| 101 |
{ |
| 102 |
return $this->typedParams; |
| 103 |
} |
| 104 |
} |
| 105 |
|