66
77package modelengine .fel .tool .mcp .server .transport ;
88
9- import com .fasterxml .jackson .core .type .TypeReference ;
10- import com .fasterxml .jackson .databind .ObjectMapper ;
119import io .modelcontextprotocol .common .McpTransportContext ;
10+ import io .modelcontextprotocol .json .McpJsonMapper ;
1211import io .modelcontextprotocol .json .TypeRef ;
1312import io .modelcontextprotocol .server .McpTransportContextExtractor ;
1413import io .modelcontextprotocol .spec .*;
3332import reactor .core .publisher .Mono ;
3433
3534import java .io .IOException ;
36- import java .lang .reflect .Type ;
3735import java .time .Duration ;
3836import java .util .List ;
3937import java .util .Map ;
@@ -59,7 +57,7 @@ public class FitMcpStreamableServerTransportProvider implements McpStreamableSer
5957 * Flag indicating whether DELETE requests are disallowed on the endpoint.
6058 */
6159 private final boolean disallowDelete ;
62- private final ObjectMapper objectMapper ;
60+ private final McpJsonMapper jsonMapper ;
6361 private final McpTransportContextExtractor <HttpClassicServerRequest > contextExtractor ;
6462 private KeepAliveScheduler keepAliveScheduler ;
6563
@@ -80,21 +78,21 @@ public class FitMcpStreamableServerTransportProvider implements McpStreamableSer
8078 * Constructs a new FitMcpStreamableServerTransportProvider instance,
8179 * for {@link FitMcpStreamableServerTransportProvider.Builder}.
8280 *
83- * @param objectMapper The ObjectMapper to use for JSON serialization/deserialization
81+ * @param jsonMapper The jsonMapper to use for JSON serialization/deserialization
8482 * of messages.
8583 * @param disallowDelete Whether to disallow DELETE requests on the endpoint.
8684 * @param contextExtractor The context extractor to fill in a {@link McpTransportContext}.
8785 * @param keepAliveInterval The interval for sending keep-alive messages to clients.
8886 * @throws IllegalArgumentException if any parameter is null
8987 */
90- private FitMcpStreamableServerTransportProvider (ObjectMapper objectMapper ,
88+ private FitMcpStreamableServerTransportProvider (McpJsonMapper jsonMapper ,
9189 boolean disallowDelete ,
9290 McpTransportContextExtractor <HttpClassicServerRequest > contextExtractor ,
9391 Duration keepAliveInterval ) {
94- Validation .notNull (objectMapper , "ObjectMapper must not be null" );
92+ Validation .notNull (jsonMapper , "jsonMapper must not be null" );
9593 Validation .notNull (contextExtractor , "McpTransportContextExtractor must not be null" );
9694
97- this .objectMapper = objectMapper ;
95+ this .jsonMapper = jsonMapper ;
9896 this .disallowDelete = disallowDelete ;
9997 this .contextExtractor = contextExtractor ;
10098
@@ -321,15 +319,14 @@ public Object handlePost(HttpClassicServerRequest request,
321319 }
322320 McpTransportContext transportContext = this .contextExtractor .extract (request );
323321 try {
324-
325322 McpSchema .JSONRPCMessage message = this .deserializeJsonRpcMessage (requestBody );
326323
327324 // Handle initialization request
328325 if (message instanceof McpSchema .JSONRPCRequest jsonrpcRequest
329326 && jsonrpcRequest .method ().equals (McpSchema .METHOD_INITIALIZE )) {
330327 logger .info ("[POST] Handling initialize method, with receiving message: {}" , requestBody .toString ());
331- McpSchema .InitializeRequest initializeRequest = objectMapper .convertValue (jsonrpcRequest .params (),
332- new TypeReference <McpSchema .InitializeRequest >() {
328+ McpSchema .InitializeRequest initializeRequest = jsonMapper .convertValue (jsonrpcRequest .params (),
329+ new TypeRef <McpSchema .InitializeRequest >() {
333330 });
334331 McpStreamableServerSession .McpStreamableServerSessionInit init = this .sessionFactory
335332 .startSession (initializeRequest );
@@ -487,24 +484,22 @@ public Object handleDelete(HttpClassicServerRequest request, HttpClassicServerRe
487484 }
488485
489486 /**
490- * Handles DELETE requests for session deletion.
487+ * deserialize Map to JsonRpcMessage
491488 *
492489 * @param map the map of JSON-RPC message
493490 * @return The corresponding {@link McpSchema.JSONRPCMessage} class
494491 * @throws IOException when cannot deserialize JSONRPCMessage
495492 */
496493 public McpSchema .JSONRPCMessage deserializeJsonRpcMessage (Map <String , Object > map )
497494 throws IOException {
498-
499- // Determine message type based on specific JSON structure
500495 if (map .containsKey ("method" ) && map .containsKey ("id" )) {
501- return objectMapper .convertValue (map , McpSchema .JSONRPCRequest .class );
496+ return jsonMapper .convertValue (map , McpSchema .JSONRPCRequest .class );
502497 }
503498 else if (map .containsKey ("method" ) && !map .containsKey ("id" )) {
504- return objectMapper .convertValue (map , McpSchema .JSONRPCNotification .class );
499+ return jsonMapper .convertValue (map , McpSchema .JSONRPCNotification .class );
505500 }
506501 else if (map .containsKey ("result" ) || map .containsKey ("error" )) {
507- return objectMapper .convertValue (map , McpSchema .JSONRPCResponse .class );
502+ return jsonMapper .convertValue (map , McpSchema .JSONRPCResponse .class );
508503 }
509504
510505 throw new IllegalArgumentException ("Cannot deserialize JSONRPCMessage: " + map .toString ());
@@ -583,7 +578,7 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message, String messageId
583578 return ;
584579 }
585580
586- String jsonText = objectMapper .writeValueAsString (message );
581+ String jsonText = jsonMapper .writeValueAsString (message );
587582 TextEvent textEvent = TextEvent .custom ()
588583 .id (this .sessionId ).event (Event .MESSAGE .code ()).data (jsonText ).build ();
589584 this .emitter .emit (textEvent );
@@ -607,7 +602,7 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message, String messageId
607602 }
608603
609604 /**
610- * Converts data from one type to another using the configured ObjectMapper .
605+ * Converts data from one type to another using the configured jsonMapper .
611606 *
612607 * @param data The source data object to convert
613608 * @param typeRef The target type reference
@@ -616,14 +611,7 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message, String messageId
616611 */
617612 @ Override
618613 public <T > T unmarshalFrom (Object data , TypeRef <T > typeRef ) {
619- // Convert TypeRef to TypeReference for ObjectMapper compatibility
620- TypeReference <T > typeReference = new TypeReference <T >() {
621- @ Override
622- public Type getType () {
623- return typeRef .getType ();
624- }
625- };
626- return objectMapper .convertValue (data , typeReference );
614+ return jsonMapper .convertValue (data , typeRef );
627615 }
628616
629617 /**
@@ -671,22 +659,22 @@ public static Builder builder() {
671659 * Builder for creating instances of {@link FitMcpStreamableServerTransportProvider}.
672660 */
673661 public static class Builder {
674- private ObjectMapper objectMapper ;
662+ private McpJsonMapper jsonMapper ;
675663 private boolean disallowDelete = false ;
676664 private McpTransportContextExtractor <HttpClassicServerRequest > contextExtractor = (
677665 HttpClassicServerRequest ) -> McpTransportContext .EMPTY ;
678666 private Duration keepAliveInterval ;
679667
680668 /**
681- * Sets the ObjectMapper to use for JSON serialization/deserialization of MCP messages.
669+ * Sets the jsonMapper to use for JSON serialization/deserialization of MCP messages.
682670 *
683- * @param objectMapper The ObjectMapper instance. Must not be null.
671+ * @param jsonMapper The jsonMapper instance. Must not be null.
684672 * @return this builder instance
685- * @throws IllegalArgumentException if objectMapper is null
673+ * @throws IllegalArgumentException if jsonMapper is null
686674 */
687- public Builder objectMapper ( ObjectMapper objectMapper ) {
688- Validation .notNull (objectMapper , "ObjectMapper must not be null" );
689- this .objectMapper = objectMapper ;
675+ public Builder jsonMapper ( McpJsonMapper jsonMapper ) {
676+ Validation .notNull (jsonMapper , "jsonMapper must not be null" );
677+ this .jsonMapper = jsonMapper ;
690678 return this ;
691679 }
692680
@@ -739,12 +727,10 @@ public Builder keepAliveInterval(Duration keepAliveInterval) {
739727 * @throws IllegalStateException if required parameters are not set
740728 */
741729 public FitMcpStreamableServerTransportProvider build () {
742- Validation .notNull (this .objectMapper , "ObjectMapper must be set" );
730+ Validation .notNull (this .jsonMapper , "jsonMapper must be set" );
743731
744- return new FitMcpStreamableServerTransportProvider (this .objectMapper , this .disallowDelete ,
732+ return new FitMcpStreamableServerTransportProvider (this .jsonMapper , this .disallowDelete ,
745733 this .contextExtractor , this .keepAliveInterval );
746734 }
747-
748735 }
749-
750736}
0 commit comments