Adopt Schema component in server handlers

This commit is contained in:
Christopher Hertel 2025-08-03 22:06:46 +02:00
commit 09707b4798
No known key found for this signature in database
GPG key ID: 111F6BFF7B6B5AA3
9 changed files with 1516 additions and 2424 deletions

View file

@ -28,40 +28,37 @@ use Mcp\Schema\JsonRpc\Notification;
class CancelledNotification extends Notification
{
/**
* @param string $requestId The ID of the request that is being cancelled. This MUST correspond to the ID of a request previously issued in the same direction.
* @param ?string $reason An optional string describing the reason for the cancellation. This MAY be logged or presented to the user.
* @param ?array<string, mixed> $_meta additional metadata about the notification
* @param string|int $requestId The ID of the request that is being cancelled. This MUST correspond to the ID of a request previously issued in the same direction.
* @param ?string $reason An optional string describing the reason for the cancellation. This MAY be logged or presented to the user.
*/
public function __construct(
public readonly string $requestId,
public readonly string|int $requestId,
public readonly ?string $reason = null,
public readonly ?array $_meta = null,
) {
$params = [
'requestId' => $this->requestId,
];
}
public static function getMethod(): string
{
return 'notifications/cancelled';
}
protected static function fromParams(?array $params): Notification
{
if (null === $params || !isset($params['requestId']) || (!\is_string($params['requestId']) && !\is_int($params['requestId']))) {
throw new InvalidArgumentException('Invalid or missing "requestId" parameter for "notifications/cancelled" notification.');
}
return new self($params['requestId'], $params['reason'] ?? null);
}
protected function getParams(): ?array
{
$params = ['requestId' => $this->requestId];
if (null !== $this->reason) {
$params['reason'] = $this->reason;
}
if (null !== $_meta) {
$params['_meta'] = $_meta;
}
parent::__construct('notifications/cancelled', $params);
}
public static function fromNotification(Notification $notification): self
{
if ('notifications/cancelled' !== $notification->method) {
throw new InvalidArgumentException('Notification is not a notifications/cancelled notification');
}
$params = $notification->params;
if (!isset($params['requestId']) || !\is_string($params['requestId'])) {
throw new InvalidArgumentException('Missing or invalid requestId parameter for notifications/cancelled notification');
}
return new self($params['requestId'], $params['reason'] ?? null, $params['_meta'] ?? null);
return $params;
}
}

View file

@ -11,7 +11,6 @@
namespace Mcp\Schema\Notification;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\JsonRpc\Notification;
/**
@ -21,27 +20,18 @@ use Mcp\Schema\JsonRpc\Notification;
*/
class InitializedNotification extends Notification
{
/**
* @param array<string, mixed>|null $_meta
*/
public function __construct(
public readonly ?array $_meta = null,
) {
$params = [];
if (null !== $_meta) {
$params['_meta'] = $_meta;
}
parent::__construct('notifications/initialized', $params);
public static function getMethod(): string
{
return 'notifications/initialized';
}
public static function fromNotification(Notification $notification): self
public static function fromParams(?array $params): self
{
if ('notifications/initialized' !== $notification->method) {
throw new InvalidArgumentException('Notification is not a notifications/initialized notification');
}
return new self();
}
return new self($notification->params['_meta'] ?? null);
protected function getParams(): ?array
{
return null;
}
}

View file

@ -21,51 +21,49 @@ use Mcp\Schema\JsonRpc\Notification;
class LoggingMessageNotification extends Notification
{
/**
* @param LoggingLevel $level the severity of this log message
* @param mixed $data The data to be logged, such as a string message or an object. Any JSON serializable type is allowed here.
* @param string $logger an optional name of the logger issuing this message
* @param ?array<string, mixed> $_meta optional metadata to include in the notification
* @param LoggingLevel $level the severity of this log message
* @param mixed $data The data to be logged, such as a string message or an object. Any JSON serializable type is allowed here.
* @param ?string $logger an optional name of the logger issuing this message
*/
public function __construct(
public readonly LoggingLevel $level,
public readonly mixed $data,
public readonly ?string $logger = null,
public readonly ?array $_meta = null,
) {
$params = [
'level' => $level->value,
'data' => \is_string($data) ? $data : json_encode($data),
];
if (null !== $logger) {
$params['logger'] = $logger;
}
if (null !== $_meta) {
$params['_meta'] = $_meta;
}
parent::__construct('notifications/message', $params);
}
public static function fromNotification(Notification $notification): self
public static function getMethod(): string
{
if ('notifications/message' !== $notification->method) {
throw new InvalidArgumentException('Notification is not a notifications/message notification');
}
$params = $notification->params;
return 'notifications/message';
}
protected static function fromParams(?array $params): Notification
{
if (!isset($params['level']) || !\is_string($params['level'])) {
throw new InvalidArgumentException('Missing or invalid level parameter for notifications/message notification');
throw new InvalidArgumentException('Missing or invalid "level" parameter for "notifications/message" notification.');
}
if (!isset($params['data'])) {
throw new InvalidArgumentException('Missing data parameter for notifications/message notification');
throw new InvalidArgumentException('Missing "data" parameter for "notifications/message" notification.');
}
$level = LoggingLevel::from($params['level']);
$data = \is_string($params['data']) ? $params['data'] : json_encode($params['data']);
return new self($level, $params['data'], $params['logger'] ?? null, $params['_meta'] ?? null);
return new self($level, $data, $params['logger'] ?? null);
}
protected function getParams(): ?array
{
$params = [
'level' => $this->level->value,
'data' => $this->data,
];
if (null !== $this->logger) {
$params['logger'] = $this->logger;
}
return $params;
}
}

View file

@ -22,45 +22,59 @@ use Mcp\Schema\JsonRpc\Notification;
class ProgressNotification extends Notification
{
/**
* @param string|int $progressToken the progress token which was given in the initial request, used to
* associate this notification with the request that is proceeding
* @param float $progress The progress thus far. This should increase every time progress is
* made, even if the total is unknown.
* @param ?float $total total number of items to process (or total progress required), if known
* @param ?string $message an optional message describing the current progress
* @param ?array<string, mixed> $_meta optional metadata to include in the notification
* @param string|int $progressToken the progress token which was given in the initial request, used to
* associate this notification with the request that is proceeding
* @param float $progress The progress thus far. This should increase every time progress is
* made, even if the total is unknown.
* @param ?float $total total number of items to process (or total progress required), if known
* @param ?string $message an optional message describing the current progress
*/
public function __construct(
public readonly string|int $progressToken,
public readonly float $progress,
public readonly ?float $total = null,
public readonly ?string $message = null,
public readonly ?array $_meta = null,
) {
$params = [];
if (null !== $_meta) {
$params['_meta'] = $_meta;
}
parent::__construct('notifications/progress', $params);
}
public static function fromNotification(Notification $notification): self
public static function getMethod(): string
{
if ('notifications/progress' !== $notification->method) {
throw new InvalidArgumentException('Notification is not a notifications/progress notification');
}
$params = $notification->params;
return 'notifications/progress';
}
protected static function fromParams(?array $params): Notification
{
if (!isset($params['progressToken']) || !\is_string($params['progressToken'])) {
throw new InvalidArgumentException('Missing or invalid progressToken parameter for notifications/progress notification');
throw new InvalidArgumentException('Missing or invalid "progressToken" parameter for "notifications/progress" notification.');
}
if (!isset($params['progress']) || !\is_float($params['progress'])) {
throw new InvalidArgumentException('Missing or invalid progress parameter for notifications/progress notification');
throw new InvalidArgumentException('Missing or invalid "progress" parameter for "notifications/progress" notification.');
}
return new self($params['progressToken'], $params['progress'], $params['total'] ?? null, $params['message'] ?? null, $params['_meta'] ?? null);
return new self(
$params['progressToken'],
$params['progress'],
$params['total'] ?? null,
$params['message'] ?? null,
);
}
protected function getParams(): ?array
{
$params = [
'progressToken' => $this->progressToken,
'progress' => $this->progress,
];
if (null !== $this->total) {
$params['total'] = $this->total;
}
if (null !== $this->message) {
$params['message'] = $this->message;
}
return $params;
}
}

View file

@ -11,7 +11,6 @@
namespace Mcp\Schema\Notification;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\JsonRpc\Notification;
/**
@ -21,26 +20,18 @@ use Mcp\Schema\JsonRpc\Notification;
*/
class PromptListChangedNotification extends Notification
{
/**
* @param array<string, mixed>|null $_meta
*/
public function __construct(
public readonly ?array $_meta = null,
) {
$params = [];
if (null !== $_meta) {
$params['_meta'] = $_meta;
}
parent::__construct('notifications/prompts/list_changed', $params);
public static function getMethod(): string
{
return 'notifications/prompts/list_changed';
}
public static function fromNotification(Notification $notification): self
protected static function fromParams(?array $params): Notification
{
if ('notifications/prompts/list_changed' !== $notification->method) {
throw new InvalidArgumentException('Notification is not a notifications/prompts/list_changed notification');
}
return new self();
}
return new self($notification->params['_meta'] ?? null);
protected function getParams(): ?array
{
return null;
}
}

View file

@ -11,7 +11,6 @@
namespace Mcp\Schema\Notification;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\JsonRpc\Notification;
/**
@ -21,26 +20,18 @@ use Mcp\Schema\JsonRpc\Notification;
*/
class ResourceListChangedNotification extends Notification
{
/**
* @param array<string, mixed>|null $_meta
*/
public function __construct(
public readonly ?array $_meta = null,
) {
$params = [];
if (null !== $_meta) {
$params['_meta'] = $_meta;
}
parent::__construct('notifications/resources/list_changed', $params);
public static function getMethod(): string
{
return 'notifications/resources/list_changed';
}
public static function fromNotification(Notification $notification): self
protected static function fromParams(?array $params): Notification
{
if ('notifications/resources/list_changed' !== $notification->method) {
throw new InvalidArgumentException('Notification is not a notifications/resources/list_changed notification');
}
return new self();
}
return new self($notification->params['_meta'] ?? null);
protected function getParams(): ?array
{
return null;
}
}

View file

@ -19,33 +19,29 @@ use Mcp\Schema\JsonRpc\Notification;
*/
class ResourceUpdatedNotification extends Notification
{
/**
* @param array<string, mixed>|null $_meta
*/
public function __construct(
public readonly string $uri,
public readonly ?array $_meta = null,
) {
$params = ['uri' => $uri];
if (null !== $_meta) {
$params['_meta'] = $_meta;
}
parent::__construct('notifications/resources/updated', $params);
}
public static function fromNotification(Notification $notification): self
public static function getMethod(): string
{
if ('notifications/resources/updated' !== $notification->method) {
throw new InvalidArgumentException('Notification is not a notifications/resources/updated notification');
return 'notifications/resources/updated';
}
protected static function fromParams(?array $params): Notification
{
if (null === $params || !isset($params['uri']) || !\is_string($params['uri'])) {
throw new InvalidArgumentException('Invalid or missing "uri" parameter for notifications/resources/updated notification.');
}
$params = $notification->params;
return new self($params['uri']);
}
if (!isset($params['uri']) || !\is_string($params['uri'])) {
throw new InvalidArgumentException('Missing or invalid uri parameter for notifications/resources/updated notification');
}
return new self($params['uri'], $params['_meta'] ?? null);
protected function getParams(): ?array
{
return [
'uri' => $this->uri,
];
}
}

View file

@ -11,7 +11,6 @@
namespace Mcp\Schema\Notification;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\JsonRpc\Notification;
/**
@ -23,26 +22,18 @@ use Mcp\Schema\JsonRpc\Notification;
*/
class RootsListChangedNotification extends Notification
{
/**
* @param array<string, mixed>|null $_meta
*/
public function __construct(
public readonly ?array $_meta = null,
) {
$params = [];
if (null !== $_meta) {
$params['_meta'] = $_meta;
}
parent::__construct('notifications/roots/list_changed', $params);
public static function getMethod(): string
{
return 'notifications/roots/list_changed';
}
public static function fromNotification(Notification $notification): self
protected static function fromParams(?array $params): Notification
{
if ('notifications/roots/list_changed' !== $notification->method) {
throw new InvalidArgumentException('Notification is not a notifications/roots/list_changed notification');
}
return new self();
}
return new self($notification->params['_meta'] ?? null);
protected function getParams(): ?array
{
return null;
}
}

View file

@ -11,7 +11,6 @@
namespace Mcp\Schema\Notification;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\JsonRpc\Notification;
/**
@ -21,26 +20,18 @@ use Mcp\Schema\JsonRpc\Notification;
*/
class ToolListChangedNotification extends Notification
{
/**
* @param array<string, mixed>|null $_meta
*/
public function __construct(
public readonly ?array $_meta = null,
) {
$params = [];
if (null !== $_meta) {
$params['_meta'] = $_meta;
}
parent::__construct('notifications/tools/list_changed', $params);
public static function getMethod(): string
{
return 'notifications/tools/list_changed';
}
public static function fromNotification(Notification $notification): self
protected static function fromParams(?array $params): Notification
{
if ('notifications/tools/list_changed' !== $notification->method) {
throw new InvalidArgumentException('Notification is not a notifications/tools/list_changed notification');
}
return new self();
}
return new self($notification->params['_meta'] ?? null);
protected function getParams(): ?array
{
return null;
}
}