Skip to content

Commit 4ee31c1

Browse files
committed
samples: historical weather server returns data for a range of +/- days every year
- This sends less tokens to the LLM and makes the demo cheaper. - Closes #10 Signed-off-by: Daniel Garnier-Moiroux <[email protected]>
1 parent c7dac7c commit 4ee31c1

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

samples/sample-mcp-server-secured-tools/src/main/java/org/springaicommunity/mcp/security/sample/server/securedtools/McpServerConfiguration.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ ToolCallbackProvider toolCallbackProvider(WeatherService weatherService) {
4545
SecurityFilterChain securityFilterChain(HttpSecurity http,
4646
@Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}") String issuerUrl) throws Exception {
4747
return http.authorizeHttpRequests(auth -> auth.anyRequest().permitAll())
48-
4948
.with(mcpServerAuthorization(), (mcpAuthorization) -> {
5049
mcpAuthorization.authorizationServer(issuerUrl).resourcePath("/mcp");
5150
})

samples/sample-mcp-server/src/main/java/org/springaicommunity/mcp/security/sample/server/streamable/HistoricalWeatherService.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,46 @@ public record DailyTemperatures(String date, double minTemperature, double maxTe
5656
@Tool(name = "temperature-history",
5757
description = "Get 5-year historical temperature data (in Celsius), including daily min and daily max temperatures, for a specific location")
5858
public ToolResponse getHistoricalWeatherData(@ToolParam(description = "The location latitude") double latitude,
59-
@ToolParam(description = "The location longitude") double longitude, ToolContext toolContext) {
59+
@ToolParam(description = "The location longitude") double longitude) {
6060

61-
HistoricalWeatherApiResponse response = restClient.get()
61+
var data = IntStream.range(0, 5)
62+
.parallel()
63+
.mapToObj(yearDelta -> getWeatherData(latitude, longitude, yearDelta))
64+
.flatMap(List::stream)
65+
.toList();
66+
67+
return new ToolResponse(data);
68+
}
69+
70+
/**
71+
* Obtain weather data at the given location, N years ago, for +/- 2 days. For
72+
* example, if today is 2025-09-28, and N years = 2 years, will return weather data
73+
* for 2023-09-26 through 2023-09-30.
74+
*/
75+
private List<ToolResponse.DailyTemperatures> getWeatherData(double latitude, double longitude, int yearsAgo) {
76+
var response = restClient.get()
6277
.uri("https://archive-api.open-meteo.com/v1/archive?latitude={latitude}&longitude={longitude}&start_date={start}&end_date={end}&daily=temperature_2m_min,temperature_2m_max",
6378
//@formatter:off
6479
Map.of(
6580
"latitude", latitude,
6681
"longitude", longitude,
67-
"start", LocalDate.now().minus(Period.ofYears(5)),
68-
"end", LocalDate.now()
82+
"start", LocalDate.now().minus(Period.ofYears(yearsAgo)).minus(Period.ofDays(2)),
83+
"end", yearsAgo == 0 ? LocalDate.now() : LocalDate.now().minus(Period.ofYears(yearsAgo)).plus(Period.ofDays(2))
6984
)
7085
//@formatter:on
7186
)
72-
7387
.retrieve()
7488
.body(HistoricalWeatherApiResponse.class);
7589
var entries = response.daily().time().length;
7690
//@formatter:off
77-
var mapped = IntStream.range(0, entries)
78-
.mapToObj(i -> new ToolResponse.DailyTemperatures(
79-
response.daily().time()[i].toString(),
80-
response.daily().temperature_2m_min()[i],
81-
response.daily().temperature_2m_max()[i]
82-
))
83-
.toList();
84-
//@formatter:on
85-
86-
return new ToolResponse(mapped);
87-
91+
return IntStream.range(0, entries)
92+
.mapToObj(i -> new ToolResponse.DailyTemperatures(
93+
response.daily().time()[i].toString(),
94+
response.daily().temperature_2m_min()[i],
95+
response.daily().temperature_2m_max()[i]
96+
))
97+
.toList();
98+
//@formatter:on
8899
}
89100

90101
}

0 commit comments

Comments
 (0)