| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Schema\JsonRpc; |
| 15: | |
| 16: | use Nexus\Mcp\Core\Schema\Arrayable; |
| 17: | use Nexus\Mcp\Core\Schema\Request; |
| 18: | use Nexus\Mcp\Core\Schema\RequestId; |
| 19: | use Nexus\Mcp\Core\Schema\RequestParams; |
| 20: | use Nexus\Mcp\Core\Schema\RequestParams\EmptyRequestParams; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | abstract readonly class JsonRpcRequest extends Request implements Arrayable, JsonRpcMessage |
| 38: | { |
| 39: | public function __construct(public RequestId $id, RequestParams $params = new EmptyRequestParams()) |
| 40: | { |
| 41: | parent::__construct($params); |
| 42: | } |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | #[\Override] |
| 48: | abstract public static function fromArray(array $data): static; |
| 49: | |
| 50: | #[\Override] |
| 51: | public function toArray(): array |
| 52: | { |
| 53: | $envelope = [ |
| 54: | 'jsonrpc' => self::JSONRPC_VERSION, |
| 55: | 'id' => $this->id->id, |
| 56: | 'method' => static::getMethod(), |
| 57: | ]; |
| 58: | |
| 59: | $params = $this->params->toArray(); |
| 60: | |
| 61: | if ([] !== $params) { |
| 62: | $envelope['params'] = $params; |
| 63: | } |
| 64: | |
| 65: | return $envelope; |
| 66: | } |
| 67: | |
| 68: | #[\Override] |
| 69: | public function jsonSerialize(): array |
| 70: | { |
| 71: | $envelope = [ |
| 72: | 'jsonrpc' => self::JSONRPC_VERSION, |
| 73: | 'id' => $this->id->id, |
| 74: | 'method' => static::getMethod(), |
| 75: | ]; |
| 76: | |
| 77: | $params = $this->params->jsonSerialize(); |
| 78: | |
| 79: | if ([] !== $params) { |
| 80: | $envelope['params'] = $params; |
| 81: | } |
| 82: | |
| 83: | return $envelope; |
| 84: | } |
| 85: | } |
| 86: | |