Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

c# server quickstart broken #230

Open
@DGuhr

Description

@DGuhr

Describe the bug
On this page, when you use the C# SDK, there are 2 Bugs resulting in always getting a "404" response.

First bug: The part where we extract the forecastURL and request that json is missing in the example.
Second bug: The double langitude/longitude values are only working in geos where it's really e.g. "95.789" and not "95,789" (as, for example, here in germany).
This leads to bad requests.

To Reproduce
Steps to reproduce the behavior:

  1. build the c# example locally
  2. try getting a weather forecast for any city. see the 404 error appearing.

Expected behavior
Quickstart works.

Logs
No logs, but here's an example of a working GetForecast method:

[McpServerTool, Description("Get weather forecast for a location.")]
    public static async Task<string> GetForecast(
        HttpClient client,
        [Description("Latitude of the location.")] double latitude,
        [Description("Longitude of the location.")] double longitude)
    {
        try
        {
            // Format the coordinates properly
            var formattedLat = latitude.ToString("F4", System.Globalization.CultureInfo.InvariantCulture);
            var formattedLon = longitude.ToString("F4", System.Globalization.CultureInfo.InvariantCulture);
            
            var pointsResponse = await client.GetAsync($"/points/{formattedLat},{formattedLon}");
            var pointsContent = await pointsResponse.Content.ReadAsStringAsync();
            
            if (!pointsResponse.IsSuccessStatusCode)
            {
                return $"Error retrieving points data: {(int)pointsResponse.StatusCode} {pointsResponse.StatusCode}";
            }
            
            // Parse the forecast URL from the points response
            var forecastUrl = JsonDocument.Parse(pointsContent).RootElement.GetProperty("properties").GetProperty("forecast").GetString();
            
            if (string.IsNullOrEmpty(forecastUrl))
            {
                return "Could not retrieve forecast URL from points data.";
            }
            
            // Get the forecast with the full URL from the response
            var forecastResponse = await client.GetAsync(forecastUrl);
            var forecastContent = await forecastResponse.Content.ReadAsStringAsync();
            
            if (!forecastResponse.IsSuccessStatusCode)
            {
                return $"Error retrieving forecast: {(int)forecastResponse.StatusCode} {forecastResponse.StatusCode}";
            }
            
            var forecastJson = JsonDocument.Parse(forecastContent).RootElement;
            var periods = forecastJson.GetProperty("properties").GetProperty("periods").EnumerateArray();

            return string.Join("\n---\n", periods.Select(period => $"""
                    {period.GetProperty("name").GetString()}
                    Temperature: {period.GetProperty("temperature").GetInt32()}°F
                    Wind: {period.GetProperty("windSpeed").GetString()} {period.GetProperty("windDirection").GetString()}
                    Forecast: {period.GetProperty("detailedForecast").GetString()}
                    """));
        }
        catch (Exception ex)
        {
            return $"Error retrieving forecast: {ex.Message}";
        }
    }

Additional context
Searched for some already open bug but couldn't find any. If there is one, and a fix already (repo just shows the python example), please close this. thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions