1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Core\Schema\NotificationParams;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\MetaObject;
18: use Nexus\Mcp\Core\Schema\MetaObject\NotificationMetaObject;
19: use Nexus\Mcp\Core\Schema\NotificationParams;
20: use Nexus\Mcp\Core\Schema\ParsesNumber;
21: use Nexus\Mcp\Core\Schema\ProgressToken;
22:
23: /**
24: * Parameters for a `notifications/progress` notification.
25: *
26: * @extends NotificationParams<array{
27: * _meta?: template-type<NotificationMetaObject, MetaObject, 'T'>,
28: * progressToken: int|non-empty-string,
29: * progress: float,
30: * total?: float,
31: * message?: non-empty-string,
32: * }>
33: *
34: * @see https://modelcontextprotocol.io/specification/2026-07-28/schema#progressnotificationparams
35: */
36: final readonly class ProgressNotificationParams extends NotificationParams
37: {
38: use ParsesNumber;
39:
40: /**
41: * @param null|non-empty-string $message
42: */
43: public function __construct(
44: public ProgressToken $progressToken,
45: public float $progress,
46: public ?float $total = null,
47: public ?string $message = null,
48: NotificationMetaObject $meta = new NotificationMetaObject(),
49: ) {
50: Assert::that($message)->nullOr()->isNonEmptyString('"params.message" must be a non-empty string or null.');
51:
52: parent::__construct(meta: $meta);
53: }
54:
55: #[\Override]
56: public static function fromArray(array $data): static
57: {
58: Assert::that($data)->hasOffset('progressToken', '"params" is missing the required "progressToken" key.');
59: $progressToken = $data['progressToken'];
60: Assert::that($progressToken)->isIntOrNonEmptyString('"params.progressToken" must be an int or non-empty string, {type} given.');
61:
62: Assert::that($data)->hasOffset('progress', '"params" is missing the required "progress" key.');
63: $progress = self::parseNumber($data['progress'], '"params.progress" must be a number, {type} given.');
64:
65: $total = $data['total'] ?? null;
66:
67: if (null !== $total) {
68: $total = self::parseNumber($total, '"params.total" must be a number or null, {type} given.');
69: }
70:
71: $message = $data['message'] ?? null;
72: Assert::that($message)->nullOr()->isNonEmptyString('"params.message" must be a non-empty string or null, {type} given.');
73:
74: $meta = new NotificationMetaObject();
75:
76: if (\array_key_exists('_meta', $data)) {
77: Assert::that($data['_meta'])
78: ->isArray('"params._meta" must be an object, {type} given.')
79: ->not()->isNonEmptyList('"params._meta" must be a string-keyed object.')
80: ;
81: $meta = NotificationMetaObject::fromArray($data['_meta']);
82: }
83:
84: return new self(
85: progressToken: new ProgressToken(token: $progressToken),
86: progress: $progress,
87: total: $total,
88: message: $message,
89: meta: $meta,
90: );
91: }
92:
93: #[\Override]
94: public function toArray(): array
95: {
96: $data = [];
97: $meta = $this->meta->toArray();
98:
99: if ([] !== $meta) {
100: $data['_meta'] = $meta;
101: }
102:
103: $data['progressToken'] = $this->progressToken->token;
104: $data['progress'] = $this->progress;
105:
106: if (null !== $this->total) {
107: $data['total'] = $this->total;
108: }
109:
110: if (null !== $this->message) {
111: $data['message'] = $this->message;
112: }
113:
114: return $data;
115: }
116:
117: #[\Override]
118: public function jsonSerialize(): array
119: {
120: return $this->toArray();
121: }
122: }
123: