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\Extension\Tasks\Schema\ResultResponse;
15:
16: use Nexus\Mcp\Core\Schema\Enum\ResultType;
17: use Nexus\Mcp\Core\Schema\JsonRpc\JsonRpcResultResponse;
18: use Nexus\Mcp\Core\Schema\RequestId;
19: use Nexus\Mcp\Core\Schema\Result\CallToolResult;
20: use Nexus\Mcp\Core\Schema\Result\InputRequiredResult;
21: use Nexus\Mcp\Extension\Tasks\Schema\Result\CreateTaskResult;
22:
23: /**
24: * Response envelope for a task-aware `tools/call`.
25: *
26: * @property-read CallToolResult|CreateTaskResult|InputRequiredResult $result
27: *
28: * @extends JsonRpcResultResponse<array{
29: * jsonrpc: '2.0',
30: * id: int|non-empty-string,
31: * result: array<string, mixed>,
32: * }>
33: */
34: final readonly class TaskCallToolResultResponse extends JsonRpcResultResponse
35: {
36: public function __construct(RequestId $id, CallToolResult|CreateTaskResult|InputRequiredResult $result)
37: {
38: parent::__construct(id: $id, result: $result);
39: }
40:
41: #[\Override]
42: public static function fromArray(array $data): static
43: {
44: $id = self::parseId($data);
45: $payload = self::parseResult($data);
46:
47: $result = match (true) {
48: ($payload['resultType'] ?? null) === ResultType::Task->value => CreateTaskResult::fromArray($payload),
49: self::isInputRequired($payload) => InputRequiredResult::fromArray($payload),
50: default => CallToolResult::fromArray($payload),
51: };
52:
53: return new self(id: $id, result: $result);
54: }
55:
56: #[\Override]
57: public function toArray(): array
58: {
59: return [
60: 'jsonrpc' => self::JSONRPC_VERSION,
61: 'id' => $this->id->id,
62: 'result' => $this->result->toArray(),
63: ];
64: }
65: }
66: