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