Skip to content

Commit 1702ef3

Browse files
committed
Use InMemory* repositories in tests too
1 parent 224c5b2 commit 1702ef3

16 files changed

Lines changed: 156 additions & 364 deletions

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/feature/repository/InMemoryAsyncPromptRepository.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import java.util.ArrayList;
88
import java.util.List;
9-
import java.util.Map;
9+
import java.util.NavigableMap;
1010
import java.util.concurrent.ConcurrentSkipListMap;
1111

1212
import io.modelcontextprotocol.modern.McpSchema.Prompt;
@@ -39,7 +39,7 @@
3939
*/
4040
public final class InMemoryAsyncPromptRepository implements McpAsyncPromptRepository {
4141

42-
private final Map<String, McpAsyncPrompt> prompts = new ConcurrentSkipListMap<>();
42+
private final NavigableMap<String, McpAsyncPrompt> prompts = new ConcurrentSkipListMap<>();
4343

4444
private final McpChangeBroadcaster changeBroadcaster;
4545

@@ -92,8 +92,8 @@ public Mono<McpAsyncPrompt> find(McpRequestContext ctx, String name) {
9292
}
9393

9494
private PromptsPage page(String cursor) {
95-
KeyCursorPagination.Page<Prompt> page = KeyCursorPagination.page(this.prompts.values(),
96-
prompt -> prompt.definition().name(), McpAsyncPrompt::definition, cursor, this.pageSize);
95+
KeyCursorPagination.Page<Prompt> page = KeyCursorPagination.page(this.prompts, McpAsyncPrompt::definition,
96+
cursor, this.pageSize);
9797
return new PromptsPage(page.items(), page.nextCursor(), null, null);
9898
}
9999

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/feature/repository/InMemoryAsyncResourceRepository.java‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import java.util.ArrayList;
88
import java.util.List;
9-
import java.util.Map;
9+
import java.util.NavigableMap;
1010
import java.util.concurrent.ConcurrentSkipListMap;
1111

1212
import io.modelcontextprotocol.modern.McpSchema.Resource;
@@ -52,9 +52,9 @@
5252
*/
5353
public final class InMemoryAsyncResourceRepository implements McpAsyncResourceRepository {
5454

55-
private final Map<String, StaticResource> resources = new ConcurrentSkipListMap<>();
55+
private final NavigableMap<String, StaticResource> resources = new ConcurrentSkipListMap<>();
5656

57-
private final Map<String, TemplatedResource> templates = new ConcurrentSkipListMap<>();
57+
private final NavigableMap<String, TemplatedResource> templates = new ConcurrentSkipListMap<>();
5858

5959
private final McpUriTemplateManagerFactory uriTemplateManagerFactory;
6060

@@ -164,14 +164,13 @@ public boolean supportsCompletion() {
164164
}
165165

166166
private ResourcesPage page(String cursor) {
167-
KeyCursorPagination.Page<Resource> page = KeyCursorPagination.page(this.resources.values(),
168-
resource -> resource.definition().uri(), StaticResource::definition, cursor, this.pageSize);
167+
KeyCursorPagination.Page<Resource> page = KeyCursorPagination.page(this.resources, StaticResource::definition,
168+
cursor, this.pageSize);
169169
return new ResourcesPage(page.items(), page.nextCursor(), null, null);
170170
}
171171

172172
private ResourceTemplatesPage templatesPage(String cursor) {
173-
KeyCursorPagination.Page<ResourceTemplate> page = KeyCursorPagination.page(this.templates.values(),
174-
template -> template.template().definition().uriTemplate(),
173+
KeyCursorPagination.Page<ResourceTemplate> page = KeyCursorPagination.page(this.templates,
175174
template -> template.template().definition(), cursor, this.pageSize);
176175
return new ResourceTemplatesPage(page.items(), page.nextCursor(), null, null);
177176
}

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/feature/repository/InMemoryAsyncToolRepository.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import java.util.ArrayList;
88
import java.util.List;
9-
import java.util.Map;
9+
import java.util.NavigableMap;
1010
import java.util.concurrent.ConcurrentSkipListMap;
1111

1212
import io.modelcontextprotocol.modern.McpSchema.Tool;
@@ -33,7 +33,7 @@
3333
*/
3434
public final class InMemoryAsyncToolRepository implements McpAsyncToolRepository {
3535

36-
private final Map<String, McpAsyncTool> tools = new ConcurrentSkipListMap<>();
36+
private final NavigableMap<String, McpAsyncTool> tools = new ConcurrentSkipListMap<>();
3737

3838
private final McpChangeBroadcaster changeBroadcaster;
3939

@@ -83,8 +83,8 @@ public Mono<McpAsyncTool> find(McpRequestContext ctx, String name) {
8383
}
8484

8585
private ToolsPage page(String cursor) {
86-
KeyCursorPagination.Page<Tool> page = KeyCursorPagination.page(this.tools.values(),
87-
tool -> tool.definition().name(), McpAsyncTool::definition, cursor, this.pageSize);
86+
KeyCursorPagination.Page<Tool> page = KeyCursorPagination.page(this.tools, McpAsyncTool::definition, cursor,
87+
this.pageSize);
8888
return new ToolsPage(page.items(), page.nextCursor(), null, null);
8989
}
9090

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/feature/repository/InMemorySyncPromptRepository.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import java.util.ArrayList;
88
import java.util.List;
9-
import java.util.Map;
9+
import java.util.NavigableMap;
1010
import java.util.concurrent.ConcurrentSkipListMap;
1111

1212
import io.modelcontextprotocol.modern.McpSchema.Prompt;
@@ -35,7 +35,7 @@
3535
*/
3636
public final class InMemorySyncPromptRepository implements McpSyncPromptRepository {
3737

38-
private final Map<String, McpSyncPrompt> prompts = new ConcurrentSkipListMap<>();
38+
private final NavigableMap<String, McpSyncPrompt> prompts = new ConcurrentSkipListMap<>();
3939

4040
private final McpChangeBroadcaster changeBroadcaster;
4141

@@ -79,8 +79,8 @@ public boolean remove(String name) {
7979

8080
@Override
8181
public PromptsPage list(McpRequestContext ctx, String cursor) {
82-
KeyCursorPagination.Page<Prompt> page = KeyCursorPagination.page(this.prompts.values(),
83-
prompt -> prompt.definition().name(), McpSyncPrompt::definition, cursor, this.pageSize);
82+
KeyCursorPagination.Page<Prompt> page = KeyCursorPagination.page(this.prompts, McpSyncPrompt::definition,
83+
cursor, this.pageSize);
8484
return new PromptsPage(page.items(), page.nextCursor(), null, null);
8585
}
8686

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/feature/repository/InMemorySyncResourceRepository.java‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import java.util.ArrayList;
88
import java.util.List;
9-
import java.util.Map;
9+
import java.util.NavigableMap;
1010
import java.util.concurrent.ConcurrentSkipListMap;
1111

1212
import io.modelcontextprotocol.modern.McpSchema.Resource;
@@ -48,9 +48,9 @@
4848
*/
4949
public final class InMemorySyncResourceRepository implements McpSyncResourceRepository {
5050

51-
private final Map<String, StaticResource> resources = new ConcurrentSkipListMap<>();
51+
private final NavigableMap<String, StaticResource> resources = new ConcurrentSkipListMap<>();
5252

53-
private final Map<String, TemplatedResource> templates = new ConcurrentSkipListMap<>();
53+
private final NavigableMap<String, TemplatedResource> templates = new ConcurrentSkipListMap<>();
5454

5555
private final McpUriTemplateManagerFactory uriTemplateManagerFactory;
5656

@@ -131,15 +131,14 @@ public void resourceUpdated(String uri) {
131131

132132
@Override
133133
public ResourcesPage list(McpRequestContext ctx, String cursor) {
134-
KeyCursorPagination.Page<Resource> page = KeyCursorPagination.page(this.resources.values(),
135-
resource -> resource.definition().uri(), StaticResource::definition, cursor, this.pageSize);
134+
KeyCursorPagination.Page<Resource> page = KeyCursorPagination.page(this.resources, StaticResource::definition,
135+
cursor, this.pageSize);
136136
return new ResourcesPage(page.items(), page.nextCursor(), null, null);
137137
}
138138

139139
@Override
140140
public ResourceTemplatesPage listTemplates(McpRequestContext ctx, String cursor) {
141-
KeyCursorPagination.Page<ResourceTemplate> page = KeyCursorPagination.page(this.templates.values(),
142-
template -> template.template().definition().uriTemplate(),
141+
KeyCursorPagination.Page<ResourceTemplate> page = KeyCursorPagination.page(this.templates,
143142
template -> template.template().definition(), cursor, this.pageSize);
144143
return new ResourceTemplatesPage(page.items(), page.nextCursor(), null, null);
145144
}

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/feature/repository/InMemorySyncToolRepository.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import java.util.ArrayList;
88
import java.util.List;
9-
import java.util.Map;
9+
import java.util.NavigableMap;
1010
import java.util.concurrent.ConcurrentSkipListMap;
1111

1212
import io.modelcontextprotocol.modern.McpSchema.Tool;
@@ -29,7 +29,7 @@
2929
*/
3030
public final class InMemorySyncToolRepository implements McpSyncToolRepository {
3131

32-
private final Map<String, McpSyncTool> tools = new ConcurrentSkipListMap<>();
32+
private final NavigableMap<String, McpSyncTool> tools = new ConcurrentSkipListMap<>();
3333

3434
private final McpChangeBroadcaster changeBroadcaster;
3535

@@ -70,8 +70,8 @@ public boolean remove(String name) {
7070

7171
@Override
7272
public ToolsPage list(McpRequestContext ctx, String cursor) {
73-
KeyCursorPagination.Page<Tool> page = KeyCursorPagination.page(this.tools.values(),
74-
tool -> tool.definition().name(), McpSyncTool::definition, cursor, this.pageSize);
73+
KeyCursorPagination.Page<Tool> page = KeyCursorPagination.page(this.tools, McpSyncTool::definition, cursor,
74+
this.pageSize);
7575
return new ToolsPage(page.items(), page.nextCursor(), null, null);
7676
}
7777

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/feature/repository/KeyCursorPagination.java‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
import java.nio.charset.StandardCharsets;
88
import java.util.ArrayList;
99
import java.util.Base64;
10-
import java.util.Collection;
1110
import java.util.List;
11+
import java.util.Map;
12+
import java.util.NavigableMap;
1213
import java.util.function.Function;
1314

1415
import io.modelcontextprotocol.modern.McpException;
1516

1617
/**
17-
* Pages entries that are iterated in ascending key order. The cursor is the opaque,
18+
* Pages the entries of a sorted map, in ascending key order. The cursor is the opaque,
1819
* encoded key of the last entry of the previous page, so a page stays correct when
1920
* entries are added or removed between two requests: the next page starts at the first
2021
* key after the cursor, whether or not that key still exists.
@@ -30,33 +31,32 @@ private KeyCursorPagination() {
3031
}
3132

3233
/**
33-
* Returns the page of {@code entries} following {@code cursor}, with every entry
34+
* Returns the page of {@code entries} following {@code cursor}, with every value
3435
* mapped through {@code item}.
35-
* @param entries the entries in ascending {@code key} order
36+
* <p>
37+
* The page starts with a {@code tailMap} seek to the cursor key rather than a scan
38+
* from the first entry, so walking a list of {@code n} entries page by page costs
39+
* O(n) overall instead of O(n²/pageSize).
40+
* @param entries the entries, keyed by the value the cursor encodes
3641
* @param pageSize {@code null} returns everything on one page; any cursor is then
3742
* invalid, since this method never produced one
3843
* @throws McpException invalid params when the cursor is not one this method produced
3944
*/
40-
static <S, D> Page<D> page(Collection<S> entries, Function<S, String> key, Function<S, D> item, String cursor,
41-
Integer pageSize) {
45+
static <S, D> Page<D> page(NavigableMap<String, S> entries, Function<S, D> item, String cursor, Integer pageSize) {
4246
if (pageSize == null && cursor != null) {
4347
throw invalidCursor(cursor);
4448
}
45-
String after = cursor != null ? decode(cursor) : null;
49+
NavigableMap<String, S> remaining = cursor != null ? entries.tailMap(decode(cursor), false) : entries;
4650
List<D> items = new ArrayList<>();
4751
String last = null;
4852
boolean more = false;
49-
for (S entry : entries) {
50-
String entryKey = key.apply(entry);
51-
if (after != null && entryKey.compareTo(after) <= 0) {
52-
continue;
53-
}
53+
for (Map.Entry<String, S> entry : remaining.entrySet()) {
5454
if (pageSize != null && items.size() == pageSize) {
5555
more = true;
5656
break;
5757
}
58-
items.add(item.apply(entry));
59-
last = entryKey;
58+
items.add(item.apply(entry.getValue()));
59+
last = entry.getKey();
6060
}
6161
return new Page<>(List.copyOf(items), more ? encode(last) : null);
6262
}

‎mcp-test/src/test/java/io/modelcontextprotocol/modern/server/McpServerMrtrTests.java‎

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
import io.modelcontextprotocol.modern.McpSchema.InputRequiredResult;
2424
import io.modelcontextprotocol.modern.McpSchema.MetaKeys;
2525
import io.modelcontextprotocol.modern.McpSchema.ReadResourceResult;
26+
import io.modelcontextprotocol.modern.McpSchema.Resource;
2627
import io.modelcontextprotocol.modern.McpSchema.Result;
2728
import io.modelcontextprotocol.modern.McpSchema.TextContent;
2829
import io.modelcontextprotocol.modern.McpSchema.TextResourceContents;
2930
import io.modelcontextprotocol.modern.McpSchema;
3031
import io.modelcontextprotocol.modern.server.feature.McpAsyncResource;
3132
import io.modelcontextprotocol.modern.server.feature.McpAsyncResourceRepository;
3233
import io.modelcontextprotocol.modern.server.feature.ResourcesFeature;
33-
import io.modelcontextprotocol.modern.server.feature.ResourcesPage;
3434
import io.modelcontextprotocol.modern.server.feature.ToolsFeature;
35+
import io.modelcontextprotocol.modern.server.feature.repository.InMemoryAsyncResourceRepository;
3536
import org.junit.jupiter.api.Test;
3637
import reactor.core.publisher.Mono;
3738

@@ -216,22 +217,11 @@ void inputRequestsForDeclaredCapabilitiesAreSent() {
216217

217218
@Test
218219
void resourceInputRequestsAreCheckedToo() {
219-
McpAsyncResourceRepository repo = new McpAsyncResourceRepository() {
220-
@Override
221-
public Mono<ResourcesPage> list(McpRequestContext ctx, String cursor) {
222-
return Mono.just(ResourcesPage.of(List.of()));
223-
}
224-
225-
@Override
226-
public Mono<McpAsyncResource> find(McpRequestContext ctx, String uri) {
227-
return Mono.just(McpAsyncResource.of(this::read));
228-
}
229-
230-
private Mono<McpAsyncResponse<McpSchema.ReadResourceOutcome>> read(McpRequestContext ctx,
231-
McpSchema.ReadResourceRequest request) {
232-
return Mono.just(McpAsyncResponse.result(InputRequiredResult.builder().listRoots("q1").build()));
233-
}
234-
};
220+
McpAsyncResourceRepository repo = InMemoryAsyncResourceRepository.builder()
221+
.resource(Resource.builder("file:///a.txt", "a").build(),
222+
McpAsyncResource.of((ctx, request) -> Mono
223+
.just(McpAsyncResponse.result(InputRequiredResult.builder().listRoots("q1").build()))))
224+
.build();
235225
McpServer server = baseBuilder()
236226
.feature(ResourcesFeature.ofAsync(repo, McpJsonDefaults.getMapper(), 0L, CacheScope.PRIVATE))
237227
.build();
@@ -245,26 +235,14 @@ private Mono<McpAsyncResponse<McpSchema.ReadResourceOutcome>> read(McpRequestCon
245235

246236
@Test
247237
void retryResultIsMarkedUncacheable() {
248-
McpAsyncResourceRepository repo = new McpAsyncResourceRepository() {
249-
@Override
250-
public Mono<ResourcesPage> list(McpRequestContext ctx, String cursor) {
251-
return Mono.just(ResourcesPage.of(List.of()));
252-
}
253-
254-
@Override
255-
public Mono<McpAsyncResource> find(McpRequestContext ctx, String uri) {
256-
return Mono.just(McpAsyncResource.of(this::read));
257-
}
258-
259-
private Mono<McpAsyncResponse<McpSchema.ReadResourceOutcome>> read(McpRequestContext ctx,
260-
McpSchema.ReadResourceRequest request) {
261-
return Mono.just(McpAsyncResponse.result(ReadResourceResult
262-
.builder(List.of(new TextResourceContents(request.uri(), "text/plain", "content", null)))
263-
.ttlMs(60_000L)
264-
.cacheScope(CacheScope.PUBLIC)
265-
.build()));
266-
}
267-
};
238+
McpAsyncResourceRepository repo = InMemoryAsyncResourceRepository.builder()
239+
.resource(Resource.builder("file:///a.txt", "a").build(),
240+
McpAsyncResource.of((ctx, request) -> Mono.just(McpAsyncResponse.result(ReadResourceResult
241+
.builder(List.of(new TextResourceContents(request.uri(), "text/plain", "content", null)))
242+
.ttlMs(60_000L)
243+
.cacheScope(CacheScope.PUBLIC)
244+
.build()))))
245+
.build();
268246
McpServer server = baseBuilder()
269247
.feature(ResourcesFeature.ofAsync(repo, McpJsonDefaults.getMapper(), 0L, CacheScope.PRIVATE))
270248
.build();
@@ -285,27 +263,20 @@ private Mono<McpAsyncResponse<McpSchema.ReadResourceOutcome>> read(McpRequestCon
285263
@SuppressWarnings("unchecked")
286264
void resourceRequestStateIsBoundToTheUriNotAStrayName() {
287265
AtomicReference<String> seenRequestState = new AtomicReference<>();
288-
McpAsyncResourceRepository repo = new McpAsyncResourceRepository() {
289-
@Override
290-
public Mono<ResourcesPage> list(McpRequestContext ctx, String cursor) {
291-
return Mono.just(ResourcesPage.of(List.of()));
292-
}
293-
294-
@Override
295-
public Mono<McpAsyncResource> find(McpRequestContext ctx, String uri) {
296-
return Mono.just(McpAsyncResource.of(this::read));
297-
}
298-
299-
private Mono<McpAsyncResponse<McpSchema.ReadResourceOutcome>> read(McpRequestContext ctx,
300-
McpSchema.ReadResourceRequest request) {
301-
if (request.requestState() != null) {
302-
seenRequestState.set(request.requestState());
303-
return Mono.just(McpAsyncResponse.result(ReadResourceResult.builder(List.of()).build()));
304-
}
305-
return Mono.just(McpAsyncResponse
306-
.result(InputRequiredResult.builder().elicit("q1", CONFIRM).requestState("for-a").build()));
266+
McpAsyncResource reader = McpAsyncResource.of((ctx, request) -> {
267+
if (request.requestState() != null) {
268+
seenRequestState.set(request.requestState());
269+
return Mono.just(McpAsyncResponse.result(ReadResourceResult.builder(List.of()).build()));
307270
}
308-
};
271+
return Mono.just(McpAsyncResponse
272+
.result(InputRequiredResult.builder().elicit("q1", CONFIRM).requestState("for-a").build()));
273+
});
274+
// Both URIs exist, so the replay against b.txt is rejected for its requestState
275+
// being sealed for a.txt, not for naming an unknown resource.
276+
McpAsyncResourceRepository repo = InMemoryAsyncResourceRepository.builder()
277+
.resource(Resource.builder("file:///a.txt", "a").build(), reader)
278+
.resource(Resource.builder("file:///b.txt", "b").build(), reader)
279+
.build();
309280
McpServer server = baseBuilder()
310281
.feature(ResourcesFeature.ofAsync(repo, McpJsonDefaults.getMapper(), 0L, CacheScope.PRIVATE))
311282
.build();

0 commit comments

Comments
 (0)