2525use Mcp \Schema \Request \InitializeRequest ;
2626use Mcp \Server \Handler \Notification \NotificationHandlerInterface ;
2727use Mcp \Server \Handler \Request \RequestHandlerInterface ;
28- use Mcp \Server \Session \SessionFactoryInterface ;
2928use Mcp \Server \Session \SessionInterface ;
30- use Mcp \Server \Session \SessionStoreInterface ;
29+ use Mcp \Server \Session \SessionManagerInterface ;
3130use Mcp \Server \Transport \TransportInterface ;
3231use Psr \EventDispatcher \EventDispatcherInterface ;
3332use Psr \Log \LoggerInterface ;
@@ -70,8 +69,7 @@ public function __construct(
7069 private readonly array $ requestHandlers ,
7170 private readonly array $ notificationHandlers ,
7271 private readonly MessageFactory $ messageFactory ,
73- private readonly SessionFactoryInterface $ sessionFactory ,
74- private readonly SessionStoreInterface $ sessionStore ,
72+ private readonly SessionManagerInterface $ sessionManager ,
7573 private readonly LoggerInterface $ logger = new NullLogger (),
7674 private readonly ?EventDispatcherInterface $ eventDispatcher = null ,
7775 ) {
@@ -112,7 +110,7 @@ public function processInput(TransportInterface $transport, string $input, ?Uuid
112110 {
113111 $ this ->logger ->info ('Received message to process. ' , ['message ' => $ input ]);
114112
115- $ this ->gcSessions ();
113+ $ this ->sessionManager -> gc ();
116114
117115 try {
118116 $ messages = $ this ->messageFactory ->create ($ input );
@@ -410,7 +408,7 @@ private function queueOutgoing(Request|Notification|Response|Error $message, arr
410408 */
411409 public function consumeOutgoingMessages (Uuid $ sessionId ): array
412410 {
413- $ session = $ this ->sessionFactory ->createWithId ($ sessionId, $ this -> sessionStore );
411+ $ session = $ this ->sessionManager ->createWithId ($ sessionId );
414412 $ queue = $ session ->get (self ::SESSION_OUTGOING_QUEUE , []);
415413 $ session ->set (self ::SESSION_OUTGOING_QUEUE , []);
416414 $ session ->save ();
@@ -429,7 +427,7 @@ public function consumeOutgoingMessages(Uuid $sessionId): array
429427 */
430428 public function checkResponse (int $ requestId , Uuid $ sessionId ): Response |Error |null
431429 {
432- $ session = $ this ->sessionFactory ->createWithId ($ sessionId, $ this -> sessionStore );
430+ $ session = $ this ->sessionManager ->createWithId ($ sessionId );
433431 $ responseData = $ session ->get (self ::SESSION_RESPONSES .". {$ requestId }" );
434432
435433 if (null === $ responseData ) {
@@ -471,7 +469,7 @@ public function checkResponse(int $requestId, Uuid $sessionId): Response|Error|n
471469 */
472470 public function getPendingRequests (Uuid $ sessionId ): array
473471 {
474- $ session = $ this ->sessionFactory ->createWithId ($ sessionId, $ this -> sessionStore );
472+ $ session = $ this ->sessionManager ->createWithId ($ sessionId );
475473
476474 return $ session ->get (self ::SESSION_PENDING_REQUESTS , []);
477475 }
@@ -498,7 +496,7 @@ public function handleFiberYield(mixed $yieldedValue, ?Uuid $sessionId): void
498496 return ;
499497 }
500498
501- $ session = $ this ->sessionFactory ->createWithId ($ sessionId, $ this -> sessionStore );
499+ $ session = $ this ->sessionManager ->createWithId ($ sessionId );
502500
503501 $ payloadSessionId = $ yieldedValue ['session_id ' ] ?? null ;
504502 if (\is_string ($ payloadSessionId ) && $ payloadSessionId !== $ sessionId ->toRfc4122 ()) {
@@ -582,7 +580,7 @@ private function resolveSession(TransportInterface $transport, ?Uuid $sessionId,
582580 return null ;
583581 }
584582
585- $ session = $ this ->sessionFactory ->create ($ this -> sessionStore );
583+ $ session = $ this ->sessionManager ->create ();
586584 $ this ->logger ->debug ('Created new session for initialize ' , [
587585 'session_id ' => $ session ->getId ()->toRfc4122 (),
588586 ]);
@@ -599,41 +597,22 @@ private function resolveSession(TransportInterface $transport, ?Uuid $sessionId,
599597 return null ;
600598 }
601599
602- if (!$ this ->sessionStore ->exists ($ sessionId )) {
600+ if (!$ this ->sessionManager ->exists ($ sessionId )) {
603601 $ error = Error::forInvalidRequest ('Session not found or has expired. ' );
604602 $ this ->sendResponse ($ transport , $ error , null , ['status_code ' => 404 ]);
605603
606604 return null ;
607605 }
608606
609- return $ this ->sessionFactory ->createWithId ($ sessionId , $ this ->sessionStore );
610- }
611-
612- /**
613- * Run garbage collection on expired sessions.
614- * Uses the session store's internal TTL configuration.
615- */
616- private function gcSessions (): void
617- {
618- if (random_int (0 , 100 ) > 1 ) {
619- return ;
620- }
621-
622- $ deletedSessions = $ this ->sessionStore ->gc ();
623- if (!empty ($ deletedSessions )) {
624- $ this ->logger ->debug ('Garbage collected expired sessions. ' , [
625- 'count ' => \count ($ deletedSessions ),
626- 'session_ids ' => array_map (static fn (Uuid $ id ) => $ id ->toRfc4122 (), $ deletedSessions ),
627- ]);
628- }
607+ return $ this ->sessionManager ->createWithId ($ sessionId );
629608 }
630609
631610 /**
632611 * Destroy a specific session.
633612 */
634613 public function destroySession (Uuid $ sessionId ): void
635614 {
636- $ this ->sessionStore ->destroy ($ sessionId );
615+ $ this ->sessionManager ->destroy ($ sessionId );
637616 $ this ->logger ->info ('Session destroyed. ' , ['session_id ' => $ sessionId ->toRfc4122 ()]);
638617 }
639618}
0 commit comments