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\SubscriptionsListenRequestParams;
21:
22: /**
23: * Sent from the client to open a long-lived channel for receiving notifications outside the
24: * context of a specific request. Replaces the previous HTTP GET endpoint and ensures
25: * consistent behavior between HTTP and STDIO.
26: *
27: * @property-read SubscriptionsListenRequestParams $params
28: *
29: * @extends JsonRpcRequest<'subscriptions/listen', array{
30: * jsonrpc: '2.0',
31: * id: int|non-empty-string,
32: * method: 'subscriptions/listen',
33: * params: template-type<SubscriptionsListenRequestParams, RequestParams, 'T'>,
34: * }>
35: *
36: * @see https://modelcontextprotocol.io/specification/draft/schema#subscriptionslistenrequest
37: */
38: final readonly class SubscriptionsListenRequest extends JsonRpcRequest implements ClientRequest
39: {
40: public function __construct(RequestId $id, SubscriptionsListenRequestParams $params)
41: {
42: parent::__construct(id: $id, params: $params);
43: }
44:
45: #[\Override]
46: public static function getMethod(): string
47: {
48: return 'subscriptions/listen';
49: }
50:
51: #[\Override]
52: public static function fromArray(array $data): static
53: {
54: Assert::that($data)->hasOffset('id', 'missing the required "id" key.');
55: $id = $data['id'];
56: Assert::that($id)->isArrayKey('"id" must be an int or string, {type} given.');
57:
58: Assert::that($data)->hasOffset('params', 'missing the required "params" key.');
59: Assert::that($data['params'])
60: ->isArray('"params" must be an object, {type} given.')
61: ->isMap('"params" must be a string-keyed object.')
62: ;
63:
64: return new self(
65: id: new RequestId(id: $id),
66: params: SubscriptionsListenRequestParams::fromArray($data['params']),
67: );
68: }
69:
70: #[\Override]
71: public function toArray(): array
72: {
73: return [
74: 'jsonrpc' => self::JSONRPC_VERSION,
75: 'id' => $this->id->id,
76: 'method' => static::getMethod(),
77: 'params' => $this->params->toArray(),
78: ];
79: }
80: }
81: