@@ -70,6 +70,9 @@ class Protocol
7070
7171 public const SESSION_LOGGING_LEVEL = '_mcp.logging_level ' ;
7272
73+ /** Session key for the fingerprint of the principal that opened the session */
74+ private const SESSION_PRINCIPAL = '_mcp.principal ' ;
75+
7376 /**
7477 * Deliberately generic: unexpected throwables carry internal details such as file paths, class
7578 * names and argument types, which must not be handed to the peer. The full exception is logged.
@@ -139,7 +142,7 @@ public function connect(TransportInterface $transport): void
139142 {
140143 $ transport ->onMessage ($ this ->processInput (...));
141144
142- $ transport ->onSessionEnd ($ this ->destroySession (...));
145+ $ transport ->onSessionEnd ($ this ->endSession (...));
143146
144147 // The transport keeps these callbacks, so they reference it weakly to not keep it alive.
145148 $ transportRef = \WeakReference ::create ($ transport );
@@ -167,13 +170,14 @@ public function connect(TransportInterface $transport): void
167170 *
168171 * @param TransportInterface<mixed> $transport
169172 * @param AccessToken|null $accessToken the token the input was authorized with, if the transport authorizes
173+ * @param string|null $principal who the input was authenticated as, if the transport authenticates
170174 */
171- public function processInput (TransportInterface $ transport , string $ input , ?Uuid $ sessionId , ?AccessToken $ accessToken = null ): void
175+ public function processInput (TransportInterface $ transport , string $ input , ?Uuid $ sessionId , ?AccessToken $ accessToken = null , ? string $ principal = null ): void
172176 {
173177 // Last line of defense: a malformed message must never escape as a PHP error and take the
174178 // server process down.
175179 try {
176- $ this ->doProcessInput ($ transport , $ input , $ sessionId , $ accessToken );
180+ $ this ->doProcessInput ($ transport , $ input , $ sessionId , $ accessToken, $ principal );
177181 } catch (\Throwable $ e ) {
178182 $ this ->logger ->error (\sprintf ('Uncaught exception while processing input: %s ' , $ e ->getMessage ()), ['exception ' => $ e ]);
179183
@@ -230,7 +234,7 @@ private static function findResponseId(string $input): string|int|null
230234 /**
231235 * @param TransportInterface<mixed> $transport
232236 */
233- private function doProcessInput (TransportInterface $ transport , string $ input , ?Uuid $ sessionId , ?AccessToken $ accessToken ): void
237+ private function doProcessInput (TransportInterface $ transport , string $ input , ?Uuid $ sessionId , ?AccessToken $ accessToken, ? string $ principal ): void
234238 {
235239 $ this ->logger ->info ('Received message to process. ' );
236240 $ this ->logger ->debug ('Received message payload. ' , ['message ' => $ input ]);
@@ -269,7 +273,7 @@ private function doProcessInput(TransportInterface $transport, string $input, ?U
269273 }
270274
271275 try {
272- $ this ->handleMessages ($ transport , $ sessionId , $ messages , $ accessToken );
276+ $ this ->handleMessages ($ transport , $ sessionId , $ messages , $ accessToken, $ principal );
273277 } finally {
274278 if ($ takesLock ) {
275279 $ this ->releaseSessionLock ($ sessionId );
@@ -281,9 +285,9 @@ private function doProcessInput(TransportInterface $transport, string $input, ?U
281285 * @param TransportInterface<mixed> $transport
282286 * @param array<int, mixed> $messages
283287 */
284- private function handleMessages (TransportInterface $ transport , ?Uuid $ sessionId , array $ messages , ?AccessToken $ accessToken ): void
288+ private function handleMessages (TransportInterface $ transport , ?Uuid $ sessionId , array $ messages , ?AccessToken $ accessToken, ? string $ principal ): void
285289 {
286- $ session = $ this ->resolveSession ($ transport , $ sessionId , $ messages );
290+ $ session = $ this ->resolveSession ($ transport , $ sessionId , $ messages, $ principal );
287291 if (null === $ session ) {
288292 return ;
289293 }
@@ -1112,8 +1116,9 @@ private function hasInitializeRequest(array $messages): bool
11121116 * @param TransportInterface<mixed> $transport
11131117 * @param Uuid|null $sessionId The session ID from the transport
11141118 * @param array<int,mixed> $messages The parsed messages
1119+ * @param string|null $principal Who the messages were authenticated as
11151120 */
1116- private function resolveSession (TransportInterface $ transport , ?Uuid $ sessionId , array $ messages ): ?SessionInterface
1121+ private function resolveSession (TransportInterface $ transport , ?Uuid $ sessionId , array $ messages, ? string $ principal ): ?SessionInterface
11171122 {
11181123 if ($ this ->hasInitializeRequest ($ messages )) {
11191124 // Spec: An initialize request must not be part of a batch.
@@ -1133,6 +1138,10 @@ private function resolveSession(TransportInterface $transport, ?Uuid $sessionId,
11331138 }
11341139
11351140 $ session = $ this ->sessionManager ->create ();
1141+ if (null !== $ principal ) {
1142+ $ session ->set (self ::SESSION_PRINCIPAL , self ::fingerprint ($ principal ));
1143+ }
1144+
11361145 $ this ->logger ->debug ('Created new session for initialize ' , [
11371146 'session_id ' => $ session ->getId ()->toRfc4122 (),
11381147 ]);
@@ -1171,7 +1180,36 @@ private function resolveSession(TransportInterface $transport, ?Uuid $sessionId,
11711180 return null ;
11721181 }
11731182
1174- return $ this ->sessionManager ->createWithId ($ sessionId );
1183+ $ session = $ this ->sessionManager ->createWithId ($ sessionId );
1184+
1185+ if (!$ this ->servesPrincipal ($ session , $ principal )) {
1186+ $ this ->logger ->warning ('Refused a request of another principal than the one that opened the session. ' , [
1187+ 'session_id ' => $ sessionId ->toRfc4122 (),
1188+ ]);
1189+
1190+ // Answered like an unknown session, so that another principal cannot tell the session exists.
1191+ $ error = Error ::forInvalidRequest ('Session not found or has expired. ' );
1192+ $ this ->sendResponse ($ transport , $ error , null , ['status_code ' => 404 ]);
1193+
1194+ return null ;
1195+ }
1196+
1197+ return $ session ;
1198+ }
1199+
1200+ /**
1201+ * Whether the session serves the principal: the one that opened it, or no principal for a session opened without one.
1202+ */
1203+ private function servesPrincipal (SessionInterface $ session , ?string $ principal ): bool
1204+ {
1205+ $ fingerprint = $ session ->get (self ::SESSION_PRINCIPAL );
1206+
1207+ return (\is_string ($ fingerprint ) ? $ fingerprint : null ) === self ::fingerprint ($ principal );
1208+ }
1209+
1210+ private static function fingerprint (?string $ principal ): ?string
1211+ {
1212+ return null === $ principal ? null : hash ('sha256 ' , $ principal );
11751213 }
11761214
11771215 /**
@@ -1189,6 +1227,22 @@ private static function findRequestId(array $messages): string|int|null
11891227 };
11901228 }
11911229
1230+ /**
1231+ * Ends a session at the request of a client, which must be the principal that opened it.
1232+ */
1233+ private function endSession (Uuid $ sessionId , ?string $ principal = null ): void
1234+ {
1235+ if ($ this ->sessionManager ->exists ($ sessionId ) && !$ this ->servesPrincipal ($ this ->sessionManager ->createWithId ($ sessionId ), $ principal )) {
1236+ $ this ->logger ->warning ('Refused to end a session for another principal than the one that opened it. ' , [
1237+ 'session_id ' => $ sessionId ->toRfc4122 (),
1238+ ]);
1239+
1240+ return ;
1241+ }
1242+
1243+ $ this ->destroySession ($ sessionId );
1244+ }
1245+
11921246 /**
11931247 * Destroy a specific session.
11941248 */
0 commit comments