From 238c1de3005dc1d6760a50fc487bc060730097bd Mon Sep 17 00:00:00 2001 From: Jerome Louvel <374450+jlouvel@users.noreply.github.com> Date: Mon, 30 Nov 2026 15:59:23 -0500 Subject: [PATCH 1/2] MCP: let the application turn resource subscriptions off Add McpServerAdapter.setResourceSubscriptions (default true), read by Resources.supportsSubscribe(), so server/discover no longer advertises resources.subscribe for servers that never broadcast resource updates. Fixes #1751 Co-Authored-By: Claude Sonnet 5.5 --- .../org/restlet/ext/mcp/McpServerAdapter.java | 24 +++ .../ext/mcp/internal/McpModernFeatures.java | 9 +- .../mcp/McpResourceSubscriptionsTestCase.java | 148 ++++++++++++++++++ 3 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpResourceSubscriptionsTestCase.java diff --git a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/McpServerAdapter.java b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/McpServerAdapter.java index c2c61b97e0..c80e94ccb4 100644 --- a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/McpServerAdapter.java +++ b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/McpServerAdapter.java @@ -96,6 +96,7 @@ public final class McpServerAdapter extends Restlet { private final List callScopes = new CopyOnWriteArrayList<>(); private final Map> extensions = new ConcurrentHashMap<>(); private McpTaskStore taskStore; + private boolean resourceSubscriptions = true; private final McpCoalescingFeed changes = new McpCoalescingFeed(this::publishable); private final List subscriptions = new CopyOnWriteArrayList<>(); private SubscriptionRestriction subscriptionRestriction; @@ -260,6 +261,28 @@ public McpChangeBroadcaster getChanges() { return changes; } + /** + * Sets whether the adapter advertises resource subscriptions: {@code + * capabilities.resources.subscribe} in {@code server/discover}, and {@code + * notifications/resources/updated} on {@code subscriptions/listen} streams. It is {@code true} + * by default, which suits an application broadcasting resource updates through {@link + * #getChanges()}. An application never doing so should turn it off, so that no client waits for + * updates that never come: {@code resources} is then advertised without {@code subscribe}, and + * a {@code subscriptions/listen} asking for resource updates is answered as for any field the + * server doesn't honour. The legacy {@link McpProtocol#LEGACY_VERSIONS} edge never serves + * {@code resources/subscribe}, and advertises none either way. Experimental. + * + * @param value True to serve resource subscriptions. + */ + public synchronized void setResourceSubscriptions(boolean value) { + requireConfigurable(); + resourceSubscriptions = value; + } + + public synchronized boolean isResourceSubscriptions() { + return resourceSubscriptions; + } + /** * The change to broadcast: a notification with its params converted by the JSON mapper once, at * broadcast, so that the SDK converting them again per stream can't fail and silently drop it @@ -585,6 +608,7 @@ private io.modelcontextprotocol.modern.server.McpServer modernServer( List.copyOf(prompts), List.copyOf(toolFilters), taskStore != null, + resourceSubscriptions, Map.copyOf(cacheHints)))) .features(McpExtensionFeatures.of(runningMethods, runningExtensions)) .features(McpExtensionFeatures.subscriptions(List.copyOf(subscriptions))) diff --git a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpModernFeatures.java b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpModernFeatures.java index baf3031d2e..e28a89f8f9 100644 --- a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpModernFeatures.java +++ b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpModernFeatures.java @@ -62,6 +62,7 @@ private McpModernFeatures() {} * @param prompts The prompts. * @param toolFilters The tool list filters. * @param tasks Whether the adapter serves {@code tasks/*}. + * @param resourceSubscriptions Whether the adapter serves resource subscriptions. * @param cacheHints The cache hints of the listings, by method. */ public record Registrations( @@ -71,6 +72,7 @@ public record Registrations( List prompts, List toolFilters, boolean tasks, + boolean resourceSubscriptions, Map cacheHints) { /** The hint of a listing, none by default. */ @@ -220,7 +222,8 @@ private record Template( /** * The resources: exact URIs first, then templates, in registration order. Subscriptions are - * supported: the adapter publishes resource updates through its change feed. + * supported unless the application turned them off: the adapter publishes resource updates + * through its change feed. */ private static final class Resources implements McpAsyncResourceRepository { private final List listed = new ArrayList<>(); @@ -228,8 +231,10 @@ private static final class Resources implements McpAsyncResourceRepository { private final List