Skip to content

Commit 64fcca1

Browse files
Mohammed Aboullaiteslachiewicz
authored andcommitted
Stamp default ttlMs and cacheScope on server responses (SEP-2549)
The draft spec requires cacheable results to carry ttlMs and cacheScope on the wire. Other SDKs (Go, Python, TypeScript) all stamp defaults after handlers return: ttlMs=0 (immediately stale) and cacheScope=public. This adds the same default stamping in McpAsyncServer and McpStatelessAsyncServer for all five cacheable result types. For list results the defaults are set at the build site. For ReadResourceResult, which is built by user handlers, a withCacheDefaults helper stamps missing fields while preserving any values the handler set explicitly.
1 parent 7fad072 commit 64fcca1

2 files changed

Lines changed: 58 additions & 10 deletions

File tree

‎mcp-core/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,10 @@ private McpRequestHandler<McpSchema.ListToolsResult> toolsListRequestHandler() {
548548
.filterWhen(tool -> this.toolFilter.isVisible(exchange.transportContext(), tool)
549549
.onErrorResume(error -> opaqueListFilterError(tool, error)))
550550
.collectList()
551-
.map(tools -> McpSchema.ListToolsResult.builder(tools).build());
551+
.map(tools -> McpSchema.ListToolsResult.builder(tools)
552+
.ttlMs(0)
553+
.cacheScope(McpSchema.CacheScope.PUBLIC)
554+
.build());
552555
};
553556
}
554557

@@ -815,7 +818,10 @@ private McpRequestHandler<McpSchema.ListResourcesResult> resourcesListRequestHan
815818
.stream()
816819
.map(McpServerFeatures.AsyncResourceSpecification::resource)
817820
.toList();
818-
return Mono.just(McpSchema.ListResourcesResult.builder(resourceList).build());
821+
return Mono.just(McpSchema.ListResourcesResult.builder(resourceList)
822+
.ttlMs(0)
823+
.cacheScope(McpSchema.CacheScope.PUBLIC)
824+
.build());
819825
};
820826
}
821827

@@ -825,7 +831,10 @@ private McpRequestHandler<McpSchema.ListResourceTemplatesResult> resourceTemplat
825831
.stream()
826832
.map(McpServerFeatures.AsyncResourceTemplateSpecification::resourceTemplate)
827833
.toList();
828-
return Mono.just(McpSchema.ListResourceTemplatesResult.builder(resourceList).build());
834+
return Mono.just(McpSchema.ListResourceTemplatesResult.builder(resourceList)
835+
.ttlMs(0)
836+
.cacheScope(McpSchema.CacheScope.PUBLIC)
837+
.build());
829838
};
830839
}
831840

@@ -846,10 +855,22 @@ private McpRequestHandler<McpSchema.ReadResourceResult> resourcesReadRequestHand
846855
return this.findResourceTemplateSpecification(resourceUri)
847856
.map(spec -> spec.readHandler().apply(ex, resourceRequest))
848857
.orElseGet(() -> Mono.error(RESOURCE_NOT_FOUND.apply(resourceUri)));
849-
});
858+
})
859+
.map(McpAsyncServer::withCacheDefaults);
850860
};
851861
}
852862

863+
private static McpSchema.ReadResourceResult withCacheDefaults(McpSchema.ReadResourceResult result) {
864+
if (result.ttlMs() == null || result.cacheScope() == null) {
865+
return McpSchema.ReadResourceResult.builder(result.contents())
866+
.meta(result.meta())
867+
.ttlMs(result.ttlMs() != null ? result.ttlMs() : 0)
868+
.cacheScope(result.cacheScope() != null ? result.cacheScope() : McpSchema.CacheScope.PUBLIC)
869+
.build();
870+
}
871+
return result;
872+
}
873+
853874
private Optional<McpServerFeatures.AsyncResourceSpecification> findResourceSpecification(String uri) {
854875
var result = this.resources.values()
855876
.stream()
@@ -976,7 +997,10 @@ private McpRequestHandler<McpSchema.ListPromptsResult> promptsListRequestHandler
976997
.map(McpServerFeatures.AsyncPromptSpecification::prompt)
977998
.toList();
978999

979-
return Mono.just(McpSchema.ListPromptsResult.builder(promptList).build());
1000+
return Mono.just(McpSchema.ListPromptsResult.builder(promptList)
1001+
.ttlMs(0)
1002+
.cacheScope(McpSchema.CacheScope.PUBLIC)
1003+
.build());
9801004
};
9811005
}
9821006

‎mcp-core/src/main/java/io/modelcontextprotocol/server/McpStatelessAsyncServer.java‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,10 @@ private McpStatelessRequestHandler<McpSchema.ListToolsResult> toolsListRequestHa
427427
.filterWhen(tool -> this.toolFilter.isVisible(ctx, tool)
428428
.onErrorResume(error -> opaqueListFilterError(tool, error)))
429429
.collectList()
430-
.map(tools -> McpSchema.ListToolsResult.builder(tools).build());
430+
.map(tools -> McpSchema.ListToolsResult.builder(tools)
431+
.ttlMs(0)
432+
.cacheScope(McpSchema.CacheScope.PUBLIC)
433+
.build());
431434
};
432435
}
433436

@@ -606,7 +609,10 @@ private McpStatelessRequestHandler<McpSchema.ListResourcesResult> resourcesListR
606609
.stream()
607610
.map(McpStatelessServerFeatures.AsyncResourceSpecification::resource)
608611
.toList();
609-
return Mono.just(McpSchema.ListResourcesResult.builder(resourceList).build());
612+
return Mono.just(McpSchema.ListResourcesResult.builder(resourceList)
613+
.ttlMs(0)
614+
.cacheScope(McpSchema.CacheScope.PUBLIC)
615+
.build());
610616
};
611617
}
612618

@@ -616,7 +622,10 @@ private McpStatelessRequestHandler<McpSchema.ListResourceTemplatesResult> resour
616622
.stream()
617623
.map(AsyncResourceTemplateSpecification::resourceTemplate)
618624
.toList();
619-
return Mono.just(McpSchema.ListResourceTemplatesResult.builder(resourceList).build());
625+
return Mono.just(McpSchema.ListResourceTemplatesResult.builder(resourceList)
626+
.ttlMs(0)
627+
.cacheScope(McpSchema.CacheScope.PUBLIC)
628+
.build());
620629
};
621630
}
622631

@@ -636,11 +645,23 @@ private McpStatelessRequestHandler<McpSchema.ReadResourceResult> resourcesReadRe
636645
return this.findResourceTemplateSpecification(resourceUri)
637646
.map(spec -> spec.readHandler().apply(ctx, resourceRequest))
638647
.orElseGet(() -> Mono.error(RESOURCE_NOT_FOUND.apply(resourceUri)));
639-
});
648+
})
649+
.map(McpStatelessAsyncServer::withCacheDefaults);
640650

641651
};
642652
}
643653

654+
private static McpSchema.ReadResourceResult withCacheDefaults(McpSchema.ReadResourceResult result) {
655+
if (result.ttlMs() == null || result.cacheScope() == null) {
656+
return McpSchema.ReadResourceResult.builder(result.contents())
657+
.meta(result.meta())
658+
.ttlMs(result.ttlMs() != null ? result.ttlMs() : 0)
659+
.cacheScope(result.cacheScope() != null ? result.cacheScope() : McpSchema.CacheScope.PUBLIC)
660+
.build();
661+
}
662+
return result;
663+
}
664+
644665
private Optional<McpStatelessServerFeatures.AsyncResourceSpecification> findResourceSpecification(String uri) {
645666
var result = this.resources.values()
646667
.stream()
@@ -736,7 +757,10 @@ private McpStatelessRequestHandler<McpSchema.ListPromptsResult> promptsListReque
736757
.map(McpStatelessServerFeatures.AsyncPromptSpecification::prompt)
737758
.toList();
738759

739-
return Mono.just(McpSchema.ListPromptsResult.builder(promptList).build());
760+
return Mono.just(McpSchema.ListPromptsResult.builder(promptList)
761+
.ttlMs(0)
762+
.cacheScope(McpSchema.CacheScope.PUBLIC)
763+
.build());
740764
};
741765
}
742766

0 commit comments

Comments
 (0)