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\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\RequestParamsInterface;
20:
21: /**
22: * A request that expects a response.
23: *
24: * @template-covariant TMethod of non-empty-string
25: * @template-covariant TEnvelope of array<string, mixed> = array<string, mixed>
26: *
27: * @extends Request<TMethod>
28: * @implements Arrayable<TEnvelope>
29: *
30: * @see https://modelcontextprotocol.io/specification/draft/schema#jsonrpcrequest
31: */
32: abstract readonly class JsonRpcRequest extends Request implements Arrayable, JsonRpcMessage
33: {
34: public function __construct(public RequestId $id, ?RequestParamsInterface $params = null)
35: {
36: parent::__construct(params: $params);
37: }
38:
39: #[\Override]
40: public function jsonSerialize(): array
41: {
42: $envelope = [
43: 'jsonrpc' => self::JSONRPC_VERSION,
44: 'id' => $this->id->id,
45: 'method' => static::getMethod(),
46: ];
47:
48: if (null !== $this->params) {
49: $envelope['params'] = $this->params->jsonSerialize();
50: }
51:
52: return $envelope;
53: }
54: }
55: