Friday, November 20, 2026

Build a simple AI-powered weather assistant using the GitHub Copilot SDK and .NET.

In this tutorial, we will build an AI-powered weather assistant using the GitHub Copilot SDK and .NET. To keep the focus on the core concepts, we'll develop the application as a simple console app.

Our assistant will answer a straightforward question:

What is the weather in Vancouver?

Along the way, you will learn how to:
  1. Create a .NET project
  2. Install and configure the GitHub Copilot SDK
  3. Build a custom tool
  4. Register the tool with the agent
  5. Create an AI agent
  6. Process user prompts and generate responses

By the end of this tutorial, you will have a working AI agent that can use tools to retrieve weather information and respond intelligently to user requests.

Agent workflow

The GitHub Copilot SDK supports .NET as one of its supported languages.

Step 1: Create a .NET console project

dotnet new console -n WeatherAgent
cd WeatherAgent
mkdir Helpers

Add the GitHub Copilot SDK packages:

dotnet add package GitHub.Copilot.SDK
dotnet add package Microsoft.Agents.AI.GitHub.Copilot

Step 2: Create the weather tool

Create a simple weather tools service:

public static class WeatherTools {
    public static string GetWeather(string city) {
        return $"{city}: 12°C, Cloudy";
    }
}

What is a tool?

A tool is a function that the AI agent can call when it needs information or needs to perform an action.

Examples include:

  • Get weather
  • Read a file
  • Query a database
  • Call a REST API

Step 3: Define the tool

Register a callable tool. Replace the code in Program.cs with the following:

AIFunction weatherTool = CopilotTool.DefineTool(
    (string city) => WeatherTools.GetWeather(city),
    toolOptions: new CopilotToolOptions
    {
        SkipPermission = true,
    },
    factoryOptions: new AIFunctionFactoryOptions
    {
        Name = "GetWeather",
        Description = "Returns weather information for a city.",
    }
);

Why is the description important?

The AI uses the description to decide:

  • When the tool should be used
  • What problem it solves
  • What input it expects

Good descriptions improve tool selection accuracy.

Step 4: Create the agent and register the tool

Configure the agent and register the tool with it:

await using var copilotClient = new CopilotClient();
await copilotClient.StartAsync();

AIAgent agent = copilotClient.AsAIAgent(
    name: "Weather Assistant",
    tools: [weatherTool],
    instructions: "Help users retrieve weather information."
);

As additional tools are added, the agent can perform more complex tasks. For example:

  • GetWeather()
  • GetWindSpeed()
  • GenerateReport()
  • QueryDatabase()

Step 5: Send a prompt

User request:

string prompt = "What is the weather in Vancouver?";

Execute the prompt:

AgentResponse response = await agent.RunAsync(prompt);
Console.WriteLine(response.Text);

Run the application with dotnet run. You should get this output:

Vancouver: 12°C and cloudy.

The SDK chooses a default AI model. The application can be extended to allow the user choose another AI model.

Inside the Helpers folder, add a C# class named SelectionPrompt with this code:

internal static class SelectionPrompt {
  internal static int ReadIndex(string prompt, int optionCount, int defaultIndex) {
    while (true) {
      Console.Write(prompt);
      var answer = Console.ReadLine()?.Trim();

      if (string.IsNullOrEmpty(answer))
          return defaultIndex;

      if (int.TryParse(answer, out var selection) && selection >= 1 && selection <= optionCount)
          return selection - 1;

      Console.WriteLine($"Enter a number from 1 to {optionCount}.");
    }
  }
}

Also in the Helpers folder, add a C# class named ModelSelector with this code:

internal static class ModelSelector {
  internal static async Task<string> PickAsync(CopilotClient client, string preferredModel)
  {
    var models = (await client.ListModelsAsync()).ToList();

    if (models.Count == 0)
      return string.Empty;

    var defaultIndex = models.FindIndex(model =>
      string.Equals(model.Id, preferredModel, StringComparison.OrdinalIgnoreCase));

    // Use the preferred model when available; otherwise default to the first listed model.
    defaultIndex = defaultIndex >= 0 ? defaultIndex : 0;

    Console.WriteLine("Choose a Copilot model:");
    for (var index = 0; index < models.Count; index++) {
      Console.WriteLine($"  {index + 1}. {models[index].Id}");
    }

    var selectedIndex = SelectionPrompt.ReadIndex(
      $"Model [{defaultIndex + 1}]: ",
      models.Count,
      defaultIndex);

    return models[selectedIndex].Id;
  }
}

The most important statement in the above code reads all available models into a list:

var models = (await client.ListModelsAsync()).ToList();

Let's use these helper methods in our app. In Program.cs, add this code right under await copilotClient.StartAsync();:

var model = await ModelSelector.PickAsync(copilotClient, "auto");
var sessionConfig = new SessionConfig{
    Model = model,
    Tools = [weatherTool],
    SystemMessage = new SystemMessageConfig {
        Content = "Help users retrieve weather information."
    }
};

Also, in Program.cs, replace the AIAgent agent = copilotClient.AsAIAgent(…) statement with this code:

AIAgent agent = copilotClient.AsAIAgent(
    sessionConfig: sessionConfig,
    name: "Weather Assistant");

Run the app with dotnet run. You will asked to choose a model. The number of displayed models depends on your subscription level with GitHub.

Choose a Copilot model:
  1. auto
  2. claude-sonnet-5.5
  3. claude-sonnet-5
  4. claude-fable-5.2
  5. claude-fable-5.1
  6. claude-fable-5
  7. claude-opus-5.5
  8. claude-opus-4.8-fast
  9. claude-haiku-5.5
  10. claude-haiku-4.5
  11. gpt-6.1-sol
  12. gpt-6.1-terra
  13. gpt-6-sol
Model [1]:

After entering 10 for claude-haiku-4.5, the result was:

The weather in Vancouver is currently **12°C (54°F) and cloudy**.

Understanding what happens internally

Runtime workflow

  1. User Prompt
  2. Agent Analysis
  3. Tool Selection
  4. Tool Execution
  5. Response Generation

Agent reasoning

  1. User Prompt
  2. Need City Weather
  3. Call GetWeather()
  4. Generate Answer

The Copilot SDK provides the orchestration layer that performs this workflow.

Enhancing the weather tool

Instead of returning hard-coded data, you can call a REST API:

public async Task<string>
GetWeatherAsync(string city) {
    using var client = new HttpClient();

    var json =
        await client.GetStringAsync(
            $"https://api.example.com/weather/{city}");

    return json;
}

Deployment considerations

Before deploying the assistant, decide how it will authenticate and where model inference will run. Common options include:

  1. Fine-grained personal access token: Use a narrowly scoped token for a controlled deployment, and store and rotate it securely.
  2. GitHub OAuth: For an app used by people with Copilot accounts, let each user sign in with GitHub and provide their own token to the SDK rather than sharing credentials.
  3. Azure-hosted model: For a school application, consider an Azure-hosted model instead of GitHub-hosted Copilot inference. This means configuring the app for the chosen Azure model and securing its access.

Choose the approach that fits your users, access requirements, and hosting environment. Never hard-code tokens or commit them to source control.

To implement option 1 above: 

1) Create fine-grained personal access token token on GitHub.com with ‘Copilot Requests” permission.

2) Set  GetHubToken and UseLoggedInUser options as follows:

new CopilotClientOptions { 
  LogLevel = CopilotLogLevel.Info,
  GitHubToken = Environment.GetEnvironmentVariable("COPILOT_GITHUB_TOKEN"),
  UseLoggedInUser = false
}

3) Set environment variable: COPILOT_GITHUB_TOKEN=github_pat_xxxxxxxx

Conclusion

In this tutorial, we built a .NET weather assistant with the GitHub Copilot SDK, registered a tool the agent can call, and added a model-selection step. The example uses hard-coded weather data, but you can extend the same pattern to call a real weather API or connect other tools. As you do, handle API errors and credentials securely. With the SDK coordinating prompts and tool calls, you can focus on the capabilities your assistant provides.

No comments:

Post a Comment