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\Client\Dispatch;
15:
16: use Nexus\Mcp\Core\Dispatch\InitializationState;
17: use Nexus\Mcp\Core\Schema\Request\InitializeRequest;
18: use Nexus\Mcp\Core\Schema\Request\PingRequest;
19:
20: /**
21: * Tracks the client-initiated handshake lifecycle and decides which outbound
22: * request methods may be sent before it completes.
23: */
24: final class ClientInitializationGate
25: {
26: private InitializationState $state = InitializationState::AwaitingInitialize;
27:
28: public function isInitialized(): bool
29: {
30: return InitializationState::Initialized === $this->state;
31: }
32:
33: /**
34: * @param non-empty-string $requestMethod
35: */
36: public function allowsRequest(string $requestMethod): bool
37: {
38: if (InitializeRequest::getMethod() === $requestMethod) {
39: return InitializationState::AwaitingInitialize === $this->state;
40: }
41:
42: return $this->isInitialized() || PingRequest::getMethod() === $requestMethod;
43: }
44:
45: /**
46: * Transitions `AwaitingInitialize` -> `InitializeInFlight`. Returns `true` if the transition fired.
47: */
48: public function markInitializeInFlight(): bool
49: {
50: if (InitializationState::AwaitingInitialize !== $this->state) {
51: return false;
52: }
53:
54: $this->state = InitializationState::InitializeInFlight;
55:
56: return true;
57: }
58:
59: /**
60: * Transitions `InitializeInFlight` -> `Initialized`. Returns `true` if the transition fired.
61: */
62: public function markInitialized(): bool
63: {
64: if (InitializationState::InitializeInFlight !== $this->state) {
65: return false;
66: }
67:
68: $this->state = InitializationState::Initialized;
69:
70: return true;
71: }
72:
73: /**
74: * Reverts `InitializeInFlight` -> `AwaitingInitialize`. Returns `true` if the transition fired.
75: */
76: public function revertInitializeInFlight(): bool
77: {
78: if (InitializationState::InitializeInFlight !== $this->state) {
79: return false;
80: }
81:
82: $this->state = InitializationState::AwaitingInitialize;
83:
84: return true;
85: }
86: }
87: