Skip to content

Commit aa7fff3

Browse files
authored
Merge pull request #196 from microsoft/bruno-mcp-huggingface
Add MCP-02-HuggingFace-Ollama project with image generation sample
2 parents d6f9b30 + c697409 commit aa7fff3

File tree

5 files changed

+117
-5
lines changed

5 files changed

+117
-5
lines changed

03-CoreGenerativeAITechniques/src/CoreGenerativeAITechniques.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "14 MCP", "14 MCP", "{3D13EC
103103
EndProject
104104
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MCP-01-HuggingFace", "MCP-01-HuggingFace\MCP-01-HuggingFace.csproj", "{769320A2-CE09-41EF-A345-8BE6ABBE2696}"
105105
EndProject
106+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MCP-02-HuggingFace-Ollama", "MCP-02-HuggingFace-Ollama\MCP-02-HuggingFace-Ollama.csproj", "{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}"
107+
EndProject
106108
Global
107109
GlobalSection(SolutionConfigurationPlatforms) = preSolution
108110
Debug|Any CPU = Debug|Any CPU
@@ -545,6 +547,18 @@ Global
545547
{769320A2-CE09-41EF-A345-8BE6ABBE2696}.Release|x64.Build.0 = Release|Any CPU
546548
{769320A2-CE09-41EF-A345-8BE6ABBE2696}.Release|x86.ActiveCfg = Release|Any CPU
547549
{769320A2-CE09-41EF-A345-8BE6ABBE2696}.Release|x86.Build.0 = Release|Any CPU
550+
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
551+
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Debug|Any CPU.Build.0 = Debug|Any CPU
552+
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Debug|x64.ActiveCfg = Debug|Any CPU
553+
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Debug|x64.Build.0 = Debug|Any CPU
554+
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Debug|x86.ActiveCfg = Debug|Any CPU
555+
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Debug|x86.Build.0 = Debug|Any CPU
556+
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Release|Any CPU.ActiveCfg = Release|Any CPU
557+
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Release|Any CPU.Build.0 = Release|Any CPU
558+
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Release|x64.ActiveCfg = Release|Any CPU
559+
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Release|x64.Build.0 = Release|Any CPU
560+
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Release|x86.ActiveCfg = Release|Any CPU
561+
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Release|x86.Build.0 = Release|Any CPU
548562
EndGlobalSection
549563
GlobalSection(SolutionProperties) = preSolution
550564
HideSolutionNode = FALSE
@@ -586,6 +600,7 @@ Global
586600
{5844D64E-6C50-CC23-64C5-02DCC5380E97} = {A02293A0-BFF3-4708-A110-30944AD39A69}
587601
{07F43AC9-EBB1-4F21-BC51-6DAD0F2F0E55} = {1248F5FA-C7D6-4876-8313-A15A942F84FE}
588602
{769320A2-CE09-41EF-A345-8BE6ABBE2696} = {3D13EC01-716A-41D9-BC54-1871EC14D3DE}
603+
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27} = {3D13EC01-716A-41D9-BC54-1871EC14D3DE}
589604
EndGlobalSection
590605
GlobalSection(ExtensibilityGlobals) = postSolution
591606
SolutionGuid = {961EEBAB-4149-4AA4-BEE7-9DAAE4C94133}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<RootNamespace>MCP_02_HuggingFace_Ollama</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.7.1" />
13+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.7" />
14+
<PackageReference Include="ModelContextProtocol" Version="0.3.0-preview.3" />
15+
<PackageReference Include="ModelContextProtocol-SemanticKernel" Version="0.3.0-preview-01" />
16+
<PackageReference Include="ModelContextProtocol.Core" Version="0.3.0-preview.3" />
17+
<PackageReference Include="OllamaSharp" Version="5.3.1" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#pragma warning disable SKEXP0001, SKEXP0070
2+
3+
using Microsoft.Extensions.AI;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.Hosting;
6+
using Microsoft.SemanticKernel.ChatCompletion;
7+
using ModelContextProtocol.Client;
8+
using OllamaSharp;
9+
10+
// To run the sample, you need to set the following environment variables or user secrets:
11+
// "HF_API_KEY": " your HF token"
12+
13+
var builder = Host.CreateApplicationBuilder(args);
14+
var config = builder.Configuration
15+
.AddEnvironmentVariables()
16+
.AddUserSecrets<Program>()
17+
.Build();
18+
var deploymentName = config["deploymentName"] ?? "llama3.2"; // Default to llama3.2 if not specified
19+
20+
// create MCP Client using Hugging Face endpoint
21+
var hfHeaders = new Dictionary<string, string>
22+
{
23+
{ "Authorization", $"Bearer {config["HF_API_KEY"]}" }
24+
};
25+
var clientTransport = new SseClientTransport(
26+
new()
27+
{
28+
Name = "HF Server",
29+
Endpoint = new Uri("https://huggingface.co/mcp"),
30+
AdditionalHeaders = hfHeaders
31+
});
32+
await using var mcpClient = await McpClientFactory.CreateAsync(clientTransport);
33+
34+
// Display the available server tools
35+
var tools = await mcpClient.ListToolsAsync();
36+
foreach (var tool in tools)
37+
{
38+
Console.WriteLine($"Connected to server with tools: {tool.Name}");
39+
}
40+
Console.WriteLine("Press Enter to continue...");
41+
Console.ReadLine();
42+
Console.WriteLine();
43+
44+
// create an IChatClient using the MCP tools
45+
var uri = new Uri("http://localhost:11434");
46+
var client = new OllamaApiClient(uri, deploymentName)
47+
.AsChatCompletionService()
48+
.AsChatClient()
49+
.AsBuilder()
50+
.UseFunctionInvocation()
51+
.Build();
52+
53+
var chatOptions = new ChatOptions
54+
{
55+
Tools = [.. tools],
56+
ModelId = deploymentName
57+
};
58+
59+
// Create image
60+
Console.WriteLine("Starting the process to generate an image of a pixelated puppy...");
61+
var query = "Create an image of a pixelated puppy.";
62+
var result = await client.GetResponseAsync(query, chatOptions);
63+
Console.Write($"AI response: {result}");
64+
Console.WriteLine();
65+

10-WhatsNew/readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
This page tracks the history of new features, tools, and models added to the course. Check back for updates!
44

5+
## July 2025
6+
7+
### **🕹️ New! Apps Generated with AI**
8+
9+
We're excited to introduce a new section featuring full applications generated with AI tools like GitHub Copilot Agent. These apps demonstrate how generative AI can be used to build real-world .NET solutions leveraging AI as a co-pilot.
10+
11+
- **First Sample: SpaceAINet**
12+
- [SpaceAINet Console App](./05-AppCreatedWithGenAI/SpaceAINet/README.md)
13+
- **SpaceAINet** is an AI-powered Space Battle game for .NET 9, designed to showcase how modern AI models can play classic games. The solution allows you to run the game with either local AI models (via Ollama) or cloud-based models (via Azure AI Foundry), which analyze the game state and predict the next best action to win.
14+
- Try it out and see how AI can master classic arcade gameplay!
15+
516
## June 2025
617

718
### New: Azure OpenAI Sora Video Generation Demo

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ Don't forget to [star (🌟) this repo](https://docs.github.com/en/get-started/e
3232

3333
We're constantly improving this course with the latest AI tools, models, and practical samples:
3434

35+
- **New: Hugging Face MCP Server Image Generation Samples!**
36+
- Explore new C# console app samples that show how to use the Hugging Face MCP Server to generate images directly from your code.
37+
- Try the sample using GitHub Models or Azure AI Foundry: [`03-CoreGenerativeAITechniques/src/MCP-01-HuggingFace/Program.cs`](./03-CoreGenerativeAITechniques/src/MCP-01-HuggingFace/Program.cs)
38+
- Or use Ollama for local model inference: [`03-CoreGenerativeAITechniques/src/MCP-02-HuggingFace-Ollama/Program.cs`](./03-CoreGenerativeAITechniques/src/MCP-02-HuggingFace-Ollama/Program.cs)
39+
- These samples demonstrate how to integrate image generation into .NET console applications using modern AI model providers.
40+
3541
- **🕹️ New! Apps Generated with AI**
3642

3743
We're excited to introduce a new section featuring full applications generated with AI tools like GitHub Copilot Agent. These apps demonstrate how generative AI can be used to build real-world .NET solutions leveraging AI as a co-pilot.
@@ -41,11 +47,6 @@ We're excited to introduce a new section featuring full applications generated w
4147
- **SpaceAINet** is an AI-powered Space Battle game for .NET 9, designed to showcase how modern AI models can play classic games. The solution allows you to run the game with either local AI models (via Ollama) or cloud-based models (via Azure AI Foundry), which analyze the game state and predict the next best action to win.
4248
- Try it out and see how AI can master classic arcade gameplay!
4349

44-
- **New: Foundry Local demos!**
45-
- Lesson 3 now features hands-on demos for [Foundry Local models](https://github.com/microsoft/Foundry-Local/tree/main).
46-
- See the official docs: [Foundry Local documentation](https://learn.microsoft.com/azure/ai-foundry/foundry-local/)
47-
- **Full explanation and code samples are available in [03-CoreGenerativeAITechniques/06-LocalModelRunners.md](./03-CoreGenerativeAITechniques/06-LocalModelRunners.md)**
48-
4950
## 🚀 Introduction
5051

5152
Generative AI is transforming software development, and .NET is no exception. This course aims to simplify the journey by offering:

0 commit comments

Comments
 (0)