|
| 1 | +/* |
| 2 | + * Copyright 2024-2024 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.server; |
| 6 | + |
| 7 | +import java.time.Duration; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import io.modelcontextprotocol.MockMcpServerTransport; |
| 11 | +import io.modelcontextprotocol.MockMcpServerTransportProvider; |
| 12 | +import io.modelcontextprotocol.spec.McpSchema; |
| 13 | +import io.modelcontextprotocol.spec.McpSchema.ReadResourceResult; |
| 14 | +import io.modelcontextprotocol.spec.McpSchema.ServerCapabilities; |
| 15 | +import io.modelcontextprotocol.spec.ProtocolVersions; |
| 16 | +import org.junit.jupiter.api.AfterEach; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import reactor.core.publisher.Mono; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 23 | + |
| 24 | +/** |
| 25 | + * Tests for resource subscribe/unsubscribe support in {@link McpAsyncServer}. |
| 26 | + * |
| 27 | + * Covers the handlers registered under {@code resources/subscribe} and |
| 28 | + * {@code resources/unsubscribe}, the capability guard that keeps them absent when |
| 29 | + * {@code subscribe=false}, and the {@code notifications/resources/updated} notification |
| 30 | + * emitted by {@link McpAsyncServer#addResource} when a subscribed URI is (re-)added. |
| 31 | + */ |
| 32 | +class ResourceSubscriptionTests { |
| 33 | + |
| 34 | + private static final String RESOURCE_URI = "test://resource/item"; |
| 35 | + |
| 36 | + private MockMcpServerTransport mockTransport; |
| 37 | + |
| 38 | + private MockMcpServerTransportProvider mockTransportProvider; |
| 39 | + |
| 40 | + private McpAsyncServer mcpAsyncServer; |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setUp() { |
| 44 | + mockTransport = new MockMcpServerTransport(); |
| 45 | + mockTransportProvider = new MockMcpServerTransportProvider(mockTransport); |
| 46 | + } |
| 47 | + |
| 48 | + @AfterEach |
| 49 | + void tearDown() { |
| 50 | + if (mcpAsyncServer != null) { |
| 51 | + assertThatCode(() -> mcpAsyncServer.closeGracefully().block(Duration.ofSeconds(10))) |
| 52 | + .doesNotThrowAnyException(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // ------------------------------------------------------------------------- |
| 57 | + // Capability guard |
| 58 | + // ------------------------------------------------------------------------- |
| 59 | + |
| 60 | + @Test |
| 61 | + void subscribeHandlerNotRegisteredWhenSubscribeCapabilityDisabled() { |
| 62 | + mcpAsyncServer = McpServer.async(mockTransportProvider) |
| 63 | + .serverInfo("test-server", "1.0.0") |
| 64 | + .capabilities(ServerCapabilities.builder().resources(false, false).build()) |
| 65 | + .build(); |
| 66 | + |
| 67 | + initializeSession(); |
| 68 | + mockTransportProvider.simulateIncomingMessage(subscribeRequest("req-1", RESOURCE_URI)); |
| 69 | + |
| 70 | + McpSchema.JSONRPCMessage response = mockTransport.getLastSentMessage(); |
| 71 | + assertThat(response).isInstanceOf(McpSchema.JSONRPCResponse.class); |
| 72 | + McpSchema.JSONRPCResponse jsonResponse = (McpSchema.JSONRPCResponse) response; |
| 73 | + assertThat(jsonResponse.error()).isNotNull(); |
| 74 | + assertThat(jsonResponse.error().code()).isEqualTo(McpSchema.ErrorCodes.METHOD_NOT_FOUND); |
| 75 | + } |
| 76 | + |
| 77 | + // ------------------------------------------------------------------------- |
| 78 | + // Subscribe / unsubscribe happy paths |
| 79 | + // ------------------------------------------------------------------------- |
| 80 | + |
| 81 | + @Test |
| 82 | + void subscribeRequestReturnsEmptyResult() { |
| 83 | + mcpAsyncServer = McpServer.async(mockTransportProvider) |
| 84 | + .serverInfo("test-server", "1.0.0") |
| 85 | + .capabilities(ServerCapabilities.builder().resources(true, false).build()) |
| 86 | + .build(); |
| 87 | + |
| 88 | + initializeSession(); |
| 89 | + mockTransportProvider.simulateIncomingMessage(subscribeRequest("req-1", RESOURCE_URI)); |
| 90 | + |
| 91 | + McpSchema.JSONRPCMessage response = mockTransport.getLastSentMessage(); |
| 92 | + assertThat(response).isInstanceOf(McpSchema.JSONRPCResponse.class); |
| 93 | + McpSchema.JSONRPCResponse jsonResponse = (McpSchema.JSONRPCResponse) response; |
| 94 | + assertThat(jsonResponse.id()).isEqualTo("req-1"); |
| 95 | + assertThat(jsonResponse.error()).isNull(); |
| 96 | + } |
| 97 | + |
| 98 | + // ------------------------------------------------------------------------- |
| 99 | + // addResource notification behaviour |
| 100 | + // ------------------------------------------------------------------------- |
| 101 | + |
| 102 | + @Test |
| 103 | + void addSubscribedResourceSendsResourcesUpdatedNotification() { |
| 104 | + mcpAsyncServer = McpServer.async(mockTransportProvider) |
| 105 | + .serverInfo("test-server", "1.0.0") |
| 106 | + .capabilities(ServerCapabilities.builder().resources(true, false).build()) |
| 107 | + .build(); |
| 108 | + |
| 109 | + initializeSession(); |
| 110 | + // Client subscribes first |
| 111 | + mockTransportProvider.simulateIncomingMessage(subscribeRequest("req-1", RESOURCE_URI)); |
| 112 | + mockTransport.clearSentMessages(); |
| 113 | + |
| 114 | + mcpAsyncServer.addResource(resourceSpec(RESOURCE_URI)).block(Duration.ofSeconds(5)); |
| 115 | + |
| 116 | + List<McpSchema.JSONRPCMessage> sent = mockTransport.getAllSentMessages(); |
| 117 | + assertThat(sent).hasSize(1); |
| 118 | + assertThat(sent.get(0)).isInstanceOf(McpSchema.JSONRPCNotification.class); |
| 119 | + McpSchema.JSONRPCNotification notification = (McpSchema.JSONRPCNotification) sent.get(0); |
| 120 | + assertThat(notification.method()).isEqualTo(McpSchema.METHOD_NOTIFICATION_RESOURCES_UPDATED); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void addSubscribedResourceWithListChangedSendsBothNotifications() { |
| 125 | + mcpAsyncServer = McpServer.async(mockTransportProvider) |
| 126 | + .serverInfo("test-server", "1.0.0") |
| 127 | + .capabilities(ServerCapabilities.builder().resources(true, true).build()) |
| 128 | + .build(); |
| 129 | + |
| 130 | + initializeSession(); |
| 131 | + mockTransportProvider.simulateIncomingMessage(subscribeRequest("req-1", RESOURCE_URI)); |
| 132 | + mockTransport.clearSentMessages(); |
| 133 | + |
| 134 | + mcpAsyncServer.addResource(resourceSpec(RESOURCE_URI)).block(Duration.ofSeconds(5)); |
| 135 | + |
| 136 | + List<McpSchema.JSONRPCMessage> sent = mockTransport.getAllSentMessages(); |
| 137 | + assertThat(sent).hasSize(2); |
| 138 | + |
| 139 | + List<String> methods = sent.stream() |
| 140 | + .filter(m -> m instanceof McpSchema.JSONRPCNotification) |
| 141 | + .map(m -> ((McpSchema.JSONRPCNotification) m).method()) |
| 142 | + .toList(); |
| 143 | + assertThat(methods).containsExactly(McpSchema.METHOD_NOTIFICATION_RESOURCES_UPDATED, |
| 144 | + McpSchema.METHOD_NOTIFICATION_RESOURCES_LIST_CHANGED); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void addUnsubscribedResourceDoesNotSendResourcesUpdatedNotification() { |
| 149 | + mcpAsyncServer = McpServer.async(mockTransportProvider) |
| 150 | + .serverInfo("test-server", "1.0.0") |
| 151 | + .capabilities(ServerCapabilities.builder().resources(true, true).build()) |
| 152 | + .build(); |
| 153 | + |
| 154 | + initializeSession(); |
| 155 | + // No subscribe call — resource URI is not in the subscription map |
| 156 | + mcpAsyncServer.addResource(resourceSpec(RESOURCE_URI)).block(Duration.ofSeconds(5)); |
| 157 | + |
| 158 | + List<McpSchema.JSONRPCMessage> sent = mockTransport.getAllSentMessages(); |
| 159 | + assertThat(sent).hasSize(1); |
| 160 | + McpSchema.JSONRPCNotification notification = (McpSchema.JSONRPCNotification) sent.get(0); |
| 161 | + assertThat(notification.method()).isEqualTo(McpSchema.METHOD_NOTIFICATION_RESOURCES_LIST_CHANGED); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + void addResourceAfterUnsubscribeDoesNotSendResourcesUpdatedNotification() { |
| 166 | + mcpAsyncServer = McpServer.async(mockTransportProvider) |
| 167 | + .serverInfo("test-server", "1.0.0") |
| 168 | + .capabilities(ServerCapabilities.builder().resources(true, false).build()) |
| 169 | + .build(); |
| 170 | + |
| 171 | + initializeSession(); |
| 172 | + mockTransportProvider.simulateIncomingMessage(subscribeRequest("req-1", RESOURCE_URI)); |
| 173 | + mockTransportProvider.simulateIncomingMessage(unsubscribeRequest("req-2", RESOURCE_URI)); |
| 174 | + mockTransport.clearSentMessages(); |
| 175 | + |
| 176 | + mcpAsyncServer.addResource(resourceSpec(RESOURCE_URI)).block(Duration.ofSeconds(5)); |
| 177 | + |
| 178 | + // No notifications expected: not subscribed, listChanged=false |
| 179 | + assertThat(mockTransport.getAllSentMessages()).isEmpty(); |
| 180 | + } |
| 181 | + |
| 182 | + // ------------------------------------------------------------------------- |
| 183 | + // Helpers |
| 184 | + // ------------------------------------------------------------------------- |
| 185 | + |
| 186 | + /** |
| 187 | + * Performs the MCP initialization handshake so that the session's exchangeSink is |
| 188 | + * populated and subsequent request handlers can be invoked. |
| 189 | + */ |
| 190 | + private void initializeSession() { |
| 191 | + mockTransportProvider.simulateIncomingMessage(new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION, |
| 192 | + McpSchema.METHOD_INITIALIZE, "init-req", new McpSchema.InitializeRequest( |
| 193 | + ProtocolVersions.MCP_2025_11_25, null, new McpSchema.Implementation("test-client", "1.0.0")))); |
| 194 | + |
| 195 | + mockTransportProvider.simulateIncomingMessage(new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION, |
| 196 | + McpSchema.METHOD_NOTIFICATION_INITIALIZED, null)); |
| 197 | + |
| 198 | + mockTransport.clearSentMessages(); |
| 199 | + } |
| 200 | + |
| 201 | + private static McpSchema.JSONRPCRequest subscribeRequest(String id, String uri) { |
| 202 | + return new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION, McpSchema.METHOD_RESOURCES_SUBSCRIBE, id, |
| 203 | + new McpSchema.SubscribeRequest(uri)); |
| 204 | + } |
| 205 | + |
| 206 | + private static McpSchema.JSONRPCRequest unsubscribeRequest(String id, String uri) { |
| 207 | + return new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION, McpSchema.METHOD_RESOURCES_UNSUBSCRIBE, id, |
| 208 | + new McpSchema.SubscribeRequest(uri)); |
| 209 | + } |
| 210 | + |
| 211 | + private static McpServerFeatures.AsyncResourceSpecification resourceSpec(String uri) { |
| 212 | + McpSchema.Resource resource = McpSchema.Resource.builder().uri(uri).name("test-resource").build(); |
| 213 | + return new McpServerFeatures.AsyncResourceSpecification(resource, |
| 214 | + (exchange, req) -> Mono.just(new ReadResourceResult(List.of()))); |
| 215 | + } |
| 216 | + |
| 217 | +} |
0 commit comments