@@ -120,6 +120,11 @@ public class McpAsyncServer {
120120
121121 private final McpAsyncListFilter <McpSchema .Tool > toolFilter ;
122122
123+ /**
124+ * The caching hint attached to every listing response (SEP-2549).
125+ */
126+ private final McpListCacheOptions listCacheOptions ;
127+
123128 private List <String > protocolVersions ;
124129
125130 private McpUriTemplateManagerFactory uriTemplateManagerFactory = new DefaultMcpUriTemplateManagerFactory ();
@@ -134,7 +139,7 @@ public class McpAsyncServer {
134139 McpAsyncServer (McpServerTransportProvider mcpTransportProvider , McpJsonMapper jsonMapper ,
135140 McpServerFeatures .Async features , Duration requestTimeout ,
136141 McpUriTemplateManagerFactory uriTemplateManagerFactory , JsonSchemaValidator jsonSchemaValidator ,
137- boolean validateToolInputs ) {
142+ boolean validateToolInputs , McpListCacheOptions listCacheOptions ) {
138143 this .mcpTransportProvider = mcpTransportProvider ;
139144 this .jsonMapper = jsonMapper ;
140145 this .serverInfo = features .serverInfo ();
@@ -149,6 +154,7 @@ public class McpAsyncServer {
149154 this .jsonSchemaValidator = jsonSchemaValidator ;
150155 this .validateToolInputs = validateToolInputs ;
151156 this .toolFilter = McpAsyncListFilter .and (features .toolFilters ());
157+ this .listCacheOptions = listCacheOptions != null ? listCacheOptions : McpListCacheOptions .NONE ;
152158
153159 Map <String , McpRequestHandler <?>> requestHandlers = prepareRequestHandlers ();
154160 Map <String , McpNotificationHandler > notificationHandlers = prepareNotificationHandlers (features );
@@ -166,7 +172,7 @@ public class McpAsyncServer {
166172 McpAsyncServer (McpStreamableServerTransportProvider mcpTransportProvider , McpJsonMapper jsonMapper ,
167173 McpServerFeatures .Async features , Duration requestTimeout ,
168174 McpUriTemplateManagerFactory uriTemplateManagerFactory , JsonSchemaValidator jsonSchemaValidator ,
169- boolean validateToolInputs ) {
175+ boolean validateToolInputs , McpListCacheOptions listCacheOptions ) {
170176 this .mcpTransportProvider = mcpTransportProvider ;
171177 this .jsonMapper = jsonMapper ;
172178 this .serverInfo = features .serverInfo ();
@@ -181,6 +187,7 @@ public class McpAsyncServer {
181187 this .jsonSchemaValidator = jsonSchemaValidator ;
182188 this .validateToolInputs = validateToolInputs ;
183189 this .toolFilter = McpAsyncListFilter .and (features .toolFilters ());
190+ this .listCacheOptions = listCacheOptions != null ? listCacheOptions : McpListCacheOptions .NONE ;
184191
185192 Map <String , McpRequestHandler <?>> requestHandlers = prepareRequestHandlers ();
186193 Map <String , McpNotificationHandler > notificationHandlers = prepareNotificationHandlers (features );
@@ -548,9 +555,11 @@ private McpRequestHandler<McpSchema.ListToolsResult> toolsListRequestHandler() {
548555 .filterWhen (tool -> this .toolFilter .isVisible (exchange .transportContext (), tool )
549556 .onErrorResume (error -> opaqueListFilterError (tool , error )))
550557 .collectList ()
558+ // PRIVATE, not PUBLIC: the listing is filtered per caller, so a shared
559+ // cache must never serve one principal's response to another.
551560 .map (tools -> McpSchema .ListToolsResult .builder (tools )
552- .ttlMs (0L )
553- .cacheScope (McpSchema . CacheScope . PUBLIC )
561+ .ttlMs (this . listCacheOptions . ttlMs () )
562+ .cacheScope (this . listCacheOptions . cacheScope () )
554563 .build ());
555564 };
556565 }
@@ -819,8 +828,8 @@ private McpRequestHandler<McpSchema.ListResourcesResult> resourcesListRequestHan
819828 .map (McpServerFeatures .AsyncResourceSpecification ::resource )
820829 .toList ();
821830 return Mono .just (McpSchema .ListResourcesResult .builder (resourceList )
822- .ttlMs (0L )
823- .cacheScope (McpSchema . CacheScope . PUBLIC )
831+ .ttlMs (this . listCacheOptions . ttlMs () )
832+ .cacheScope (this . listCacheOptions . cacheScope () )
824833 .build ());
825834 };
826835 }
@@ -832,8 +841,8 @@ private McpRequestHandler<McpSchema.ListResourceTemplatesResult> resourceTemplat
832841 .map (McpServerFeatures .AsyncResourceTemplateSpecification ::resourceTemplate )
833842 .toList ();
834843 return Mono .just (McpSchema .ListResourceTemplatesResult .builder (resourceList )
835- .ttlMs (0L )
836- .cacheScope (McpSchema . CacheScope . PUBLIC )
844+ .ttlMs (this . listCacheOptions . ttlMs () )
845+ .cacheScope (this . listCacheOptions . cacheScope () )
837846 .build ());
838847 };
839848 }
@@ -998,8 +1007,8 @@ private McpRequestHandler<McpSchema.ListPromptsResult> promptsListRequestHandler
9981007 .toList ();
9991008
10001009 return Mono .just (McpSchema .ListPromptsResult .builder (promptList )
1001- .ttlMs (0L )
1002- .cacheScope (McpSchema . CacheScope . PUBLIC )
1010+ .ttlMs (this . listCacheOptions . ttlMs () )
1011+ .cacheScope (this . listCacheOptions . cacheScope () )
10031012 .build ());
10041013 };
10051014 }
0 commit comments