1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
| func ForecastCall(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// get city name
city := request.Params.Arguments["city"].(string)
if len(city) == 0 {
return nil, fmt.Errorf("city name is empty")
}
// get info type
infoType := request.Params.Arguments["type"].(string)
if len(infoType) == 0 {
return nil, fmt.Errorf("info type is empty")
}
adcode, exists := cityMap.CityClient.GetAdcode(city)
if !exists {
return nil, fmt.Errorf("city %s not found, code %s", city, adcode)
}
// get weather forecast
weatherInfo, err := weather.FetchWeatherData(adcode, infoType)
if err != nil {
return nil, err
}
// parse weather info
forecast := make([]ForecastResult, 0)
if infoType == "base" {
// parse base info
var baseInfo weather.BaseInfo
if err := json.Unmarshal([]byte(weatherInfo), &baseInfo); err != nil {
return nil, fmt.Errorf("parse base info failed: %v", err)
}
for _, forecastInfo := range baseInfo.Lives {
// append forecast info
forecast = append(forecast, ForecastResult{
Name: forecastInfo.City,
Temperature: forecastInfo.Temperature,
Wind: forecastInfo.Windpower,
Forecast: forecastInfo.Weather,
Date: forecastInfo.Reporttime,
})
}
} else if infoType == "all" {
// parse all info
var allInfo weather.AllInfo
if err := json.Unmarshal([]byte(weatherInfo), &allInfo); err != nil {
return nil, fmt.Errorf("parse all info failed: %v", err)
}
for _, forecastInfo := range allInfo.Forecasts {
for _, cast := range forecastInfo.Casts {
forecast = append(forecast, ForecastResult{
Name: forecastInfo.City,
Temperature: cast.Daytemp,
Wind: cast.Daywind,
Forecast: cast.Dayweather,
Date: cast.Date,
})
}
}
}
forecastResponse, err := json.Marshal(&forecast)
if err != nil {
return nil, fmt.Errorf("error marshalling forecast: %w", err)
}
return mcp.NewToolResultText(string(forecastResponse)), nil
}
|