NOTE: This blog post was created with the help of AI tools like GitHub Copilot and ChatGPT, and it’s part of my journey to explore how AI can boost our productivity in sharing technical content.


πŸ“Ί Introduction

In this post, I’ll share how to use a C# Console Application to connect directly to an external MCP (Model Context Protocol) Server, like Hugging Face, and generate AI images.
We will walk through the process step-by-step, including using GitHub Copilot in Agent Mode, the MCP SDK for .NET, and integrating the Hugging Face MCP Server.

If you prefer to see this in action, here’s the video where I explain it all:


πŸ› οΈ Details

Project Setup

We’ll start by creating a .NET Console Application.
The goal: connect to an external MCP server and generate AI images.

βœ… Tools we are going to use:

  • Visual Studio Code or Visual Studio 2022
  • GitHub Copilot Agent Mode
  • MCP SDK for C#
  • Hugging Face MCP Server
  • GitHub Models (Optional or Azure AI Foundry models)

Secure Configuration

Before calling any services, we need to securely store secrets for:

  • Hugging Face Token
  • GitHub Token

We use User Secrets in .NET for this.


Create a Chat Client

We start by creating a simple Chat Client using the C# MCP SDK.
By default, it will connect to a GPT-4.1-mini model hosted on GitHub.


Connect to Hugging Face MCP Server

Once the basic client is running, we authenticate and connect to the Hugging Face MCP Server.

// create MCP Client using Hugging Face endpoint
var hfHeaders = new Dictionary<string, string>
{
    { "Authorization", $"Bearer {config["HF_API_KEY"]}" }
};
var clientTransport = new SseClientTransport(
    new()
    {
        Name = "HF Server",
        Endpoint = new Uri("https://huggingface.co/mcp"),
        AdditionalHeaders = hfHeaders
    });
await using var mcpClient = await McpClientFactory.CreateAsync(clientTransport);


List Available Tools

Hugging Face exposes several tools (including ones for image generation).
We can list them and confirm what’s available.

// Display the available server tools
var tools = await mcpClient.ListToolsAsync();
foreach (var tool in tools)
{
    Console.WriteLine($"Connected to server with tools: {tool.Name}");
}

Generate an Image with AI

Finally, we use the MCP Server’s tools to generate an image from a prompt.
Once completed, the response will include a URL to the generated image.

// create an IChatClient using the MCP tools
IChatClient client = GetChatClient();
var chatOptions = new ChatOptions
{
    Tools = [.. tools],
    ModelId = deploymentName
};

// Create image
Console.WriteLine("Starting the process to generate an image of a pixelated puppy...");
var query = "Create an image of a pixelated puppy.";
var result = await client.GetResponseAsync(query, chatOptions);
Console.Write($"AI response: {result}");

This is a sample image generated by the Hugging Face C# MCP Sample:

beaver generated by the hugging face mcp server

Bonus: AI Foundry

The same code can be adapted to run with AI Foundry models for experimentation and additional flexibility.


βœ… Conclusion

This is a super fun and practical example of combining:

  • C#
  • MCP Protocol
  • Hugging Face MCP Server
  • GitHub Copilot in Agent Mode

All wrapped together to show how developers can create powerful solutions leveraging modern AI infrastructure directly from a simple .NET Console App.

If you’re exploring AI in .NET, this is an excellent hands-on scenario to expand your skills.


πŸ“š Resources

πŸ”— Sample Repository: https://aka.ms/genainet
πŸ”— Hugging Face MCP Server: https://huggingface.co/settings/mcp
πŸ”— GitHub Models: https://github.com/marketplace?type=models
πŸ”— C# MCP SDK: https://github.com/modelcontextprotocol/csharp-sdk
πŸ”— Full Sample Code: https://github.com/microsoft/Generative-AI-for-beginners-dotnet/blob/main/03-CoreGenerativeAITechniques/src/MCP-01-HuggingFace/Program.cs

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


2 responses to “πŸ€– Using C# to Call Hugging Face MCP Server and Generate AI Images!”

  1. […] πŸ€– Using C# to Call Hugging Face MCP Server and Generate AI Images! (Bruno Capuano) […]

    Like

  2. […] the previous post, we also covered how we can do the same with a MCP server that acts as a bridge between the C# app and the underlying AI tooling β€” helping invoke tools […]

    Like

Leave a comment

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading