| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | /** |
| 6: | * This file is part of the Nexus MCP SDK package. |
| 7: | * |
| 8: | * (c) 2025 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\Schema\Message; |
| 15: | |
| 16: | use Nexus\Mcp\Schema\Result\Result; |
| 17: | |
| 18: | /** |
| 19: | * A successful (non-error) response to a request. |
| 20: | * |
| 21: | * @template TArray of array<string, mixed> |
| 22: | * @template TJson of array<string, mixed>|\stdClass |
| 23: | * |
| 24: | * @extends Message< |
| 25: | * array{jsonrpc: '2.0', id: int|non-empty-string, result: TArray}, |
| 26: | * array{jsonrpc: '2.0', id: int|non-empty-string, result: TJson} |
| 27: | * > |
| 28: | */ |
| 29: | final readonly class JsonRpcResponse extends Message |
| 30: | { |
| 31: | /** |
| 32: | * @var non-empty-string |
| 33: | */ |
| 34: | public string $jsonrpc; |
| 35: | |
| 36: | /** |
| 37: | * @param RequestId $id A uniquely identifying ID for a request in JSON-RPC. |
| 38: | * @param Result<TArray, TJson> $result The result of the request. |
| 39: | */ |
| 40: | public function __construct( |
| 41: | public RequestId $id, |
| 42: | public Result $result, |
| 43: | ) { |
| 44: | $this->jsonrpc = self::JSON_RPC_VERSION; |
| 45: | } |
| 46: | |
| 47: | #[\Override] |
| 48: | public function toArray(): array |
| 49: | { |
| 50: | return [ |
| 51: | 'jsonrpc' => $this->jsonrpc, |
| 52: | 'id' => $this->id->value, |
| 53: | 'result' => $this->result->toArray(), |
| 54: | ]; |
| 55: | } |
| 56: | |
| 57: | #[\Override] |
| 58: | public function jsonSerialize(): array |
| 59: | { |
| 60: | return [ |
| 61: | 'jsonrpc' => $this->jsonrpc, |
| 62: | 'id' => $this->id->value, |
| 63: | 'result' => $this->result->jsonSerialize(), |
| 64: | ]; |
| 65: | } |
| 66: | } |
| 67: |