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\NotificationParams;
19: use Nexus\Mcp\Core\Schema\ParsesNumber;
20: use Nexus\Mcp\Core\Schema\ProgressToken;
21:
22: /**
23: * Parameters for a `notifications/progress` notification.
24: *
25: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#progressnotificationparams
26: */
27: final readonly class ProgressNotificationParams extends NotificationParams
28: {
29: use ParsesNumber;
30:
31: public function __construct(
32: public ProgressToken $progressToken,
33: public float $progress,
34: public ?float $total = null,
35: public ?string $message = null,
36: MetaObject $meta = new MetaObject(),
37: ) {
38: parent::__construct($meta);
39: }
40:
41: /**
42: * @param array<string, mixed> $data
43: */
44: #[\Override]
45: public static function fromArray(array $data): static
46: {
47: Assert::that($data)->hasOffset('progressToken', 'missing the required "progressToken" key.');
48: $progressToken = $data['progressToken'];
49: Assert::that($progressToken)->isArrayKey('"params.progressToken" must be an int or string, {type} given.');
50:
51: Assert::that($data)->hasOffset('progress', 'missing the required "progress" key.');
52: $progress = self::parseNumber($data['progress'], '"params.progress" must be a number, {type} given.');
53:
54: $total = $data['total'] ?? null;
55:
56: if (null !== $total) {
57: $total = self::parseNumber($total, '"params.total" must be a number or null, {type} given.');
58: }
59:
60: $message = $data['message'] ?? null;
61: Assert::that($message)->nullOr()->isString('"params.message" must be a string or null, {type} given.');
62:
63: $meta = new MetaObject();
64:
65: if (\array_key_exists('_meta', $data)) {
66: Assert::that($data['_meta'])
67: ->isArray('"params._meta" must be an object, {type} given.')
68: ->isMap('"params._meta" must be a string-keyed object.')
69: ;
70: $meta = MetaObject::fromArray($data['_meta']);
71: }
72:
73: return new self(new ProgressToken($progressToken), $progress, $total, $message, $meta);
74: }
75:
76: #[\Override]
77: public function toArray(): array
78: {
79: $data = [
80: ...parent::toArray(),
81: 'progressToken' => $this->progressToken->token,
82: 'progress' => $this->progress,
83: ];
84:
85: if (null !== $this->total) {
86: $data['total'] = $this->total;
87: }
88:
89: if (null !== $this->message) {
90: $data['message'] = $this->message;
91: }
92:
93: return $data;
94: }
95:
96: #[\Override]
97: public function jsonSerialize(): array
98: {
99: return $this->toArray();
100: }
101: }
102: