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;
15:
16: /**
17: * The latest version of the Model Context Protocol that the client supports.
18: * The client MAY decide to support older versions as well.
19: */
20: final readonly class ProtocolVersion implements \JsonSerializable, \Stringable
21: {
22: public const string LATEST_PROTOCOL_VERSION = '2025-06-18';
23: public const array SUPPORTED_PROTOCOL_VERSIONS = [
24: self::LATEST_PROTOCOL_VERSION,
25: '2025-03-26',
26: '2024-11-05',
27: ];
28:
29: private string $version;
30: private \DateTimeImmutable $versionAsDate;
31:
32: /**
33: * @param non-empty-string $version
34: */
35: public function __construct(string $version)
36: {
37: if (! \in_array($version, self::SUPPORTED_PROTOCOL_VERSIONS, true)) {
38: throw new \InvalidArgumentException(\sprintf(
39: 'Unsupported protocol version "%s". Supported versions are: "%s".',
40: $version,
41: implode('", "', self::SUPPORTED_PROTOCOL_VERSIONS),
42: ));
43: }
44:
45: $this->version = $version;
46: $this->versionAsDate = new \DateTimeImmutable($version, new \DateTimeZone('UTC'));
47: }
48:
49: #[\Override]
50: public function __toString(): string
51: {
52: return $this->version;
53: }
54:
55: #[\Override]
56: public function jsonSerialize(): string
57: {
58: return $this->version;
59: }
60:
61: public function supportsBatchRequest(): bool
62: {
63: return $this->versionAsDate <= new \DateTimeImmutable('2025-03-26', new \DateTimeZone('UTC'));
64: }
65:
66: public function supportsElicitation(): bool
67: {
68: return $this->versionAsDate >= new \DateTimeImmutable('2025-06-18', new \DateTimeZone('UTC'));
69: }
70: }
71: