Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ Changes log
bracketed and without zone, other hosts must be ASCII host names, and user information
and percent-encoding are refused.
- Enhancements
- `McpServerAdapter.setResourceSubscriptions(boolean)` lets an application that never
broadcasts resource updates stop advertising `capabilities.resources.subscribe` in
`server/discover`, so that no client waits for `notifications/resources/updated` that
never come; a `subscriptions/listen` asking for resource updates is then answered as for
any field the server doesn't honour. It is `true` by default, so nothing changes for
existing applications (#1751).
- `McpClientAdapter.setCallCustomizer(BiConsumer<Request, ContextView>)` gives the customizer
of a 2026-07-28 request over HTTP the Reactor context its call was subscribed with, so a
client passes per-call state, such as a credential computed from the call's parameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public final class McpServerAdapter extends Restlet {
private final List<McpCallScope> callScopes = new CopyOnWriteArrayList<>();
private final Map<String, Map<String, Object>> extensions = new ConcurrentHashMap<>();
private McpTaskStore taskStore;
private boolean resourceSubscriptions = true;
private final McpCoalescingFeed changes = new McpCoalescingFeed(this::publishable);
private final List<McpSubscription> subscriptions = new CopyOnWriteArrayList<>();
private SubscriptionRestriction subscriptionRestriction;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -71,6 +72,7 @@ public record Registrations(
List<McpAsyncPrompt> prompts,
List<McpToolFilter> toolFilters,
boolean tasks,
boolean resourceSubscriptions,
Map<String, McpCacheHint> cacheHints) {

/** The hint of a listing, none by default. */
Expand Down Expand Up @@ -220,16 +222,19 @@ 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<McpSchema.Resource> listed = new ArrayList<>();
private final Map<String, McpAsyncResource> byUri = new LinkedHashMap<>();
private final List<Template> templates = new ArrayList<>();
private final McpCacheHint listHint;
private final McpCacheHint templatesHint;
private final boolean subscriptions;

private Resources(Registrations registrations) {
this.subscriptions = registrations.resourceSubscriptions();
this.listHint = registrations.hint("resources/list");
this.templatesHint = registrations.hint("resources/templates/list");
for (McpResource resource : registrations.resources()) {
Expand Down Expand Up @@ -320,7 +325,7 @@ public Mono<McpAsyncResource> find(McpRequestContext ctx, String uri) {

@Override
public boolean supportsSubscribe() {
return true;
return subscriptions;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,41 @@ private static McpNotification state(String taskId, String status) {
.coalescedBy(taskId);
}

/**
* A stream asking for resource updates has them honoured by default, and not once the
* application turned resource subscriptions off.
*/
@Test
void resourceSubscriptionsFollowTheAdapterSetting() throws Exception {
Map<String, Object> asked =
Map.of("toolsListChanged", true, "resourceSubscriptions", List.of("test://one"));

McpServerAdapter on = resourceAdapter();
on.start();
assertEquals(
Map.of("toolsListChanged", true, "resourceSubscriptions", List.of("test://one")),
honoured(next(listen(on, asked, null))));

McpServerAdapter off = resourceAdapter();
off.setResourceSubscriptions(false);
off.start();
assertEquals(Map.of("toolsListChanged", true), honoured(next(listen(off, asked, null))));
}

private McpServerAdapter resourceAdapter() {
McpServerAdapter adapter = adapter();
McpServerAdapterTestCase.addEcho(adapter);
adapter.getResources()
.add(
new McpResource(
McpSchema.Resource.builder("test://one", "one").build(),
io.modelcontextprotocol.modern.server.feature.McpAsyncResource.of(
(ctx, request) ->
reactor.core.publisher.Mono.error(
new IllegalStateException()))));
return adapter;
}

private McpServerAdapter adapter() {
McpServerAdapter adapter = new McpServerAdapter();
adapters.add(adapter);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
* Copyright 2005-2026 Qlik
*<p>
* The content of this file is subject to the terms of the Apache 2.0 open
* source license available at https://www.opensource.org/licenses/apache-2.0
*<p>
* Restlet is a registered trademark of QlikTech International AB.
*/
package org.restlet.ext.mcp;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.modelcontextprotocol.json.McpJsonDefaults;
import io.modelcontextprotocol.json.McpJsonMapper;
import io.modelcontextprotocol.json.TypeRef;
import io.modelcontextprotocol.modern.McpSchema;
import io.modelcontextprotocol.modern.McpSchema.MetaKeys;
import io.modelcontextprotocol.modern.server.feature.McpAsyncResource;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.representation.StringRepresentation;
import reactor.core.publisher.Mono;

/** Whether an adapter advertises and serves resource subscriptions. */
class McpResourceSubscriptionsTestCase {
private static final McpJsonMapper MAPPER = McpJsonDefaults.getMapper();
private static final String MODERN = McpProtocol.MODERN_VERSION;

private final List<McpServerAdapter> adapters = new ArrayList<>();

@AfterEach
void cleanup() throws Exception {
for (McpServerAdapter adapter : adapters) {
adapter.stop();
}
}

/** Resource subscriptions are advertised by default. */
@Test
void advertisedByDefault() throws Exception {
McpServerAdapter adapter = adapter();
adapter.start();

assertEquals(Boolean.TRUE, resources(adapter).get("subscribe"));
}

/** An application turning them off advertises resources without subscribe. */
@Test
void notAdvertisedWhenTurnedOff() throws Exception {
McpServerAdapter adapter = adapter();
adapter.setResourceSubscriptions(false);
adapter.start();

Map<?, ?> resources = resources(adapter);
assertNotNull(resources, "resources are still advertised");
assertNotEquals(Boolean.TRUE, resources.get("subscribe"), resources.toString());
}

/** The setting can't change once the adapter runs. */
@Test
void fixedOnceStarted() throws Exception {
McpServerAdapter adapter = adapter();
adapter.start();

assertThrows(IllegalStateException.class, () -> adapter.setResourceSubscriptions(false));
}

private McpServerAdapter adapter() {
McpServerAdapter adapter = new McpServerAdapter();
adapter.getResources()
.add(
new McpResource(
McpSchema.Resource.builder("test://one", "one").build(),
McpAsyncResource.of(
(ctx, request) ->
Mono.error(new IllegalStateException()))));
adapters.add(adapter);
return adapter;
}

private static Map<?, ?> resources(McpServerAdapter adapter) throws Exception {
Map<String, Object> discover = result(call(adapter, "server/discover", Map.of(), null));
return (Map<?, ?>) ((Map<?, ?>) discover.get("capabilities")).get("resources");
}

private static Response call(
McpServerAdapter adapter, String method, Map<String, Object> params, String name) {
return adapter.handle(modern(method, params, name));
}

private static Request modern(String method, Map<String, Object> params, String name) {
Map<String, Object> meta = new LinkedHashMap<>();
meta.put(MetaKeys.PROTOCOL_VERSION, MODERN);
meta.put(MetaKeys.CLIENT_CAPABILITIES, Map.of());
Map<String, Object> withMeta = new LinkedHashMap<>(params);
withMeta.put("_meta", meta);
Map<String, Object> envelope = new LinkedHashMap<>();
envelope.put("jsonrpc", "2.0");
envelope.put("id", 1);
envelope.put("method", method);
envelope.put("params", withMeta);
String body;
try {
body = MAPPER.writeValueAsString(envelope);
} catch (java.io.IOException e) {
throw new IllegalStateException(e);
}
Request request =
new Request(
Method.POST,
"http://localhost/mcp",
new StringRepresentation(body, MediaType.APPLICATION_JSON));
request.getHeaders().set(McpProtocol.VERSION_HEADER, MODERN);
request.getHeaders().set("Accept", "application/json, text/event-stream");
request.getHeaders().set("Mcp-Method", method);
if (name != null) {
request.getHeaders().set("Mcp-Name", name);
}
return request;
}

private static Map<String, Object> envelope(Response response) throws Exception {
var bytes = new ByteArrayOutputStream();
if (response.getEntity() != null) {
response.getEntity().write(bytes);
}
return MAPPER.readValue(
bytes.toString(StandardCharsets.UTF_8), new TypeRef<Map<String, Object>>() {});
}

@SuppressWarnings("unchecked")
private static Map<String, Object> result(Response response) throws Exception {
Map<String, Object> envelope = envelope(response);
assertTrue(envelope.containsKey("result"), envelope.toString());
return (Map<String, Object>) envelope.get("result");
}
}