|
| 1 | +package spring.ai.experimental.mcp.spec; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.UUID; |
| 6 | +import java.util.concurrent.ConcurrentHashMap; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import reactor.core.publisher.Mono; |
| 11 | +import reactor.core.publisher.MonoSink; |
| 12 | +import spring.ai.experimental.mcp.client.util.Assert; |
| 13 | + |
| 14 | +/** |
| 15 | + * Implementation of the MCP client session. |
| 16 | + * |
| 17 | + * @author Christian Tzolov |
| 18 | + * @author Dariusz Jędrzejczyk |
| 19 | + */ |
| 20 | +public class DefaultMcpSession implements McpSession { |
| 21 | + |
| 22 | + private final ConcurrentHashMap<Object, MonoSink<McpSchema.JSONRPCResponse>> pendingResponses = new ConcurrentHashMap<>(); |
| 23 | + |
| 24 | + private final Duration requestTimeout; |
| 25 | + |
| 26 | + private final ObjectMapper objectMapper; |
| 27 | + |
| 28 | + private final McpTransport transport; |
| 29 | + |
| 30 | + public DefaultMcpSession(Duration requestTimeout, ObjectMapper objectMapper, McpTransport transport) { |
| 31 | + |
| 32 | + Assert.notNull(objectMapper, "The ObjectMapper can not be null"); |
| 33 | + Assert.notNull(requestTimeout, "The requstTimeout can not be null"); |
| 34 | + Assert.notNull(transport, "The transport can not be null"); |
| 35 | + |
| 36 | + this.requestTimeout = requestTimeout; |
| 37 | + this.objectMapper = objectMapper; |
| 38 | + this.transport = transport; |
| 39 | + |
| 40 | + this.transport.setInboudMessageHandler(message -> { |
| 41 | + switch (message) { |
| 42 | + case McpSchema.JSONRPCResponse response -> { |
| 43 | + var sink = pendingResponses.remove(response.id()); |
| 44 | + if (sink == null) { |
| 45 | + System.out.println("Unexpected response for unkown id " + response.id()); |
| 46 | + } else { |
| 47 | + sink.success(response); |
| 48 | + } |
| 49 | + } |
| 50 | + case McpSchema.JSONRPCRequest request -> { |
| 51 | + System.out.println("Client does not yet support server requests"); |
| 52 | + } |
| 53 | + case McpSchema.JSONRPCNotification notification -> { |
| 54 | + System.out.println("Notifications not yet supported"); |
| 55 | + } |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + this.transport.setInboundErrorHandler(error -> System.out.println("Error received " + error)); |
| 60 | + |
| 61 | + this.transport.start(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public <T> Mono<T> sendRequest(String method, Object requestParams, TypeReference<T> typeRef) { |
| 66 | + String requestId = UUID.randomUUID().toString(); |
| 67 | + |
| 68 | + return Mono.<McpSchema.JSONRPCResponse>create(sink -> { |
| 69 | + this.pendingResponses.put(requestId, sink); |
| 70 | + McpSchema.JSONRPCRequest jsonrpcRequest = new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION, method, |
| 71 | + requestId, requestParams); |
| 72 | + try { |
| 73 | + // TODO: This is non-blocking, but it's actually a synchronous call, |
| 74 | + // perhaps there's no need to make it return Mono? |
| 75 | + this.transport.sendMessage(jsonrpcRequest) |
| 76 | + // TODO: It's most efficient to create a dedicated |
| 77 | + // Subscriber here |
| 78 | + .subscribe(v -> { |
| 79 | + }, e -> { |
| 80 | + this.pendingResponses.remove(requestId); |
| 81 | + sink.error(e); |
| 82 | + }); |
| 83 | + } catch (Exception e) { |
| 84 | + sink.error(e); |
| 85 | + } |
| 86 | + }).timeout(this.requestTimeout).handle((jsonRpcResponse, s) -> { |
| 87 | + if (jsonRpcResponse.error() != null) { |
| 88 | + s.error(new McpError(jsonRpcResponse.error())); |
| 89 | + } else { |
| 90 | + if (typeRef.getType().getTypeName().equals("java.lang.Void")) { |
| 91 | + s.complete(); |
| 92 | + } else { |
| 93 | + s.next(this.objectMapper.convertValue(jsonRpcResponse.result(), typeRef)); |
| 94 | + } |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Mono<Void> sendNotification(String method, Map<String, Object> params) { |
| 101 | + McpSchema.JSONRPCNotification jsonrpcNotification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION, |
| 102 | + method, params); |
| 103 | + try { |
| 104 | + this.transport.sendMessage(jsonrpcNotification); |
| 105 | + } catch (Exception e) { |
| 106 | + return Mono.error(new McpError(e)); |
| 107 | + } |
| 108 | + return Mono.empty(); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public Mono<Void> closeGracefully(Duration timeout) { |
| 113 | + // TODO handle the timeout in transport |
| 114 | + return Mono.fromRunnable(this.transport::close); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments