|
| 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