|
| 1 | +package io.helidon.integrations.mcp.tests.imperative; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.stream.Collectors; |
| 5 | + |
| 6 | +import io.helidon.integrations.mcp.server.JsonSchema; |
| 7 | +import io.helidon.integrations.mcp.server.McpParameters; |
| 8 | +import io.helidon.integrations.mcp.server.Tool; |
| 9 | +import io.helidon.integrations.mcp.server.ToolContent; |
| 10 | +import io.helidon.integrations.mcp.server.ToolContents; |
| 11 | +import io.helidon.webclient.api.HttpClientResponse; |
| 12 | +import io.helidon.webclient.api.WebClient; |
| 13 | + |
| 14 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 15 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 16 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 17 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 18 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | + |
| 20 | +public class McpWeatherTool implements Tool { |
| 21 | + private final WebClient webClient; |
| 22 | + |
| 23 | + McpWeatherTool() { |
| 24 | + webClient = WebClient.builder() |
| 25 | + .baseUri("https://api.weather.gov") |
| 26 | + .addHeader("Accept", "application/geo+json") |
| 27 | + . addHeader( "User-Agent", "WeatherApiClient/1.0 ([email protected])") |
| 28 | + .build(); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public String name() { |
| 33 | + return "weather-state-report"; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public String description() { |
| 38 | + return "Get a weather report per US state"; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public JsonSchema schema() { |
| 43 | + return JsonSchema.builder() |
| 44 | + .addString("state") |
| 45 | + .build(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public ToolContent process(McpParameters parameters) { |
| 50 | + String state = parameters.get("state").asString().orElse(""); |
| 51 | + |
| 52 | + try (HttpClientResponse response = webClient.get() |
| 53 | + .path("/alerts/active/area/" + state) |
| 54 | + .request()) { |
| 55 | + Alert alert = new ObjectMapper().readValue(response.as(String.class), new TypeReference<>() {}); |
| 56 | + String content = alert.features() |
| 57 | + .stream() |
| 58 | + .map(f -> String.format(""" |
| 59 | + Event: %s |
| 60 | + Area: %s |
| 61 | + Severity: %s |
| 62 | + Description: %s |
| 63 | + Instructions: %s |
| 64 | + """, f.properties().event(), f.properties.areaDesc(), f.properties.severity(), |
| 65 | + f.properties.description(), f.properties.instruction())) |
| 66 | + .collect(Collectors.joining("\n")); |
| 67 | + return ToolContents.textContent(content); |
| 68 | + } catch (JsonProcessingException e) { |
| 69 | + throw new RuntimeException(e); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 74 | + public record Alert(@JsonProperty("features") List<Feature> features) { |
| 75 | + |
| 76 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 77 | + public record Feature(@JsonProperty("properties") Properties properties) {} |
| 78 | + |
| 79 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 80 | + public record Properties(@JsonProperty("event") String event, |
| 81 | + @JsonProperty("areaDesc") String areaDesc, |
| 82 | + @JsonProperty("severity") String severity, |
| 83 | + @JsonProperty("description") String description, |
| 84 | + @JsonProperty("instruction") String instruction) {} |
| 85 | + } |
| 86 | +} |
0 commit comments