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\Tool\Tool;
23: use Nexus\Mcp\Core\Validation\EnumValueValidator;
24:
25: /**
26: * The result returned by the server for a `tools/list` request.
27: *
28: * @extends PaginatedResult<array{
29: * _meta?: template-type<MetaObject, Arrayable, 'T'>,
30: * resultType: non-empty-string,
31: * tools: list<template-type<Tool, 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#listtoolsresult
38: */
39: final readonly class ListToolsResult extends PaginatedResult implements ServerResult
40: {
41: /**
42: * @var list<Tool>
43: */
44: public array $tools;
45:
46: /**
47: * @param list<Tool> $tools
48: */
49: public function __construct(
50: array $tools,
51: int $ttlMs,
52: CacheScope $cacheScope,
53: ?Cursor $nextCursor = null,
54: MetaObject $meta = new MetaObject(),
55: ) {
56: Assert::that($tools)
57: ->isList('"result.tools" must be a list, non-list array given.')
58: ->values()->isInstanceOf(Tool::class)
59: ;
60:
61: $this->tools = $tools;
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('tools', '"result" is missing the required "tools" key.');
70: Assert::that($data['tools'])
71: ->isList('"result.tools" must be a list, {type} given.')
72: ->values()
73: ->isArray('each "result.tool" must be an object, {type} given.')
74: ->isMap('each "result.tool" must be a string-keyed object.')
75: ;
76: $tools = array_map(Tool::fromArray(...), $data['tools']);
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(tools: $tools, ttlMs: $ttlMs, cacheScope: $cacheScope, nextCursor: $nextCursor, meta: $meta);
104: }
105:
106: #[\Override]
107: public function toArray(): array
108: {
109: $data = [];
110: $meta = $this->meta->toArray();
111:
112: if ([] !== $meta) {
113: $data['_meta'] = $meta;
114: }
115:
116: $data['resultType'] = self::getResultType();
117: $data['tools'] = array_map(static fn(Tool $tool): array => $tool->toArray(), $this->tools);
118:
119: if (null !== $this->nextCursor) {
120: $data['nextCursor'] = $this->nextCursor->cursor;
121: }
122:
123: $data['ttlMs'] = $this->ttlMs;
124: $data['cacheScope'] = $this->cacheScope->value;
125:
126: return $data;
127: }
128:
129: #[\Override]
130: protected function getResultType(): string
131: {
132: return ResultType::Complete->value;
133: }
134: }
135: