|
| 1 | +package spring.ai.mcp.client; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.util.concurrent.CompletableFuture; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import reactor.core.publisher.Mono; |
| 9 | +import spring.ai.mcp.spec.McpSchema; |
| 10 | +import spring.ai.mcp.spec.McpSession; |
| 11 | +import spring.ai.mcp.spec.McpTransport; |
| 12 | + |
| 13 | +public class McpAsyncClient extends McpAsyncSession { |
| 14 | + |
| 15 | + public McpAsyncClient(McpTransport transport) { |
| 16 | + this(transport, Duration.ofSeconds(10), new ObjectMapper()); |
| 17 | + } |
| 18 | + |
| 19 | + public McpAsyncClient(McpTransport transport, Duration requestTimeout, ObjectMapper objectMapper) { |
| 20 | + super(requestTimeout, objectMapper, transport); |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + /** |
| 25 | + * The initialization phase MUST be the first interaction between client and server. |
| 26 | + * During this phase, the client and server: |
| 27 | + * <ul> |
| 28 | + * <li>Establish protocol version compatibility</li> |
| 29 | + * <li>Exchange and negotiate capabilities</li> |
| 30 | + * <li>Share implementation details</li> |
| 31 | + * </ul> |
| 32 | + * <br/> |
| 33 | + * The client MUST initiate this phase by sending an initialize request containing: |
| 34 | + * <ul> |
| 35 | + * <li>The protocol version the client supports</li> |
| 36 | + * <li>The client's capabilities</li> |
| 37 | + * <li>Client implementation information</li> |
| 38 | + * </ul> |
| 39 | + * |
| 40 | + * The server MUST respond with its own capabilities and information: |
| 41 | + * {@link McpSchema.ServerCapabilities}. <br/> |
| 42 | + * After successful initialization, the client MUST send an initialized notification |
| 43 | + * to indicate it is ready to begin normal operations. |
| 44 | + * |
| 45 | + * <br/> |
| 46 | + * |
| 47 | + * <a href= |
| 48 | + * "https://github.com/modelcontextprotocol/specification/blob/main/docs/specification/basic/lifecycle.md#initialization">Initialization |
| 49 | + * Spec</a> |
| 50 | + * @return the initialize result. |
| 51 | + */ |
| 52 | + public Mono<McpSchema.InitializeResult> initialize() { |
| 53 | + McpSchema.InitializeRequest initializeRequest = new McpSchema.InitializeRequest(// @formatter:off |
| 54 | + McpSchema.LATEST_PROTOCOL_VERSION, |
| 55 | + new McpSchema.ClientCapabilities(null, new McpSchema.ClientCapabilities.RootCapabilities(true), null), |
| 56 | + new McpSchema.Implementation("mcp-java-client", "0.0.1")); // @formatter:on |
| 57 | + |
| 58 | + Mono<McpSchema.InitializeResult> result = |
| 59 | + this.sendRequest("initialize", initializeRequest, new TypeReference<McpSchema.InitializeResult>() {}); |
| 60 | + |
| 61 | + return result.flatMap(initializeResult -> { |
| 62 | + if (initializeResult.protocolVersion() != McpSchema.LATEST_PROTOCOL_VERSION) { |
| 63 | + return Mono.error( |
| 64 | + new McpError("Unsupported protocol version from the server: " |
| 65 | + + initializeResult.protocolVersion())); |
| 66 | + } else { |
| 67 | + return this.sendNotification("notifications/initialized", null) |
| 68 | + .thenReturn(initializeResult); |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Send a roots/list_changed notification. |
| 75 | + */ |
| 76 | + public Mono<Void> sendRootsListChanged() { |
| 77 | + return this.sendNotification("notifications/roots/list_changed"); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Send a synchronous ping request. |
| 82 | + */ |
| 83 | + public Mono<Void> ping() { |
| 84 | + return this.sendRequest("ping", null, |
| 85 | + new TypeReference<Void>() {}); |
| 86 | + } |
| 87 | + |
| 88 | + // -------------------------- |
| 89 | + // Tools |
| 90 | + // -------------------------- |
| 91 | + /** |
| 92 | + * Send a tools/call request. |
| 93 | + * @param callToolRequest the call tool request. |
| 94 | + * @return the call tool result. |
| 95 | + */ |
| 96 | + public Mono<McpSchema.CallToolResult> callTool(McpSchema.CallToolRequest callToolRequest) { |
| 97 | + return this.sendRequest("tools/call", callToolRequest, |
| 98 | + new TypeReference<McpSchema.CallToolResult>() {}); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Send a tools/list request. |
| 103 | + * @return the list of tools result. |
| 104 | + */ |
| 105 | + public Mono<McpSchema.ListToolsResult> listTools(String cursor) { |
| 106 | + return this.sendRequest("tools/list", new McpSchema.PaginatedRequest(cursor), |
| 107 | + new TypeReference<McpSchema.ListToolsResult>() {}); |
| 108 | + } |
| 109 | + |
| 110 | + // -------------------------- |
| 111 | + // Resources |
| 112 | + // -------------------------- |
| 113 | + |
| 114 | + /** |
| 115 | + * Send a resources/list request. |
| 116 | + * @param cursor the cursor |
| 117 | + * @return the list of resources result. |
| 118 | + */ |
| 119 | + public Mono<McpSchema.ListResourcesResult> listResources(String cursor) { |
| 120 | + return this.sendRequest("resources/list", new McpSchema.PaginatedRequest(cursor), |
| 121 | + new TypeReference<McpSchema.ListResourcesResult>() {}); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Send a resources/read request. |
| 126 | + * @param resource the resource to read |
| 127 | + * @return the resource content. |
| 128 | + */ |
| 129 | + public Mono<McpSchema.ReadResourceResult> readResource(McpSchema.Resource resource) { |
| 130 | + return this.readResource(new McpSchema.ReadResourceRequest(resource.uri())); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Send a resources/read request. |
| 135 | + * @param readResourceRequest the read resource request. |
| 136 | + * @return the resource content. |
| 137 | + */ |
| 138 | + public Mono<McpSchema.ReadResourceResult> readResource(McpSchema.ReadResourceRequest readResourceRequest) { |
| 139 | + return this.sendRequest("resources/read", readResourceRequest, |
| 140 | + new TypeReference<McpSchema.ReadResourceResult>() {}); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Resource templates allow servers to expose parameterized resources using URI |
| 145 | + * templates. Arguments may be auto-completed through the completion API. |
| 146 | + * |
| 147 | + * Request a list of resource templates the server has. |
| 148 | + * @param cursor the cursor |
| 149 | + * @return the list of resource templates result. |
| 150 | + */ |
| 151 | + public Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates(String cursor) { |
| 152 | + return this.sendRequest("resources/templates/list", new McpSchema.PaginatedRequest(cursor), |
| 153 | + new TypeReference<McpSchema.ListResourceTemplatesResult>() {}); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * List Changed Notification. When the list of available resources changes, servers |
| 158 | + * that declared the listChanged capability SHOULD send a notification: |
| 159 | + */ |
| 160 | + public Mono<Void> sendResourcesListChanged() { |
| 161 | + return this.sendNotification("notifications/resources/list_changed"); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Subscriptions. The protocol supports optional subscriptions to resource changes. |
| 166 | + * Clients can subscribe to specific resources and receive notifications when they |
| 167 | + * change. |
| 168 | + * |
| 169 | + * Send a resources/subscribe request. |
| 170 | + * @param subscribeRequest the subscribe request contains the uri of the resource to |
| 171 | + * subscribe to. |
| 172 | + */ |
| 173 | + public Mono<Void> subscribeResource(McpSchema.SubscribeRequest subscribeRequest) { |
| 174 | + return this.sendRequest("resources/subscribe", subscribeRequest, |
| 175 | + new TypeReference<Void>() {}); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Send a resources/unsubscribe request. |
| 180 | + * @param unsubscribeRequest the unsubscribe request contains the uri of the resource |
| 181 | + * to unsubscribe from. |
| 182 | + */ |
| 183 | + public Mono<Void> unsubscribeResource(McpSchema.UnsubscribeRequest unsubscribeRequest) { |
| 184 | + return this.sendRequest("resources/unsubscribe", unsubscribeRequest, |
| 185 | + new TypeReference<Void>() {}); |
| 186 | + } |
| 187 | +} |
0 commit comments