Skip to content

Commit 931ab2b

Browse files
refactor: Replace ClientSession with ClientState for clearer intent on runtime state management
1 parent c290238 commit 931ab2b

File tree

9 files changed

+53
-65
lines changed

9 files changed

+53
-65
lines changed

src/Client.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,23 @@ public function connect(TransportInterface $transport): void
9292
*/
9393
public function isConnected(): bool
9494
{
95-
return null !== $this->transport && $this->protocol->getSession()->isInitialized();
95+
return null !== $this->transport && $this->protocol->getState()->isInitialized();
9696
}
9797

9898
/**
9999
* Get server information from initialization.
100100
*/
101101
public function getServerInfo(): ?Implementation
102102
{
103-
return $this->protocol->getSession()->getServerInfo();
103+
return $this->protocol->getState()->getServerInfo();
104104
}
105105

106106
/**
107107
* Get server instructions.
108108
*/
109109
public function getInstructions(): ?string
110110
{
111-
return $this->protocol->getSession()->getInstructions();
111+
return $this->protocol->getState()->getInstructions();
112112
}
113113

114114
/**

src/Client/Handler/Notification/ProgressNotificationHandler.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
namespace Mcp\Client\Handler\Notification;
1313

14-
use Mcp\Client\Session\ClientSessionInterface;
14+
use Mcp\Client\State\ClientStateInterface;
1515
use Mcp\Schema\JsonRpc\Notification;
1616
use Mcp\Schema\Notification\ProgressNotification;
1717

1818
/**
1919
* Internal handler for progress notifications.
2020
*
21-
* Writes progress data to session for transport to consume and execute callbacks.
21+
* Writes progress data to state for transport to consume and execute callbacks.
2222
*
2323
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
2424
*
@@ -27,7 +27,7 @@
2727
class ProgressNotificationHandler implements NotificationHandlerInterface
2828
{
2929
public function __construct(
30-
private readonly ClientSessionInterface $session,
30+
private readonly ClientStateInterface $state,
3131
) {
3232
}
3333

@@ -42,7 +42,7 @@ public function handle(Notification $notification): void
4242
return;
4343
}
4444

45-
$this->session->storeProgress(
45+
$this->state->storeProgress(
4646
(string) $notification->progressToken,
4747
$notification->progress,
4848
$notification->total,

src/Client/Protocol.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use Mcp\Client\Handler\Notification\NotificationHandlerInterface;
1515
use Mcp\Client\Handler\Notification\ProgressNotificationHandler;
1616
use Mcp\Client\Handler\Request\RequestHandlerInterface;
17-
use Mcp\Client\Session\ClientSession;
18-
use Mcp\Client\Session\ClientSessionInterface;
17+
use Mcp\Client\State\ClientState;
18+
use Mcp\Client\State\ClientStateInterface;
1919
use Mcp\Client\Transport\TransportInterface;
2020
use Mcp\JsonRpc\MessageFactory;
2121
use Mcp\Schema\JsonRpc\Error;
@@ -41,7 +41,7 @@
4141
class Protocol
4242
{
4343
private ?TransportInterface $transport = null;
44-
private ClientSessionInterface $session;
44+
private ClientStateInterface $state;
4545
private MessageFactory $messageFactory;
4646
private LoggerInterface $logger;
4747

@@ -58,12 +58,12 @@ public function __construct(
5858
?MessageFactory $messageFactory = null,
5959
?LoggerInterface $logger = null,
6060
) {
61-
$this->session = new ClientSession();
61+
$this->state = new ClientState();
6262
$this->messageFactory = $messageFactory ?? MessageFactory::make();
6363
$this->logger = $logger ?? new NullLogger();
6464

6565
$this->notificationHandlers = [
66-
new ProgressNotificationHandler($this->session),
66+
new ProgressNotificationHandler($this->state),
6767
...$notificationHandlers,
6868
];
6969
}
@@ -79,7 +79,7 @@ public function __construct(
7979
public function connect(TransportInterface $transport, Configuration $config): void
8080
{
8181
$this->transport = $transport;
82-
$transport->setSession($this->session);
82+
$transport->setState($this->state);
8383
$transport->onInitialize(fn () => $this->initialize($config));
8484
$transport->onMessage($this->processMessage(...));
8585
$transport->onError(fn (\Throwable $e) => $this->logger->error('Transport error', ['exception' => $e]));
@@ -108,9 +108,9 @@ public function initialize(Configuration $config): Response|Error
108108

109109
if ($response instanceof Response) {
110110
$initResult = InitializeResult::fromArray($response->result);
111-
$this->session->setServerInfo($initResult->serverInfo);
112-
$this->session->setInstructions($initResult->instructions);
113-
$this->session->setInitialized(true);
111+
$this->state->setServerInfo($initResult->serverInfo);
112+
$this->state->setInstructions($initResult->instructions);
113+
$this->state->setInitialized(true);
114114

115115
$this->sendNotification(new InitializedNotification());
116116

@@ -136,18 +136,18 @@ public function initialize(Configuration $config): Response|Error
136136
*/
137137
public function request(Request $request, int $timeout, bool $withProgress = false): Response|Error
138138
{
139-
$requestId = $this->session->nextRequestId();
139+
$requestId = $this->state->nextRequestId();
140140
$request = $request->withId($requestId);
141141

142142
if ($withProgress) {
143143
$progressToken = "prog-{$requestId}";
144144
$request = $request->withMeta(['progressToken' => $progressToken]);
145145
}
146146

147-
$this->session->addPendingRequest($requestId, $timeout);
147+
$this->state->addPendingRequest($requestId, $timeout);
148148
$this->sendRequest($request);
149149

150-
$immediate = $this->session->consumeResponse($requestId);
150+
$immediate = $this->state->consumeResponse($requestId);
151151
if (null !== $immediate) {
152152
$this->logger->debug('Received immediate response', ['id' => $requestId]);
153153

@@ -242,7 +242,7 @@ private function handleResponse(Response|Error $response): void
242242

243243
$this->logger->debug('Handling response', ['id' => $requestId]);
244244

245-
$this->session->storeResponse($requestId, $response->jsonSerialize());
245+
$this->state->storeResponse($requestId, $response->jsonSerialize());
246246
}
247247

248248
/**
@@ -311,8 +311,8 @@ private function handleNotification(Notification $notification): void
311311
}
312312
}
313313

314-
public function getSession(): ClientSessionInterface
314+
public function getState(): ClientStateInterface
315315
{
316-
return $this->session;
316+
return $this->state;
317317
}
318318
}
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Mcp\Client\Session;
12+
namespace Mcp\Client\State;
1313

1414
use Mcp\Schema\Implementation;
1515
use Mcp\Schema\JsonRpc\Error;
1616
use Mcp\Schema\JsonRpc\Response;
17-
use Symfony\Component\Uid\Uuid;
1817

1918
/**
20-
* In-memory client session implementation.
19+
* In-memory client state implementation.
20+
*
21+
* Stores ephemeral runtime state for the client's connection to a server.
22+
* This includes pending requests, responses, progress updates, and
23+
* negotiated parameters from initialization.
2124
*
2225
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
2326
*/
24-
class ClientSession implements ClientSessionInterface
27+
class ClientState implements ClientStateInterface
2528
{
26-
private Uuid $id;
2729
private int $requestIdCounter = 1;
2830
private bool $initialized = false;
2931
private ?Implementation $serverInfo = null;
@@ -38,16 +40,6 @@ class ClientSession implements ClientSessionInterface
3840
/** @var array<int, array{token: string, progress: float, total: ?float, message: ?string}> */
3941
private array $progressUpdates = [];
4042

41-
public function __construct(?Uuid $id = null)
42-
{
43-
$this->id = $id ?? Uuid::v4();
44-
}
45-
46-
public function getId(): Uuid
47-
{
48-
return $this->id;
49-
}
50-
5143
public function nextRequestId(): int
5244
{
5345
return $this->requestIdCounter++;

src/Client/Session/ClientSessionInterface.php renamed to src/Client/State/ClientStateInterface.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,23 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Mcp\Client\Session;
12+
namespace Mcp\Client\State;
1313

1414
use Mcp\Schema\Implementation;
1515
use Mcp\Schema\JsonRpc\Error;
1616
use Mcp\Schema\JsonRpc\Response;
17-
use Symfony\Component\Uid\Uuid;
1817

1918
/**
20-
* Interface for client session state management.
19+
* Interface for client state management.
2120
*
22-
* Tracks pending requests, stores responses, and manages message queues.
21+
* Tracks pending requests, stores responses, and manages runtime state
22+
* for the client's connection to a server. This is ephemeral state that
23+
* exists only for the lifetime of the connection.
2324
*
2425
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
2526
*/
26-
interface ClientSessionInterface
27+
interface ClientStateInterface
2728
{
28-
/**
29-
* Get the session ID.
30-
*/
31-
public function getId(): Uuid;
32-
3329
/**
3430
* Get the next request ID for outgoing requests.
3531
*/
@@ -76,7 +72,7 @@ public function consumeResponse(int $requestId): Response|Error|null;
7672
public function setInitialized(bool $initialized): void;
7773

7874
/**
79-
* Check if session is initialized.
75+
* Check if connection is initialized.
8076
*/
8177
public function isInitialized(): bool;
8278

src/Client/Transport/BaseTransport.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Mcp\Client\Transport;
1313

14-
use Mcp\Client\Session\ClientSessionInterface;
14+
use Mcp\Client\State\ClientStateInterface;
1515
use Psr\Log\LoggerInterface;
1616
use Psr\Log\NullLogger;
1717

@@ -36,7 +36,7 @@ abstract class BaseTransport implements TransportInterface
3636
/** @var callable(string): void|null */
3737
protected $closeCallback;
3838

39-
protected ?ClientSessionInterface $session = null;
39+
protected ?ClientStateInterface $state = null;
4040
protected LoggerInterface $logger;
4141

4242
public function __construct(?LoggerInterface $logger = null)
@@ -64,9 +64,9 @@ public function onClose(callable $listener): void
6464
$this->closeCallback = $listener;
6565
}
6666

67-
public function setSession(ClientSessionInterface $session): void
67+
public function setState(ClientStateInterface $state): void
6868
{
69-
$this->session = $session;
69+
$this->state = $state;
7070
}
7171

7272
/**

src/Client/Transport/HttpTransport.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ private function processSSEEvent(string $event): void
241241
*/
242242
private function processProgress(): void
243243
{
244-
if (null === $this->activeProgressCallback || null === $this->session) {
244+
if (null === $this->activeProgressCallback || null === $this->state) {
245245
return;
246246
}
247247

248-
$updates = $this->session->consumeProgressUpdates();
248+
$updates = $this->state->consumeProgressUpdates();
249249

250250
foreach ($updates as $update) {
251251
try {
@@ -266,18 +266,18 @@ private function processFiber(): void
266266
return;
267267
}
268268

269-
if (null === $this->session) {
269+
if (null === $this->state) {
270270
return;
271271
}
272272

273-
$pendingRequests = $this->session->getPendingRequests();
273+
$pendingRequests = $this->state->getPendingRequests();
274274

275275
foreach ($pendingRequests as $pending) {
276276
$requestId = $pending['request_id'];
277277
$timestamp = $pending['timestamp'];
278278
$timeout = $pending['timeout'];
279279

280-
$response = $this->session->consumeResponse($requestId);
280+
$response = $this->state->consumeResponse($requestId);
281281

282282
if (null !== $response) {
283283
$this->logger->debug('Resuming fiber with response', ['request_id' => $requestId]);

src/Client/Transport/StdioTransport.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ private function tick(): void
196196
*/
197197
private function processProgress(): void
198198
{
199-
if (null === $this->activeProgressCallback || null === $this->session) {
199+
if (null === $this->activeProgressCallback || null === $this->state) {
200200
return;
201201
}
202202

203-
$updates = $this->session->consumeProgressUpdates();
203+
$updates = $this->state->consumeProgressUpdates();
204204

205205
foreach ($updates as $update) {
206206
try {
@@ -243,19 +243,19 @@ private function processFiber(): void
243243
return;
244244
}
245245

246-
if (null === $this->session) {
246+
if (null === $this->state) {
247247
return;
248248
}
249249

250-
$pendingRequests = $this->session->getPendingRequests();
250+
$pendingRequests = $this->state->getPendingRequests();
251251

252252
foreach ($pendingRequests as $pending) {
253253
$requestId = $pending['request_id'];
254254
$timestamp = $pending['timestamp'];
255255
$timeout = $pending['timeout'];
256256

257257
// Check if response arrived
258-
$response = $this->session->consumeResponse($requestId);
258+
$response = $this->state->consumeResponse($requestId);
259259

260260
if (null !== $response) {
261261
$this->logger->debug('Resuming fiber with response', ['request_id' => $requestId]);

src/Client/Transport/TransportInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Mcp\Client\Transport;
1313

14-
use Mcp\Client\Session\ClientSessionInterface;
14+
use Mcp\Client\State\ClientStateInterface;
1515
use Mcp\Schema\JsonRpc\Error;
1616
use Mcp\Schema\JsonRpc\Response;
1717

@@ -101,7 +101,7 @@ public function onError(callable $callback): void;
101101
public function onClose(callable $callback): void;
102102

103103
/**
104-
* Set the client session for state management.
104+
* Set the client state for runtime state management.
105105
*/
106-
public function setSession(ClientSessionInterface $session): void;
106+
public function setState(ClientStateInterface $state): void;
107107
}

0 commit comments

Comments
 (0)