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\Request;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\JsonRpc\JsonRpcRequest;
18: use Nexus\Mcp\Core\Schema\RequestId;
19: use Nexus\Mcp\Core\Schema\RequestParams;
20: use Nexus\Mcp\Core\Schema\RequestParams\ReadResourceRequestParams;
21:
22: /**
23: * Sent from the client to the server, to read a specific resource URI.
24: *
25: * @property-read ReadResourceRequestParams $params
26: *
27: * @extends JsonRpcRequest<'resources/read', array{
28: * jsonrpc: '2.0',
29: * id: int|non-empty-string,
30: * method: 'resources/read',
31: * params: template-type<ReadResourceRequestParams, RequestParams, 'T'>,
32: * }>
33: *
34: * @see https://modelcontextprotocol.io/specification/draft/schema#readresourcerequest
35: */
36: final readonly class ReadResourceRequest extends JsonRpcRequest implements ClientRequest
37: {
38: public function __construct(RequestId $id, ReadResourceRequestParams $params)
39: {
40: parent::__construct(id: $id, params: $params);
41: }
42:
43: #[\Override]
44: public static function getMethod(): string
45: {
46: return 'resources/read';
47: }
48:
49: #[\Override]
50: public static function fromArray(array $data): static
51: {
52: Assert::that($data)->hasOffset('id', 'missing the required "id" key.');
53: $id = $data['id'];
54: Assert::that($id)->isArrayKey('"id" must be an int or string, {type} given.');
55:
56: Assert::that($data)->hasOffset('params', 'missing the required "params" key.');
57: Assert::that($data['params'])
58: ->isArray('"params" must be an object, {type} given.')
59: ->isMap('"params" must be a string-keyed object.')
60: ;
61:
62: return new self(
63: id: new RequestId(id: $id),
64: params: ReadResourceRequestParams::fromArray($data['params']),
65: );
66: }
67:
68: #[\Override]
69: public function toArray(): array
70: {
71: return [
72: 'jsonrpc' => self::JSONRPC_VERSION,
73: 'id' => $this->id->id,
74: 'method' => static::getMethod(),
75: 'params' => $this->params->toArray(),
76: ];
77: }
78: }
79: