3434 * requested filter, then forwards the changes from a {@link McpChangePublisher} that the
3535 * honoured filter covers. The core fields are honoured for the primitives the server has;
3636 * other features honour the fields they define through
37- * {@link McpFeature#honouredSubscriptions}.
37+ * {@link McpFeature#honouredSubscriptions}. A {@link SubscriptionRestriction} then
38+ * narrows the result per request.
3839 *
3940 * @author Dariusz Jędrzejczyk
4041 */
@@ -55,6 +56,8 @@ final class SubscriptionsFeature implements McpFeature {
5556
5657 private final List <McpFeature > features ;
5758
59+ private final SubscriptionRestriction restriction ;
60+
5861 private final boolean hasTools ;
5962
6063 private final boolean hasPrompts ;
@@ -67,13 +70,17 @@ final class SubscriptionsFeature implements McpFeature {
6770
6871 /**
6972 * @param features the server's other features, asked for the fields they honour
73+ * @param restriction narrows what the server and its features honour, per request
7074 */
7175 SubscriptionsFeature (McpChangePublisher publisher , McpJsonMapper jsonMapper , List <McpFeature > features ,
72- boolean hasTools , boolean hasPrompts , boolean hasResources , boolean hasResourcesSubscribe ) {
76+ SubscriptionRestriction restriction , boolean hasTools , boolean hasPrompts , boolean hasResources ,
77+ boolean hasResourcesSubscribe ) {
7378 Assert .notNull (publisher , "publisher must not be null" );
79+ Assert .notNull (restriction , "restriction must not be null" );
7480 this .publisher = publisher ;
7581 this .jsonMapper = jsonMapper ;
7682 this .features = List .copyOf (features );
83+ this .restriction = restriction ;
7784 this .hasTools = hasTools ;
7885 this .hasPrompts = hasPrompts ;
7986 this .hasResources = hasResources ;
@@ -114,7 +121,8 @@ public Mono<McpAsyncResponse<Result>> handle(McpRequestContext ctx, Object param
114121 catch (McpException ex ) {
115122 return Mono .error (ex );
116123 }
117- return Mono .just (McpAsyncResponse .streaming (notifier -> listen (ctx , honoured , notifier )));
124+ return restricted (ctx , requested , honoured )
125+ .map (agreed -> McpAsyncResponse .streaming (notifier -> listen (ctx , agreed , notifier )));
118126 }
119127
120128 @ Override
@@ -189,6 +197,47 @@ private SubscriptionFilter honoured(McpRequestContext ctx, SubscriptionFilter re
189197 return SubscriptionFilter .of (honoured );
190198 }
191199
200+ /**
201+ * What the restriction leaves of {@code honoured}. Deferred so that a restriction
202+ * throwing instead of returning an error {@code Mono} still rejects the request.
203+ */
204+ private Mono <SubscriptionFilter > restricted (McpRequestContext ctx , SubscriptionFilter requested ,
205+ SubscriptionFilter honoured ) {
206+ return Mono .defer (() -> this .restriction .restrict (ctx , requested , honoured ))
207+ .defaultIfEmpty (SubscriptionFilter .EMPTY )
208+ .map (restricted -> intersection (honoured , restricted ));
209+ }
210+
211+ /**
212+ * The part of {@code restricted} that {@code honoured} covers: the restriction may
213+ * only narrow, so that the stream never acknowledges a notification the client didn't
214+ * request or that the server can't deliver. A flag is kept if both set it to
215+ * {@code true}, a list keeps the elements of {@code honoured}'s list that
216+ * {@code restricted}'s list contains, and any other value is kept if both have it.
217+ */
218+ private static SubscriptionFilter intersection (SubscriptionFilter honoured , SubscriptionFilter restricted ) {
219+ SubscriptionFilter widened = restricted .missingFrom (honoured );
220+ if (!widened .isEmpty ()) {
221+ logger .warn ("Ignoring subscriptions the restriction added beyond what the server honours: {}" , widened );
222+ }
223+ Map <String , Object > kept = new LinkedHashMap <>();
224+ honoured .fields ().forEach ((field , value ) -> {
225+ Object agreed = restricted .get (field );
226+ if (value instanceof List <?> elements ) {
227+ List <?> agreedElements = agreed instanceof List <?> list ? list : List .of ();
228+ List <?> both = elements .stream ().filter (agreedElements ::contains ).toList ();
229+ if (!both .isEmpty ()) {
230+ kept .put (field , both );
231+ }
232+ }
233+ else if (value .equals (agreed )) {
234+ // Covers flags too: honoured only ever sets them to true.
235+ kept .put (field , value );
236+ }
237+ });
238+ return SubscriptionFilter .of (kept );
239+ }
240+
192241 /**
193242 * The params of the notification for {@code change}, with the stream's {@code _meta},
194243 * or {@code null} if they aren't an object.
0 commit comments