11package io .modelcontextprotocol .server .transport ;
22
33import java .io .IOException ;
4- import java .util .Map ;
54import java .util .concurrent .ConcurrentHashMap ;
65
76import com .fasterxml .jackson .core .type .TypeReference ;
87import com .fasterxml .jackson .databind .ObjectMapper ;
9- import io .modelcontextprotocol .spec .McpError ;
10- import io .modelcontextprotocol .spec .McpSchema ;
11- import io .modelcontextprotocol .spec .McpServerSession ;
12- import io .modelcontextprotocol .spec .McpServerTransport ;
13- import io .modelcontextprotocol .spec .McpServerTransportProvider ;
8+ import io .modelcontextprotocol .spec .*;
149import io .modelcontextprotocol .util .Assert ;
1510import org .slf4j .Logger ;
1611import org .slf4j .LoggerFactory ;
@@ -100,6 +95,8 @@ public class WebFluxSseServerTransportProvider implements McpServerTransportProv
10095
10196 private McpServerSession .Factory sessionFactory ;
10297
98+ private McpContextFactory mcpContextFactory ;
99+
103100 /**
104101 * Map of active client sessions, keyed by session ID.
105102 */
@@ -169,6 +166,11 @@ public void setSessionFactory(McpServerSession.Factory sessionFactory) {
169166 this .sessionFactory = sessionFactory ;
170167 }
171168
169+ @ Override
170+ public void setMcpContextFactory (final McpContextFactory mcpContextFactory ) {
171+ this .mcpContextFactory = mcpContextFactory ;
172+ }
173+
172174 /**
173175 * Broadcasts a JSON-RPC message to all connected clients through their SSE
174176 * connections. The message is serialized to JSON and sent as a server-sent event to
@@ -261,7 +263,7 @@ private Mono<ServerResponse> handleSseConnection(ServerRequest request) {
261263 .body (Flux .<ServerSentEvent <?>>create (sink -> {
262264 WebFluxMcpSessionTransport sessionTransport = new WebFluxMcpSessionTransport (sink );
263265
264- McpServerSession session = sessionFactory .create (sessionTransport );
266+ McpServerSession session = sessionFactory .create (sessionTransport , createContext ( request ) );
265267 String sessionId = session .getId ();
266268
267269 logger .debug ("Created new SSE connection for session: {}" , sessionId );
@@ -280,6 +282,18 @@ private Mono<ServerResponse> handleSseConnection(ServerRequest request) {
280282 }), ServerSentEvent .class );
281283 }
282284
285+ private McpContext createContext (final ServerRequest request ) {
286+ // create a context form the request
287+ McpContext context ;
288+ if (mcpContextFactory != null ) {
289+ context = mcpContextFactory .create (request );
290+ }
291+ else {
292+ context = McpContext .empty ();
293+ }
294+ return context ;
295+ }
296+
283297 /**
284298 * Handles incoming JSON-RPC messages from clients. Deserializes the message and
285299 * processes it through the configured message handler.
@@ -314,14 +328,16 @@ private Mono<ServerResponse> handleMessage(ServerRequest request) {
314328 return request .bodyToMono (String .class ).flatMap (body -> {
315329 try {
316330 McpSchema .JSONRPCMessage message = McpSchema .deserializeJsonRpcMessage (objectMapper , body );
317- return session .handle (message ).flatMap (response -> ServerResponse .ok ().build ()).onErrorResume (error -> {
318- logger .error ("Error processing message: {}" , error .getMessage ());
319- // TODO: instead of signalling the error, just respond with 200 OK
320- // - the error is signalled on the SSE connection
321- // return ServerResponse.ok().build();
322- return ServerResponse .status (HttpStatus .INTERNAL_SERVER_ERROR )
323- .bodyValue (new McpError (error .getMessage ()));
324- });
331+ return session .handle (message , createContext (request ))
332+ .flatMap (response -> ServerResponse .ok ().build ())
333+ .onErrorResume (error -> {
334+ logger .error ("Error processing message: {}" , error .getMessage ());
335+ // TODO: instead of signalling the error, just respond with 200 OK
336+ // - the error is signalled on the SSE connection
337+ // return ServerResponse.ok().build();
338+ return ServerResponse .status (HttpStatus .INTERNAL_SERVER_ERROR )
339+ .bodyValue (new McpError (error .getMessage ()));
340+ });
325341 }
326342 catch (IllegalArgumentException | IOException e ) {
327343 logger .error ("Failed to deserialize message: {}" , e .getMessage ());
0 commit comments