AI Agents in .NET: Build Production-Ready AI Agents with Semantic Kernel and MCP

Divya Vijayan By Divya Vijayan on September 16, 2026

AI agents in .NET have moved from demos to production systems. The conversation has shifted from “look, the model can call a function” to “how do we run this reliably, securely, and affordably in front of real users?” That second question is an engineering question — and the .NET platform is exceptionally well suited to answer it.

This article explains how developers can build AI agents in .NET using Semantic Kernel, Microsoft.Extensions.AI, Model Context Protocol (MCP), and retrieval-augmented generation (RAG). Drawing on the enterprise engineering experience of PIT Solutions, it focuses on reliability, security, observability, testing, and cost control rather than a demo-only implementation.

What Are AI Agents in .NET?

For developers working on .NET applications, an AI agent is an application that gives an AI model a goal, a set of tools, and an execution loop. The model decides what to do, while the surrounding .NET code controls how it happens.

  • A chatbot replies to messages.
  • A RAG system retrieves relevant documents and answers once.
  • An agent is given a goal, a set of tools, and an execution loop. It reasons, decides which tool to call, observes the result, and repeats until the goal is met.

reason -> call a tool -> observe result -> reason again -> ... -> finish

The control layer — dependency injection, tool contracts, authorization, resilience, state, and telemetry — is where production engineering lives.

Key terms at a glance

Term What it means
LLM Large Language Model — the language “brain” (GPT, Claude, Gemini). It predicts text and reasons over what you send it.
Prompt The text (your instructions + data) that your application sends to the LLM.
Token The unit of text an LLM reads and writes (roughly ¾ of a word). You are billed per token.
Tool / function A C# method the model is allowed to call to fetch real data or take an action.
Agent Your application that gives an LLM tools plus an execution loop so it can take actions.
Hallucination When the LLM produces confident but incorrect output — the reason grounding and validation matter.

Why Building Production AI Agents Is Different From a Demo

A demo works on the happy path on a laptop. A production-ready AI agent must survive failures, rate limits, unexpected tool output, security threats, cost spikes, and non-deterministic model behavior. The difference is not the prompt — it is the engineering around the model.

Concern Demo Production
Reliability one call, crashes on error timeouts, retries, circuit breakers, fallbacks
Cost control unlimited tokens and loops token budgets, max iterations, per-request caps
Safety model can call any tool least-privilege tools, approval for risky actions
Security API key in source Key Vault / managed identity, prompt-injection guards
Observability Console.WriteLine OpenTelemetry traces of prompts, tokens, tool calls
Correctness “looks right” evaluation tests with golden prompts
Operability runs locally hosted, scalable, monitored ASP.NET Core service

Most of these concerns are familiar territory for enterprise .NET development. Building production AI agents is largely a matter of applying dependency injection, resilience, secret management, authorization, OpenTelemetry, and automated testing to a new kind of workload.

The .NET AI Stack for Building Production AI Agents

A practical stack for AI agent development in .NET combines provider-agnostic model abstractions, orchestration, tool interoperability, and enterprise-grade application infrastructure. This AI agent architecture separates model access, orchestration, tools, knowledge, and production controls.

  • Microsoft.Extensions.AI provides provider-agnostic abstractions such as IChatClient and IEmbeddingGenerator. This lets a .NET application work with different model providers without rewriting the application layer.
  • Semantic Kernel provides orchestration through the Kernel, plugins, and agent capabilities. For teams evaluating Semantic Kernel .NET and AI agents with Semantic Kernel, it can serve as an AI agent framework for .NET, exposing C# functions as tools so the model can interact with application services through typed contracts.

Model Context Protocol (MCP)

MCP is an open standard for exposing tools and data to AI applications. Model Context Protocol in .NET scenarios is useful when an agent needs governed access to shared capabilities such as GitHub, databases, or internal APIs. This is part of a broader .NET AI development approach in which provider-specific model access and external tool integrations remain behind clear application boundaries. A .NET application can also expose its own capabilities through an MCP server.

AI Model Providers

The model provider supplies the underlying chat or embedding model. Keep provider-specific configuration at the infrastructure boundary so the agent and business logic remain as portable as practical.

How to Build an AI Agent in .NET

The implementation below builds an agent incrementally: register the model, create the Semantic Kernel, add local tools, connect MCP tools, ground responses with RAG, and run the agent loop.

Register the AI Model

Everything starts with  IChatClient, wired through standard dependency injection.

 

builder.Services
    .AddChatClient(sp =>
        new AzureOpenAIClient(
            new Uri(config["AzureOpenAI:Endpoint"]!),
            new DefaultAzureCredential())        // managed identity, no keys in code
        .GetChatClient(config["AzureOpenAI:Deployment"]!)
        .AsIChatClient())
    .UseFunctionInvocation()                     // enables tool calling
    .UseOpenTelemetry()                          // traces every model call
    .UseLogging();

 

Notice what comes for free: no API keys in source, automatic tool invocation, and telemetry — all through the same middleware pattern used elsewhere in .NET.

Create the Semantic Kernel

Create the Kernel around the registered AI services and application dependencies. The Kernel becomes the composition point for the model, plugins, MCP tools, and other services the agent can use.

Step 1: Add Tools and Plugins

A tool is just a C# method the model is allowed to call. Keep inputs and outputs typed.

 

public sealed class OrdersPlugin(IOrderService orders)
{
    [KernelFunction, Description("Gets the current status of a customer order by id.")]
    public async Task<OrderStatus> GetOrderStatusAsync(
        [Description("The order identifier, e.g.: ORD-10293")] string orderId)
        => await orders.GetStatusAsync(orderId);
}

 

The model reads the descriptions to decide when to call this method. Treat descriptions as part of the contract — they are effectively the API documentation the model reads.

Step 2: Connect MCP Tools

These tools are functions you wrote inside your own app. But some data or capability lives outside your app or is owned by another team — and you don’t want to re-implement it everywhere. MCP is a standard plug format for tools — like a USB port for AI. If a capability is exposed as an MCP server, any AI app can plug in and use it without custom integration code.Consuming an external MCP server takes a few lines, and the tools plug straight into the kernel alongside local plugins.

 

public sealed class OrdersPlugin(IOrderService orders)
{
    [KernelFunction, Description("Gets the current status of a customer order by id.")]
    public async Task<OrderStatus> GetOrderStatusAsync(
        [Description("The order identifier, e.g.: ORD-10293")] string orderId)
        => await orders.GetStatusAsync(orderId);
}
 

Using Model Context Protocol (MCP) with AI Agents in .NET

MCP is most valuable when tools or data are shared across applications or teams. In a production .NET system, treat MCP servers as governed integration boundaries: authenticate connections, restrict available tools, validate tool inputs and outputs, and monitor calls just as you would for any other enterprise API.

Add RAG and External Knowledge

The LLM only knows what it was trained on — not your company documents, policies, or anything recent or private. Ask it about those and it may guess. Grounding forces the model to answer from your real content instead of its memory, which is what RAG (Retrieval-Augmented Generation) does:

  • Retrieve the relevant documents from your knowledge base,
  • Augment the prompt by inserting those documents,
  • the LLM generates an answer grounded in them.

Documents are converted into embeddings (numeric representations of meaning) and kept in a vector store (Azure AI Search, Qdrant, or pgvector via  Microsoft.Extensions.VectorData). Retrieval is then exposed as just another tool the agent can call when it needs background knowledge, instead of stuffing everything into the prompt.

 

[KernelFunction, Description("Searches the firm's policies and research notes.")]
public async Task<string> SearchResearch(string query)
    => await _vectorStore.SearchAsync(query);  // returns the most relevant document
chunks

 

Adding RAG to .NET AI Agents for Grounded Responses

RAG helps an agent answer from current, private, or company-specific information instead of relying only on model training data. Documents are embedded and stored in a vector store, then retrieved as context when the agent needs grounded knowledge. In a production system, retrieval should be treated as a tool with clear access controls, relevance checks, and observability.

Run the Agent

 

ChatCompletionAgent agent = new()
{
    Name = "support-agent",
    Instructions = "Help customers with orders. Use tools for facts. Never guess an
order status.",
    Kernel = kernel,
    Arguments = new(new PromptExecutionSettings
    {
        FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
    })
};
await foreach (var response in agent.InvokeStreamingAsync(userMessage, thread))
    yield return response.Message.Content;  // stream tokens to the UI

 

That’s a working agent. The remaining work is what makes it production-grade.

Production Engineering for AI Agents

The model is the easy part; the engineering around it is what makes production-ready AI agents reliable, secure, cost-bounded, observable, and testable.

Reliability and Resilience

Model endpoints fail, rate-limit, and time out. Wrap them with the standard resilience stack:

 

builder.Services.ConfigureHttpClientDefaults(http =>
    http.AddStandardResilienceHandler(o =>
    {
        o.AttemptTimeout.Timeout       = TimeSpan.FromSeconds(30);
        o.TotalRequestTimeout.Timeout = TimeSpan.FromSeconds(90);
        o.Retry.MaxRetryAttempts      = 3;
    }));

 

The same capability can be published as an MCP server so other agents reuse it — one implementation, exposed once and governed centrally. (If your app only ever uses its own data, you can skip MCP; it earns its place when tools are shared across apps or teams.)

Cost and Token Controls

An agent that loops forever burns money. Cap iterations and tokens per request, and set a budget. Tokens deserve to be treated as a first-class resource, much like database connections — just as you would never open unlimited database connections, never make unlimited or uncapped model calls. In the systems we have shipped, the first runaway loop almost always surfaces in the monthly cloud bill before it surfaces in the logs.

Human-in-the-Loop Controls

Not every tool should fire automatically. Read-only tools such as get order status can run freely; destructive actions such as issuing a refund or placing a trade should require explicit human approval. Apply least privilege to tools exactly as you would to a service account: give the agent only the tools it actually needs, nothing more.

Security and Tool Authorization

  • Secrets in Key Vault, access via managed identity — never in source or prompts. These controls form a practical foundation for AI agent security.
  • Treat all tool and retrieved data as untrusted input. A document or API response can contain hidden “prompt-injection” instructions that the model may obey. The principle mirrors classic output sanitization: never trust content that wasn’t generated locally. On the regulated workloads we work with, every tool and document response is treated as hostile by default.
  • Validate the model’s structured output before acting on it — confirm the JSON is well-formed and the values are real (for example, that a returned stock symbol actually exists) before showing it to users or taking action.

Observability

With OpenTelemetry wired in at registration, every prompt, token count, latency figure, and tool call becomes a span. That makes it possible to answer “why did the agent do that, and what did it cost?” — non-negotiable for operating AI in production. This is central to AI agent observability in production.

Testing and Evaluation

Agents are non-deterministic, so exact-match assertions don’t work. Unit-test the tools normally; evaluate the agent with a suite of golden prompts scored for correctness, safety, and cost. This evaluation approach is a core part of AI agent testing.

What Can Go Wrong When an AI Agent Reaches Production

Production AI agents can fail in ways that are different from conventional deterministic services. Plan for:

  • Non-determinism — the same input can produce different output; test with evaluations.
  • Runaway tool loops — cap iterations and per-request token budgets.
  • Latency — stream responses and parallelize independent tool calls.
  • Prompt injection via tool data — untrusted by default.
  • Over-permissioned tools — least privilege; gate destructive actions.
  • Context-window limits — summarize and trim history; don’t dump everything.

When Should You Use an AI Agent in a .NET Application?

Use an AI agent when a workflow benefits from goal-driven reasoning, dynamic tool selection, or interaction with multiple sources of information. Examples include support workflows that need to query orders and policies, research assistants that retrieve enterprise knowledge, and operations workflows that combine APIs with human approval. Prefer conventional application logic when the workflow is deterministic, rules are explicit, and there is little value in model-driven decision-making.

How PIT Solutions Helps Build Enterprise AI Agents

The team combines enterprise .NET development with AI and Data Science capabilities to build secure, scalable AI solutions. Its .NET development practice covers enterprise applications, APIs, cloud modernization, security, and DevOps, while its AI and Data Science practice includes Agentic AI (Autonomous Agents). For organizations evaluating AI agent development, this combination helps connect agent capabilities to existing enterprise systems and operational requirements.

Ready to Build Production-Ready AI Agents?

Ready to move beyond an AI demo? Talk to PIT Solutions about designing and building production-ready AI agents in .NET with the right architecture, security controls, observability, and enterprise integration for your use case.

Explore related capabilities: .NET Application Development | AI & Data Science | Agentic AI / Autonomous Agents

Semantic Kernel and the Evolving .NET Agent Ecosystem

Semantic Kernel remains a practical framework for building AI-enabled .NET applications and agent-based solutions. At the same time, Microsoft is evolving its agent ecosystem with Microsoft Agent Framework, which builds on the experience of Semantic Kernel and AutoGen.

For teams already using Semantic Kernel, the concepts covered in this article—tool calling, MCP, RAG, security, observability, and production controls—remain valuable when building modern .NET AI solutions. Microsoft Agent Framework provides another path for teams looking to adopt newer agentic workflow capabilities as the ecosystem evolves.

Conclusion

Building an AI agent that demos well can take an afternoon. Building one that runs in production — reliable, secure, cost-bounded, observable, and testable — takes real software engineering. That is good news for .NET teams: the platform already provides dependency injection, resilience, secret management, OpenTelemetry, and a mature testing culture. Semantic Kernel and MCP add the AI orchestration and interoperability layer on top.

In our experience at PIT Solutions, the agents that reach production are not the flashiest demos. They are the ones engineered like every other service trusted in production — and .NET is a first-class platform for building them.