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\RequestParams;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\Cursor;
18: use Nexus\Mcp\Core\Schema\MetaObject;
19: use Nexus\Mcp\Core\Schema\MetaObject\RequestMetaObject;
20: use Nexus\Mcp\Core\Schema\RequestParams;
21:
22: /**
23: * Common params for paginated requests.
24: *
25: * @extends RequestParams<array{
26: * _meta: template-type<RequestMetaObject, MetaObject, 'T'>,
27: * cursor?: non-empty-string,
28: * }>
29: *
30: * @see https://modelcontextprotocol.io/specification/2026-07-28/schema#paginatedrequestparams
31: */
32: final readonly class PaginatedRequestParams extends RequestParams
33: {
34: public function __construct(
35: RequestMetaObject $meta,
36: public ?Cursor $cursor = null,
37: ) {
38: parent::__construct(meta: $meta);
39: }
40:
41: #[\Override]
42: public static function fromArray(array $data): static
43: {
44: $cursor = null;
45:
46: if (\array_key_exists('cursor', $data)) {
47: $raw = $data['cursor'];
48: Assert::that($raw)->isNonEmptyString('"params.cursor" must be a non-empty string, {type} given.');
49: $cursor = new Cursor(cursor: $raw);
50: }
51:
52: Assert::that($data)->hasOffset('_meta', '"params" is missing the required "_meta" key.');
53: Assert::that($data['_meta'])
54: ->isArray('"params._meta" must be an object, {type} given.')
55: ->not()->isNonEmptyList('"params._meta" must be a string-keyed object.')
56: ;
57: $meta = RequestMetaObject::fromArray($data['_meta']);
58:
59: return new self(meta: $meta, cursor: $cursor);
60: }
61:
62: #[\Override]
63: public function toArray(): array
64: {
65: $data = ['_meta' => $this->meta->toArray()];
66:
67: if (null !== $this->cursor) {
68: $data['cursor'] = $this->cursor->cursor;
69: }
70:
71: return $data;
72: }
73: }
74: