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:

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
Leave a reply to ๐ผ๏ธ Text-to-Image locally in C# with TransformerSharp – El Bruno Cancel reply