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;
15:
16: use Nexus\Assert\Assert;
17:
18: /**
19: * The `_meta` extension slot carried by request params. Adds a typed `progressToken`
20: * alongside open-ended extras.
21: *
22: * @implements Arrayable<array<string, mixed>>
23: *
24: * @see https://modelcontextprotocol.io/specification/2025-11-25/basic#_meta
25: */
26: final readonly class RequestMetaObject implements Arrayable
27: {
28: /**
29: * @param array<string, mixed> $extras
30: */
31: public function __construct(public ?ProgressToken $progressToken = null, public array $extras = [])
32: {
33: }
34:
35: #[\Override]
36: public static function fromArray(array $data): static
37: {
38: $progressToken = null;
39:
40: if (\array_key_exists('progressToken', $data)) {
41: $raw = $data['progressToken'];
42: Assert::that($raw)->isArrayKey('"_meta.progressToken" must be an int or string, {type} given.');
43:
44: $progressToken = new ProgressToken($raw);
45: unset($data['progressToken']);
46: }
47:
48: return new self($progressToken, $data);
49: }
50:
51: #[\Override]
52: public function toArray(): array
53: {
54: $out = $this->extras;
55:
56: if (null !== $this->progressToken) {
57: $out['progressToken'] = $this->progressToken->token;
58: }
59:
60: return $out;
61: }
62:
63: #[\Override]
64: public function jsonSerialize(): array|\stdClass
65: {
66: $data = $this->toArray();
67:
68: return [] === $data ? new \stdClass() : $data;
69: }
70: }
71: