1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2025 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\Server\Message\Result;
15:
16: use Nexus\Mcp\Implementation;
17: use Nexus\Mcp\Message\Result;
18: use Nexus\Mcp\ProtocolVersion;
19: use Nexus\Mcp\Server\ServerCapabilities;
20:
21: /**
22: * After receiving an initialize request from the client, the server sends this response.
23: */
24: final readonly class InitializeResult extends Result implements ServerResult
25: {
26: /**
27: * @param null|non-empty-string $instructions Instructions describing how to use the server and its features
28: * @param null|array<string, mixed> $meta Reserved by MCP to allow clients and servers to attach
29: * additional metadata to their interactions.
30: */
31: public function __construct(
32: public ProtocolVersion $protocolVersion,
33: public ServerCapabilities $capabilities,
34: public Implementation $serverInfo,
35: public ?string $instructions = null,
36: public ?array $meta = null,
37: ) {}
38:
39: /**
40: * @return array{
41: * _meta?: array<string, mixed>,
42: * capabilities: array<string, mixed>,
43: * instructions?: string,
44: * protocolVersion: string,
45: * serverInfo: array{name: non-empty-string, version: non-empty-string, title?: non-empty-string},
46: * }
47: */
48: #[\Override]
49: public function toArray(): array
50: {
51: return array_filter([
52: '_meta' => $this->meta,
53: 'capabilities' => $this->capabilities->toArray(),
54: 'instructions' => $this->instructions,
55: 'protocolVersion' => $this->protocolVersion->__toString(),
56: 'serverInfo' => $this->serverInfo->toArray(),
57: ], static fn(mixed $value): bool => null !== $value);
58: }
59:
60: /**
61: * @return array{
62: * _meta?: array<string, mixed>,
63: * capabilities: array<string, mixed>,
64: * instructions?: string,
65: * protocolVersion: string,
66: * serverInfo: array{name: non-empty-string, version: non-empty-string, title?: non-empty-string},
67: * }
68: */
69: #[\Override]
70: public function jsonSerialize(): array
71: {
72: return $this->toArray();
73: }
74: }
75: