![]() |
VOOZH | about |
Prefer using MCP servers with Semantic Kernel or Microsoft Agent Framework in combination with OllamaSharp.
dotnet add package OllamaSharp.ModelContextProtocol --version 5.4.19
NuGet\Install-Package OllamaSharp.ModelContextProtocol -Version 5.4.19
<PackageReference Include="OllamaSharp.ModelContextProtocol" Version="5.4.19" />
<PackageVersion Include="OllamaSharp.ModelContextProtocol" Version="5.4.19" />Directory.Packages.props
<PackageReference Include="OllamaSharp.ModelContextProtocol" />Project file
paket add OllamaSharp.ModelContextProtocol --version 5.4.19
#r "nuget: OllamaSharp.ModelContextProtocol, 5.4.19"
#:package OllamaSharp.ModelContextProtocol@5.4.19
#addin nuget:?package=OllamaSharp.ModelContextProtocol&version=5.4.19Install as a Cake Addin
#tool nuget:?package=OllamaSharp.ModelContextProtocol&version=5.4.19Install as a Cake Tool
👁 nuget version
👁 nuget downloads
👁 Api docs
OllamaSharp provides .NET bindings for the Ollama API, simplifying interactions with Ollama both locally and remotely.
| Topic | Description |
|---|---|
| Getting started | Installation, client setup and first steps |
| Chat and Generate | Chat class, GenerateAsync, streaming, images, structured output, thinking models |
| Model Management | List, pull, push, copy, delete, show, embed |
| Tool support | Function calling with source generators |
| Advanced Configuration | Configuration options that go beyond the basics |
| Native AOT support | Ahead-of-Time compilation guidance |
| API reference | Full auto-generated API docs |
OllamaSharp wraps each Ollama API endpoint in awaitable methods that fully support response streaming.
The following list shows a few simple code examples.
ℹ Try our full featured that's included in this repository
// set up the client
var uri = new Uri("http://localhost:11434");
var ollama = new OllamaApiClient(uri);
// select a model which should be used for further operations
ollama.SelectedModel = "qwen3.5:35b-a3b";
For .NET Native AOT scenarios, create a custom JsonSerializerContext with your types and pass it into the constructor.
[JsonSerializable(typeof(MyCustomType))]
public partial class MyJsonContext : JsonSerializerContext { }
// Use the static factory method for NativeAOT
var ollama = new OllamaApiClient(uri, "qwen3.5:35b-a3b", MyJsonContext.Default);
See the for detailed guidance.
var models = await ollama.ListLocalModelsAsync();
await foreach (var status in ollama.PullModelAsync("qwen3.5:35b-a3b"))
Console.WriteLine($"{status.Percent}% {status.Status}");
GenerateAsync maps to the /api/generate endpoint and is ideal for single-turn, context-free completions.
await foreach (var stream in ollama.GenerateAsync("How are you today?"))
Console.Write(stream.Response);
The Chat class is the recommended way to build conversational applications. It automatically tracks the full message history (including tool calls and their results) across turns so the model always has full context.
// messages including their roles and tool calls will automatically be tracked within the chat object
// and are accessible via the Messages property
var chat = new Chat(ollama);
while (true)
{
var message = Console.ReadLine();
await foreach (var answerToken in chat.SendAsync(message))
Console.Write(answerToken);
}
You can also set a system prompt, send images for vision models, request structured JSON output, and enable thinking mode for reasoning models. See the Chat and Generate documentation for the full guide.
Microsoft built an abstraction library to streamline the usage of different AI providers. This is a really interesting concept if you plan to build apps that might use different providers, like ChatGPT, Claude and local models with Ollama.
I encourage you to read their accouncement Introducing Microsoft.Extensions.AI Preview – Unified AI Building Blocks for .NET.
OllamaSharp is the first full implementation of their IChatClient and IEmbeddingGenerator that makes it possible to use Ollama just like every other chat provider.
To do this, simply use the OllamaApiClient as IChatClient instead of IOllamaApiClient.
// install package Microsoft.Extensions.AI.Abstractions
private static IChatClient CreateChatClient(Arguments arguments)
{
if (arguments.Provider.Equals("ollama", StringComparison.OrdinalIgnoreCase))
return new OllamaApiClient(arguments.Uri, arguments.Model);
else
return new OpenAIChatClient(new OpenAI.OpenAIClient(arguments.ApiKey), arguments.Model); // ChatGPT or compatible
}
The OllamaApiClient implements both interfaces from Microsoft.Extensions.AI, you just need to cast it accordingly:
IChatClient for model inferenceIEmbeddingGenerator<string, Embedding<float>> for embedding generationOllamaSharp can be used with Ollama cloud models as well. Use the constructor that takes an HttpClient and set it up to send the api key as default request header.
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:11434");
client.DefaultRequestHeaders.Add(/* your api key here */);
var ollama = new OllamaApiClient(client);
It can be confusing which library to use with AI in C#. The following paragraph should help you decide which library to start with.
Prefer OllamaSharp if ...
Prefer Microsoft.Extensions.AI if ...
Prefer Semantic Kernel if ...
No matter which one you choose, OllamaSharp should always be the bridge to Ollama behind the scenes as recommended by Microsoft (1) (2) (3).
I would like to thank all the contributors who take the time to improve OllamaSharp. First and foremost mili-tan, who always keeps OllamaSharp in sync with the Ollama API.
The icon and name were reused from the amazing Ollama project.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 5.4.19 | 525 | 3/4/2026 | 5.4.19 is deprecated because it is no longer maintained. |
| 5.4.18 | 207 | 2/27/2026 | |
| 5.4.17 | 78 | 2/27/2026 | |
| 5.4.16 | 598 | 1/28/2026 | |
| 5.4.15 | 77 | 1/28/2026 | |
| 5.4.13 | 114 | 1/26/2026 | |
| 5.4.12 | 720 | 12/5/2025 | |
| 5.4.11 | 537 | 11/14/2025 | |
| 5.4.10 | 322 | 11/11/2025 | |
| 5.4.9 | 726 | 11/10/2025 | |
| 5.4.8 | 613 | 10/17/2025 | |
| 5.4.7 | 433 | 9/25/2025 | |
| 5.4.6 | 5,562 | 9/22/2025 | |
| 5.4.5 | 278 | 9/19/2025 | |
| 5.4.4 | 324 | 9/15/2025 | |
| 5.4.3 | 305 | 9/15/2025 | |
| 5.4.2 | 273 | 9/15/2025 | |
| 5.4.1 | 221 | 9/11/2025 | |
| 5.3.12 | 487 | 9/10/2025 | |
| 5.3.11 | 184 | 9/10/2025 |