Skip to content

Commit 2d82990

Browse files
committed
Add InMemory* implementation for modern server repositories
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
1 parent b539249 commit 2d82990

11 files changed

Lines changed: 2204 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Versioning follows [Semantic Versioning](https://semver.org/); see
2424
`server/discover`, multi round-trip requests with sealed `requestState`, `subscriptions/listen`, caching
2525
hints, required client capabilities and SEP-2243 `Mcp-Param` header validation. The API may change in minor
2626
releases until 3.0. See [MCP Server (2026-07-28)](docs/server-modern.md).
27+
- Add in-memory repositories for the 2026-07-28 server: `InMemorySyncToolRepository`,
28+
`InMemorySyncPromptRepository`, `InMemorySyncResourceRepository` and their async counterparts
29+
`InMemoryAsyncToolRepository`, `InMemoryAsyncPromptRepository` and `InMemoryAsyncResourceRepository`, with runtime add and remove, change broadcasts and paging. See [In-Memory Repositories](docs/server-modern.md#in-memory-repositories).
2730
- Add `McpTransportContext.putAll(Map)`, returning a context that combines an
2831
existing context with additional entries, which take precedence.
2932
- `McpTransportContext` no longer compares contexts by their contents in

‎docs/server-modern.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,50 @@ different catalog per tenant or user. Two rules apply to `list`:
243243

244244
To paginate, return a page with a `nextCursor`, for example `new ToolsPage(tools, nextCursor, null, null)`.
245245

246+
### In-Memory Repositories
247+
248+
For a fixed or runtime-managed catalog, the `io.modelcontextprotocol.modern.server.feature.repository` package
249+
implements the repositories for you: `InMemorySyncToolRepository`, `InMemorySyncPromptRepository` and
250+
`InMemorySyncResourceRepository`, and their async counterparts `InMemoryAsyncToolRepository`,
251+
`InMemoryAsyncPromptRepository` and `InMemoryAsyncResourceRepository`, which hold `McpAsyncTool`s, `McpAsyncPrompt`s
252+
and `McpAsyncResource`s and never block. They list entries sorted by name (or URI), page
253+
them when built with `pageSize(...)`, and support adding and removing entries at runtime. When
254+
built with a `changeBroadcaster(...)`, every add and remove is broadcast as the matching
255+
[change notification](#change-notifications):
256+
257+
```java
258+
McpChangeFeed changes = new McpChangeFeed();
259+
260+
InMemorySyncToolRepository tools = InMemorySyncToolRepository.builder()
261+
.tool(weather)
262+
.changeBroadcaster(changes)
263+
.build();
264+
265+
InMemorySyncResourceRepository resources = InMemorySyncResourceRepository.builder()
266+
.resource(Resource.builder("file:///config.json", "Configuration").build(), configReader)
267+
.template(McpSyncResourceTemplate.of(ResourceTemplate.builder("file:///logs/{date}", "Daily log").build()),
268+
logReader)
269+
.changeBroadcaster(changes)
270+
.supportsSubscribe(true)
271+
.build();
272+
273+
McpServer server = McpServer.builder()
274+
.serverInfo(serverInfo)
275+
.feature(ToolsFeature.ofSync(tools))
276+
.feature(ResourcesFeature.ofSync(resources))
277+
.subscriptions(changes)
278+
.build();
279+
280+
tools.add(forecast); // broadcasts ToolsListChanged
281+
resources.resourceUpdated("file:///config.json"); // broadcasts ResourceUpdated
282+
```
283+
284+
The resource repository pairs each resource and template with the `McpSyncResource` that reads it. A
285+
`resources/read` URI is served by the resource registered under exactly that URI, or else by the first
286+
template, in `uriTemplate` order, that matches it. The prompt and resource repositories serve
287+
[completions](#completions) from the completion handlers of their prompts and templates when built with
288+
`supportsCompletion(true)`.
289+
246290
### Tools
247291

248292
A repository lists tool definitions and finds a tool by name. `find` returns an `McpSyncTool`, which pairs a
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
* Copyright 2026-2026 the original author or authors.
3+
*/
4+
5+
package io.modelcontextprotocol.modern.server.feature.repository;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.concurrent.ConcurrentSkipListMap;
11+
12+
import io.modelcontextprotocol.modern.McpSchema.Prompt;
13+
import io.modelcontextprotocol.modern.server.McpRequestContext;
14+
import io.modelcontextprotocol.modern.server.feature.McpAsyncCompletionHandler;
15+
import io.modelcontextprotocol.modern.server.feature.McpAsyncPrompt;
16+
import io.modelcontextprotocol.modern.server.feature.McpAsyncPromptRepository;
17+
import io.modelcontextprotocol.modern.server.feature.McpChangeBroadcaster;
18+
import io.modelcontextprotocol.modern.server.feature.McpChangeFeed;
19+
import io.modelcontextprotocol.modern.server.feature.PromptsPage;
20+
import io.modelcontextprotocol.modern.server.feature.ServerChange;
21+
import io.modelcontextprotocol.util.Assert;
22+
import reactor.core.publisher.Mono;
23+
24+
/**
25+
* An {@link McpAsyncPromptRepository} that keeps {@link McpAsyncPrompt}s in memory, keyed
26+
* by name. Prompts can be added and removed at runtime; each change is broadcast as
27+
* {@link ServerChange.PromptsListChanged} when a change broadcaster is configured.
28+
* {@code prompts/list} returns prompts sorted by name, paged when a page size is set.
29+
* <p>
30+
* Reading and changing the catalog never blocks. {@code list} and {@code find} read it
31+
* when subscribed to, not when called, and report an invalid cursor as an error signal.
32+
* <p>
33+
* Prompt arguments are completed through
34+
* {@link McpAsyncPrompt#withCompletionHandler(McpAsyncCompletionHandler) the prompt's
35+
* completion handler} only when the repository is built with
36+
* {@link Builder#supportsCompletion(boolean) supportsCompletion(true)}.
37+
*
38+
* @author Christian Tzolov
39+
*/
40+
public final class InMemoryAsyncPromptRepository implements McpAsyncPromptRepository {
41+
42+
private final Map<String, McpAsyncPrompt> prompts = new ConcurrentSkipListMap<>();
43+
44+
private final McpChangeBroadcaster changeBroadcaster;
45+
46+
private final Integer pageSize;
47+
48+
private final boolean supportsCompletion;
49+
50+
private InMemoryAsyncPromptRepository(Builder builder) {
51+
this.changeBroadcaster = builder.changeBroadcaster;
52+
this.pageSize = builder.pageSize;
53+
this.supportsCompletion = builder.supportsCompletion;
54+
builder.prompts.forEach(this::put);
55+
}
56+
57+
public static Builder builder() {
58+
return new Builder();
59+
}
60+
61+
/**
62+
* Adds a prompt and broadcasts {@link ServerChange.PromptsListChanged}.
63+
* @throws IllegalArgumentException if a prompt with the same name exists
64+
*/
65+
public void add(McpAsyncPrompt prompt) {
66+
put(prompt);
67+
broadcastListChanged();
68+
}
69+
70+
/**
71+
* Removes a prompt by name and, if it existed, broadcasts
72+
* {@link ServerChange.PromptsListChanged}.
73+
* @return whether a prompt was removed
74+
*/
75+
public boolean remove(String name) {
76+
Assert.hasText(name, "name must not be empty");
77+
boolean removed = this.prompts.remove(name) != null;
78+
if (removed) {
79+
broadcastListChanged();
80+
}
81+
return removed;
82+
}
83+
84+
@Override
85+
public Mono<PromptsPage> list(McpRequestContext ctx, String cursor) {
86+
return Mono.fromSupplier(() -> page(cursor));
87+
}
88+
89+
@Override
90+
public Mono<McpAsyncPrompt> find(McpRequestContext ctx, String name) {
91+
return Mono.fromSupplier(() -> this.prompts.get(name));
92+
}
93+
94+
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);
97+
return new PromptsPage(page.items(), page.nextCursor(), null, null);
98+
}
99+
100+
@Override
101+
public boolean supportsCompletion() {
102+
return this.supportsCompletion;
103+
}
104+
105+
private void put(McpAsyncPrompt prompt) {
106+
Assert.notNull(prompt, "prompt must not be null");
107+
String name = prompt.definition().name();
108+
if (this.prompts.putIfAbsent(name, prompt) != null) {
109+
throw new IllegalArgumentException("Prompt with name '" + name + "' already exists");
110+
}
111+
}
112+
113+
private void broadcastListChanged() {
114+
if (this.changeBroadcaster != null) {
115+
this.changeBroadcaster.broadcast(new ServerChange.PromptsListChanged());
116+
}
117+
}
118+
119+
/**
120+
* Builder for {@link InMemoryAsyncPromptRepository}.
121+
*/
122+
public static final class Builder {
123+
124+
private final List<McpAsyncPrompt> prompts = new ArrayList<>();
125+
126+
private McpChangeBroadcaster changeBroadcaster;
127+
128+
private Integer pageSize;
129+
130+
private boolean supportsCompletion;
131+
132+
private Builder() {
133+
}
134+
135+
public Builder prompt(McpAsyncPrompt prompt) {
136+
Assert.notNull(prompt, "prompt must not be null");
137+
this.prompts.add(prompt);
138+
return this;
139+
}
140+
141+
public Builder prompts(List<McpAsyncPrompt> prompts) {
142+
Assert.notNull(prompts, "prompts must not be null");
143+
this.prompts.addAll(prompts);
144+
return this;
145+
}
146+
147+
/**
148+
* Receives a {@link ServerChange.PromptsListChanged} for every add and remove,
149+
* typically an {@link McpChangeFeed} also registered with
150+
* {@code McpServer.Builder#subscriptions}. Unset, changes are not announced.
151+
*/
152+
public Builder changeBroadcaster(McpChangeBroadcaster changeBroadcaster) {
153+
this.changeBroadcaster = changeBroadcaster;
154+
return this;
155+
}
156+
157+
/**
158+
* Maximum number of prompts per {@code prompts/list} page. Unset, all prompts are
159+
* returned on one page.
160+
*/
161+
public Builder pageSize(Integer pageSize) {
162+
Assert.isTrue(pageSize == null || pageSize > 0, "pageSize must be positive");
163+
this.pageSize = pageSize;
164+
return this;
165+
}
166+
167+
/**
168+
* Whether {@code completion/complete} is served for {@code ref/prompt} references
169+
* and {@code completions} is advertised. The server reads this once, when it is
170+
* built, so it is a fixed setting rather than derived from the prompts present:
171+
* prompts with a completion handler may be added later. A prompt without a
172+
* completion handler completes to no values. Defaults to {@code false}.
173+
*/
174+
public Builder supportsCompletion(boolean supportsCompletion) {
175+
this.supportsCompletion = supportsCompletion;
176+
return this;
177+
}
178+
179+
public InMemoryAsyncPromptRepository build() {
180+
return new InMemoryAsyncPromptRepository(this);
181+
}
182+
183+
}
184+
185+
}

0 commit comments

Comments
 (0)