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,54 @@ 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. Not
203+ * called when nothing is honoured: it could only narrow to nothing, and may do costly
204+ * work such as a permission lookup.
205+ */
206+ private Mono <SubscriptionFilter > restricted (McpRequestContext ctx , SubscriptionFilter requested ,
207+ SubscriptionFilter honoured ) {
208+ if (honoured .isEmpty ()) {
209+ return Mono .just (SubscriptionFilter .EMPTY );
210+ }
211+ return Mono .defer (() -> this .restriction .restrict (ctx , requested , honoured ))
212+ .defaultIfEmpty (SubscriptionFilter .EMPTY )
213+ .map (restricted -> intersection (honoured , restricted ));
214+ }
215+
216+ /**
217+ * The part of {@code restricted} that {@code honoured} covers: the restriction may
218+ * only narrow, so that the stream never acknowledges a notification the client didn't
219+ * request or that the server can't deliver. A list keeps the elements of
220+ * {@code honoured}'s list that {@code restricted}'s list contains, and any other
221+ * value, flags included, is kept if {@code restricted} has the same value.
222+ */
223+ private static SubscriptionFilter intersection (SubscriptionFilter honoured , SubscriptionFilter restricted ) {
224+ if (honoured == restricted ) {
225+ return restricted ;
226+ }
227+ SubscriptionFilter widened = restricted .missingFrom (honoured );
228+ if (!widened .isEmpty ()) {
229+ logger .warn ("Ignoring subscriptions the restriction added beyond what the server honours: {}" , widened );
230+ }
231+ Map <String , Object > kept = new LinkedHashMap <>();
232+ honoured .fields ().forEach ((field , value ) -> {
233+ Object agreed = restricted .get (field );
234+ if (value instanceof List <?> elements ) {
235+ List <?> agreedElements = agreed instanceof List <?> list ? list : List .of ();
236+ List <?> both = elements .stream ().filter (agreedElements ::contains ).toList ();
237+ if (!both .isEmpty ()) {
238+ kept .put (field , both );
239+ }
240+ }
241+ else if (value .equals (agreed )) {
242+ kept .put (field , value );
243+ }
244+ });
245+ return SubscriptionFilter .of (kept );
246+ }
247+
192248 /**
193249 * The params of the notification for {@code change}, with the stream's {@code _meta},
194250 * or {@code null} if they aren't an object.
0 commit comments