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\Arrayable;
18: use Nexus\Mcp\Core\Schema\MetaObject;
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<MetaObject, Arrayable, '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/draft/schema#progressnotificationparams
35: */
36: final readonly class ProgressNotificationParams extends NotificationParams
37: {
38: use ParsesNumber;
39:
40: /**
41: * @var null|non-empty-string
42: */
43: public ?string $message;
44:
45: public function __construct(
46: public ProgressToken $progressToken,
47: public float $progress,
48: public ?float $total = null,
49: ?string $message = null,
50: MetaObject $meta = new MetaObject(),
51: ) {
52: Assert::that($message)->nullOr()->isNonEmptyString('"params.message" must be a non-empty string or null.');
53:
54: $this->message = $message;
55:
56: parent::__construct(meta: $meta);
57: }
58:
59: #[\Override]
60: public static function fromArray(array $data): static
61: {
62: Assert::that($data)->hasOffset('progressToken', '"params" is missing the required "progressToken" key.');
63: $progressToken = $data['progressToken'];
64: Assert::that($progressToken)->isArrayKey('"params.progressToken" must be an int or string, {type} given.');
65:
66: Assert::that($data)->hasOffset('progress', '"params" is missing the required "progress" key.');
67: $progress = self::parseNumber($data['progress'], '"params.progress" must be a number, {type} given.');
68:
69: $total = $data['total'] ?? null;
70:
71: if (null !== $total) {
72: $total = self::parseNumber($total, '"params.total" must be a number or null, {type} given.');
73: }
74:
75: $message = $data['message'] ?? null;
76: Assert::that($message)->nullOr()->isString('"params.message" must be a string or null, {type} given.');
77:
78: $meta = new MetaObject();
79:
80: if (\array_key_exists('_meta', $data)) {
81: Assert::that($data['_meta'])
82: ->isArray('"params._meta" must be an object, {type} given.')
83: ->isMap('"params._meta" must be a string-keyed object.')
84: ;
85: $meta = MetaObject::fromArray($data['_meta']);
86: }
87:
88: return new self(
89: progressToken: new ProgressToken(token: $progressToken),
90: progress: $progress,
91: total: $total,
92: message: $message,
93: meta: $meta,
94: );
95: }
96:
97: #[\Override]
98: public function toArray(): array
99: {
100: $data = [];
101: $meta = $this->meta->toArray();
102:
103: if ([] !== $meta) {
104: $data['_meta'] = $meta;
105: }
106:
107: $data['progressToken'] = $this->progressToken->token;
108: $data['progress'] = $this->progress;
109:
110: if (null !== $this->total) {
111: $data['total'] = $this->total;
112: }
113:
114: if (null !== $this->message) {
115: $data['message'] = $this->message;
116: }
117:
118: return $data;
119: }
120:
121: #[\Override]
122: public function jsonSerialize(): array
123: {
124: return $this->toArray();
125: }
126: }
127: