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\Result;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\Arrayable;
18: use Nexus\Mcp\Core\Schema\Cursor;
19: use Nexus\Mcp\Core\Schema\Enum\CacheScope;
20: use Nexus\Mcp\Core\Schema\Enum\ResultType;
21: use Nexus\Mcp\Core\Schema\MetaObject;
22: use Nexus\Mcp\Core\Schema\Resource\Resource;
23: use Nexus\Mcp\Core\Validation\EnumValueValidator;
24:
25: /**
26: * The result returned by the server for a `resources/list` request.
27: *
28: * @extends PaginatedResult<array{
29: * _meta?: template-type<MetaObject, Arrayable, 'T'>,
30: * resultType: non-empty-string,
31: * resources: list<template-type<Resource, Arrayable, 'T'>>,
32: * nextCursor?: non-empty-string,
33: * ttlMs: int,
34: * cacheScope: value-of<CacheScope>,
35: * }>
36: *
37: * @see https://modelcontextprotocol.io/specification/draft/schema#listresourcesresult
38: */
39: final readonly class ListResourcesResult extends PaginatedResult implements ServerResult
40: {
41: /**
42: * @var list<Resource>
43: */
44: public array $resources;
45:
46: /**
47: * @param list<Resource> $resources
48: */
49: public function __construct(
50: array $resources,
51: int $ttlMs,
52: CacheScope $cacheScope,
53: ?Cursor $nextCursor = null,
54: MetaObject $meta = new MetaObject(),
55: ) {
56: Assert::that($resources)
57: ->isList('"result.resources" must be a list, non-list array given.')
58: ->values()->isInstanceOf(Resource::class)
59: ;
60:
61: $this->resources = $resources;
62:
63: parent::__construct(ttlMs: $ttlMs, cacheScope: $cacheScope, nextCursor: $nextCursor, meta: $meta);
64: }
65:
66: #[\Override]
67: public static function fromArray(array $data): static
68: {
69: Assert::that($data)->hasOffset('resources', '"result" is missing the required "resources" key.');
70: Assert::that($data['resources'])
71: ->isList('"result.resources" must be a list, {type} given.')
72: ->values()
73: ->isArray('each "result.resource" must be an object, {type} given.')
74: ->isMap('each "result.resource" must be a string-keyed object.')
75: ;
76: $resources = array_map(Resource::fromArray(...), $data['resources']);
77:
78: Assert::that($data)->hasOffset('ttlMs', '"result" is missing the required "ttlMs" key.');
79: $ttlMs = $data['ttlMs'];
80: Assert::that($ttlMs)->isInt('"result.ttlMs" must be an integer, {type} given.');
81:
82: Assert::that($data)->hasOffset('cacheScope', '"result" is missing the required "cacheScope" key.');
83: $cacheScope = EnumValueValidator::parse(CacheScope::class, $data['cacheScope'], '"result.cacheScope"');
84:
85: $nextCursor = null;
86:
87: if (\array_key_exists('nextCursor', $data)) {
88: $raw = $data['nextCursor'];
89: Assert::that($raw)->isString('"result.nextCursor" must be a string, {type} given.');
90: $nextCursor = new Cursor(cursor: $raw);
91: }
92:
93: $meta = new MetaObject();
94:
95: if (\array_key_exists('_meta', $data)) {
96: Assert::that($data['_meta'])
97: ->isArray('"result._meta" must be an object, {type} given.')
98: ->isMap('"result._meta" must be a string-keyed object.')
99: ;
100: $meta = MetaObject::fromArray($data['_meta']);
101: }
102:
103: return new self(
104: resources: $resources,
105: ttlMs: $ttlMs,
106: cacheScope: $cacheScope,
107: nextCursor: $nextCursor,
108: meta: $meta,
109: );
110: }
111:
112: #[\Override]
113: public function toArray(): array
114: {
115: $data = [];
116: $meta = $this->meta->toArray();
117:
118: if ([] !== $meta) {
119: $data['_meta'] = $meta;
120: }
121:
122: $data['resultType'] = self::getResultType();
123: $data['resources'] = array_map(
124: static fn(Resource $resource): array => $resource->toArray(),
125: $this->resources,
126: );
127:
128: if (null !== $this->nextCursor) {
129: $data['nextCursor'] = $this->nextCursor->cursor;
130: }
131:
132: $data['ttlMs'] = $this->ttlMs;
133: $data['cacheScope'] = $this->cacheScope->value;
134:
135: return $data;
136: }
137:
138: #[\Override]
139: protected function getResultType(): string
140: {
141: return ResultType::Complete->value;
142: }
143: }
144: