2121use Mcp \Schema \Request \InitializeRequest ;
2222use Mcp \Server \Handler \Notification \NotificationHandlerInterface ;
2323use Mcp \Server \Handler \Request \RequestHandlerInterface ;
24- use Mcp \Server \Session \SessionFactoryInterface ;
2524use Mcp \Server \Session \SessionInterface ;
26- use Mcp \Server \Session \SessionStoreInterface ;
25+ use Mcp \Server \Session \SessionManagerInterface ;
2726use Mcp \Server \Transport \TransportInterface ;
2827use Psr \Log \LoggerInterface ;
2928use Psr \Log \NullLogger ;
@@ -65,8 +64,7 @@ public function __construct(
6564 private readonly array $ requestHandlers ,
6665 private readonly array $ notificationHandlers ,
6766 private readonly MessageFactory $ messageFactory ,
68- private readonly SessionFactoryInterface $ sessionFactory ,
69- private readonly SessionStoreInterface $ sessionStore ,
67+ private readonly SessionManagerInterface $ sessionManager ,
7068 private readonly LoggerInterface $ logger = new NullLogger (),
7169 ) {
7270 }
@@ -106,7 +104,7 @@ public function processInput(TransportInterface $transport, string $input, ?Uuid
106104 {
107105 $ this ->logger ->info ('Received message to process. ' , ['message ' => $ input ]);
108106
109- $ this ->gcSessions ();
107+ $ this ->sessionManager -> gc ();
110108
111109 try {
112110 $ messages = $ this ->messageFactory ->create ($ input );
@@ -367,7 +365,7 @@ private function queueOutgoing(Request|Notification|Response|Error $message, arr
367365 */
368366 public function consumeOutgoingMessages (Uuid $ sessionId ): array
369367 {
370- $ session = $ this ->sessionFactory ->createWithId ($ sessionId, $ this -> sessionStore );
368+ $ session = $ this ->sessionManager ->createWithId ($ sessionId );
371369 $ queue = $ session ->get (self ::SESSION_OUTGOING_QUEUE , []);
372370 $ session ->set (self ::SESSION_OUTGOING_QUEUE , []);
373371 $ session ->save ();
@@ -386,7 +384,7 @@ public function consumeOutgoingMessages(Uuid $sessionId): array
386384 */
387385 public function checkResponse (int $ requestId , Uuid $ sessionId ): Response |Error |null
388386 {
389- $ session = $ this ->sessionFactory ->createWithId ($ sessionId, $ this -> sessionStore );
387+ $ session = $ this ->sessionManager ->createWithId ($ sessionId );
390388 $ responseData = $ session ->get (self ::SESSION_RESPONSES .". {$ requestId }" );
391389
392390 if (null === $ responseData ) {
@@ -428,7 +426,7 @@ public function checkResponse(int $requestId, Uuid $sessionId): Response|Error|n
428426 */
429427 public function getPendingRequests (Uuid $ sessionId ): array
430428 {
431- $ session = $ this ->sessionFactory ->createWithId ($ sessionId, $ this -> sessionStore );
429+ $ session = $ this ->sessionManager ->createWithId ($ sessionId );
432430
433431 return $ session ->get (self ::SESSION_PENDING_REQUESTS , []);
434432 }
@@ -455,7 +453,7 @@ public function handleFiberYield(mixed $yieldedValue, ?Uuid $sessionId): void
455453 return ;
456454 }
457455
458- $ session = $ this ->sessionFactory ->createWithId ($ sessionId, $ this -> sessionStore );
456+ $ session = $ this ->sessionManager ->createWithId ($ sessionId );
459457
460458 $ payloadSessionId = $ yieldedValue ['session_id ' ] ?? null ;
461459 if (\is_string ($ payloadSessionId ) && $ payloadSessionId !== $ sessionId ->toRfc4122 ()) {
@@ -539,7 +537,7 @@ private function resolveSession(TransportInterface $transport, ?Uuid $sessionId,
539537 return null ;
540538 }
541539
542- $ session = $ this ->sessionFactory ->create ($ this -> sessionStore );
540+ $ session = $ this ->sessionManager ->create ();
543541 $ this ->logger ->debug ('Created new session for initialize ' , [
544542 'session_id ' => $ session ->getId ()->toRfc4122 (),
545543 ]);
@@ -556,41 +554,22 @@ private function resolveSession(TransportInterface $transport, ?Uuid $sessionId,
556554 return null ;
557555 }
558556
559- if (!$ this ->sessionStore ->exists ($ sessionId )) {
557+ if (!$ this ->sessionManager ->exists ($ sessionId )) {
560558 $ error = Error::forInvalidRequest ('Session not found or has expired. ' );
561559 $ this ->sendResponse ($ transport , $ error , null , ['status_code ' => 404 ]);
562560
563561 return null ;
564562 }
565563
566- return $ this ->sessionFactory ->createWithId ($ sessionId , $ this ->sessionStore );
567- }
568-
569- /**
570- * Run garbage collection on expired sessions.
571- * Uses the session store's internal TTL configuration.
572- */
573- private function gcSessions (): void
574- {
575- if (random_int (0 , 100 ) > 1 ) {
576- return ;
577- }
578-
579- $ deletedSessions = $ this ->sessionStore ->gc ();
580- if (!empty ($ deletedSessions )) {
581- $ this ->logger ->debug ('Garbage collected expired sessions. ' , [
582- 'count ' => \count ($ deletedSessions ),
583- 'session_ids ' => array_map (static fn (Uuid $ id ) => $ id ->toRfc4122 (), $ deletedSessions ),
584- ]);
585- }
564+ return $ this ->sessionManager ->createWithId ($ sessionId );
586565 }
587566
588567 /**
589568 * Destroy a specific session.
590569 */
591570 public function destroySession (Uuid $ sessionId ): void
592571 {
593- $ this ->sessionStore ->destroy ($ sessionId );
572+ $ this ->sessionManager ->destroy ($ sessionId );
594573 $ this ->logger ->info ('Session destroyed. ' , ['session_id ' => $ sessionId ->toRfc4122 ()]);
595574 }
596575}
0 commit comments