dotnet-ai
.NET AI integration with Microsoft.Extensions.AI, Semantic Kernel, MCP servers, embeddings, vector search, and agent frameworks
What this skill does
# .NET AI Integration
## Microsoft.Extensions.AI (Unified AI Abstraction)
The recommended way to integrate AI in .NET apps. Provides a vendor-agnostic abstraction over AI services.
```csharp
// Install: dotnet add package Microsoft.Extensions.AI
// Provider packages: Microsoft.Extensions.AI.OpenAI, Microsoft.Extensions.AI.AzureAIInference, etc.
using Microsoft.Extensions.AI;
// Register in DI
builder.Services.AddChatClient(new AzureOpenAIClient(
new Uri(builder.Configuration["AI:Endpoint"]!),
new DefaultAzureCredential())
.GetChatClient("gpt-4o"));
// Or with OpenAI directly
builder.Services.AddChatClient(new OpenAIClient(apiKey)
.GetChatClient("gpt-4o"));
```
### Chat Completion
```csharp
public sealed class ChatService(IChatClient chatClient)
{
public async Task<string> AskAsync(string question, CancellationToken ct)
{
var response = await chatClient.GetResponseAsync(question, cancellationToken: ct);
return response.Text;
}
public async Task<string> AskWithContextAsync(string question, string systemPrompt, CancellationToken ct)
{
var messages = new List<ChatMessage>
{
new(ChatRole.System, systemPrompt),
new(ChatRole.User, question)
};
var response = await chatClient.GetResponseAsync(messages, cancellationToken: ct);
return response.Text;
}
// Streaming
public async IAsyncEnumerable<string> StreamAsync(
string prompt, [EnumeratorCancellation] CancellationToken ct = default)
{
await foreach (var update in chatClient.GetStreamingResponseAsync(prompt, cancellationToken: ct))
{
if (update.Text is not null)
yield return update.Text;
}
}
}
```
### Function Calling (Tool Use) - from official docs
```csharp
using Microsoft.Extensions.AI;
using OpenAI;
// Build client with function invocation middleware
IChatClient client =
new ChatClientBuilder(new OpenAIClient(key).GetChatClient("gpt-4o").AsIChatClient())
.UseFunctionInvocation() // Auto-invokes local functions
.Build();
// Define tools available to the model
var chatOptions = new ChatOptions
{
Tools = [AIFunctionFactory.Create((string location, string unit) =>
{
return "Periods of rain or drizzle, 15 C";
},
"get_current_weather",
"Gets the current weather in a given location")]
};
// Conversation with automatic tool invocation
List<ChatMessage> chatHistory =
[
new(ChatRole.System, "You are a hiking enthusiast who helps discover fun hikes."),
new(ChatRole.User, "I live in Montreal. What's the current weather like?")
];
ChatResponse response = await client.GetResponseAsync(chatHistory, chatOptions);
Console.WriteLine(response.Text); // Model auto-called get_current_weather
```
### Embeddings
```csharp
// IEmbeddingGenerator<string, Embedding<float>>
builder.Services.AddEmbeddingGenerator(new AzureOpenAIClient(endpoint, credential)
.GetEmbeddingClient("text-embedding-3-small"));
public sealed class SemanticSearchService(IEmbeddingGenerator<string, Embedding<float>> embedder)
{
public async Task<float[]> GetEmbeddingAsync(string text, CancellationToken ct)
{
var embedding = await embedder.GenerateAsync(text, cancellationToken: ct);
return embedding[0].Vector.ToArray();
}
public async Task<IReadOnlyList<float[]>> GetBatchEmbeddingsAsync(
IEnumerable<string> texts, CancellationToken ct)
{
var embeddings = await embedder.GenerateAsync(texts.ToList(), cancellationToken: ct);
return embeddings.Select(e => e.Vector.ToArray()).ToList();
}
}
```
## Semantic Kernel (AI Orchestration)
```csharp
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
// Build kernel
var kernel = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion("gpt-4o", endpoint, credential)
.Build();
// Simple prompt
var result = await kernel.InvokePromptAsync("Summarize: {{$input}}", new() { ["input"] = text });
// With plugins
kernel.Plugins.AddFromType<TimePlugin>();
kernel.Plugins.AddFromType<WeatherPlugin>();
// Auto function calling
var settings = new OpenAIPromptExecutionSettings { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
var chatService = kernel.GetRequiredService<IChatCompletionService>();
var response = await chatService.GetChatMessageContentAsync("What time is it in London?", settings, kernel);
```
## MCP (Model Context Protocol) in .NET - from official docs
### Build MCP Server
```bash
# Requires .NET 10.0 SDK
dotnet new install Microsoft.McpServer.ProjectTemplates
dotnet new mcpserver -n MyMcpServer
```
```csharp
// Program.cs
using ModelContextProtocol.Server;
using System.ComponentModel;
var hostBuilder = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
services.AddMcpServer(options =>
{
options.Name = "SampleMcpServer";
options.Version = "1.0";
})
.WithStdioServerTransport() // or .WithHttpServerTransport()
.AddMcpServerTools();
});
var host = hostBuilder.Build();
await host.RunAsync();
```
```csharp
// Tool definitions
public class RandomNumberTools
{
[McpServerTool]
[Description("Gets a random number between min and max")]
public string GetRandomNumber(
[Description("Minimum value")] int min,
[Description("Maximum value")] int max)
{
return $"Your random number is {Random.Shared.Next(min, max + 1)}.";
}
[McpServerTool]
[Description("Describes random weather in the provided city")]
public string GetCityWeather(
[Description("Name of the city")] string city)
{
var weather = Environment.GetEnvironmentVariable("WEATHER_CHOICES") ?? "balmy,rainy,stormy";
var choices = weather.Split(",");
return $"The weather in {city} is {choices[Random.Shared.Next(0, choices.Length)]}.";
}
}
```
### MCP Server Config (.vscode/mcp.json)
```json
{
"servers": {
"MyMcpServer": {
"type": "stdio",
"command": "dotnet",
"args": ["run", "--project", "<path-to-csproj>"],
"env": { "WEATHER_CHOICES": "sunny,humid,freezing" }
}
}
}
```
### Build MCP Client
```csharp
using ModelContextProtocol.Client;
using Microsoft.Extensions.AI;
// Create MCP client connection
var transport = new StdioClientTransport(new()
{
Command = "dotnet run",
Arguments = ["--project", "<path-to-mcp-server>"],
Name = "Minimal MCP Server",
});
McpClient mcpClient = await McpClient.CreateAsync(transport);
// Discover tools
IList<McpClientTool> tools = await mcpClient.ListToolsAsync();
foreach (McpClientTool tool in tools)
Console.WriteLine(tool);
// Integrate MCP tools with chat client
IChatClient chatClient = new ChatClientBuilder(baseClient)
.UseFunctionInvocation()
.Build();
// Use MCP tools in chat
List<ChatMessage> messages = [new(ChatRole.User, "What's the weather in Paris?")];
await foreach (var update in chatClient.GetStreamingResponseAsync(
messages, new() { Tools = [.. tools] }))
{
Console.Write(update);
}
```
## Vector Search
```csharp
// Using Microsoft.Extensions.VectorData
using Microsoft.Extensions.VectorData;
public sealed class ProductSearchVector
{
[VectorStoreRecordKey]
public int Id { get; set; }
[VectorStoreRecordData]
public string Name { get; set; } = "";
[VectorStoreRecordData]
public string Description { get; set; } = "";
[VectorStoreRecordVector(1536)] // OpenAI embedding dimension
public ReadOnlyMemory<float> Embedding { get; set; }
}
// Search
public sealed class VectorSearchService(
IVectorStore vectorStore,
IEmbeddingGenerator<string, Embedding<float>> embedder)
{
public async Task<IReadOnlyList<ProductSearchVector>> SearchAsync(
string query, int topK = 5, CancellationToken ct = default)
{
Related in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.