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\EmptyRequestParams;
21:
22: /**
23: * A request from the client asking the server to advertise its supported
24: * protocol versions, capabilities, and other metadata. Servers **MUST**
25: * implement `server/discover`. Clients **MAY** call it but are not required
26: * to — version negotiation can also happen inline via per-request `_meta`.
27: *
28: * @property-read EmptyRequestParams $params
29: *
30: * @extends JsonRpcRequest<'server/discover', array{
31: * jsonrpc: '2.0',
32: * id: int|non-empty-string,
33: * method: 'server/discover',
34: * params: template-type<EmptyRequestParams, RequestParams, 'T'>,
35: * }>
36: *
37: * @see https://modelcontextprotocol.io/specification/draft/schema#discoverrequest
38: */
39: final readonly class DiscoverRequest extends JsonRpcRequest implements ClientRequest
40: {
41: public function __construct(RequestId $id, EmptyRequestParams $params)
42: {
43: parent::__construct(id: $id, params: $params);
44: }
45:
46: #[\Override]
47: public static function getMethod(): string
48: {
49: return 'server/discover';
50: }
51:
52: #[\Override]
53: public static function fromArray(array $data): static
54: {
55: Assert::that($data)->hasOffset('id', 'missing the required "id" key.');
56: $id = $data['id'];
57: Assert::that($id)->isArrayKey('"id" must be an int or string, {type} given.');
58:
59: Assert::that($data)->hasOffset('params', 'missing the required "params" key.');
60: Assert::that($data['params'])
61: ->isArray('"params" must be an object, {type} given.')
62: ->isMap('"params" must be a string-keyed object.')
63: ;
64: $params = EmptyRequestParams::fromArray($data['params']);
65:
66: return new self(id: new RequestId(id: $id), params: $params);
67: }
68:
69: #[\Override]
70: public function toArray(): array
71: {
72: return [
73: 'jsonrpc' => self::JSONRPC_VERSION,
74: 'id' => $this->id->id,
75: 'method' => static::getMethod(),
76: 'params' => $this->params->toArray(),
77: ];
78: }
79: }
80: