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\MetaObject;
18: use Nexus\Mcp\Core\Schema\Result;
19: use Nexus\Mcp\Core\Schema\Task\Task;
20:
21: /**
22: * A response to a task-augmented request.
23: *
24: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#createtaskresult
25: */
26: final readonly class CreateTaskResult extends Result implements ClientResult, ServerResult
27: {
28: public function __construct(public Task $task, MetaObject $meta = new MetaObject())
29: {
30: parent::__construct($meta);
31: }
32:
33: /**
34: * @param array<string, mixed> $data
35: */
36: #[\Override]
37: public static function fromArray(array $data): static
38: {
39: Assert::that($data)->hasOffset('task', '"result" missing the required "task" key.');
40: Assert::that($data['task'])
41: ->isArray('"result.task" must be an object, {type} given.')
42: ->isMap('"result.task" must be a string-keyed object.')
43: ;
44:
45: $task = Task::fromArray($data['task']);
46:
47: $meta = new MetaObject();
48:
49: if (\array_key_exists('_meta', $data)) {
50: Assert::that($data['_meta'])
51: ->isArray('"result._meta" must be an object, {type} given.')
52: ->isMap('"result._meta" must be a string-keyed object.')
53: ;
54: $meta = MetaObject::fromArray($data['_meta']);
55: }
56:
57: return new self($task, $meta);
58: }
59:
60: #[\Override]
61: public function toArray(): array
62: {
63: return [
64: ...parent::toArray(),
65: 'task' => $this->task->toArray(),
66: ];
67: }
68:
69: #[\Override]
70: public function jsonSerialize(): array
71: {
72: return $this->toArray();
73: }
74: }
75: