DEV Community

Nikhil Wagh
Nikhil Wagh

Posted on

Model Context Protocol in .NET: Build Smarter AI Agents with OpenAI & MCP

Introduction

In 2025, we're moving beyond simple prompt-response interactions with AI. It's no longer enough to just send a prompt to GPT and get a block of text. Developers now want AI agents that can take action—read files, update databases, fetch web content, and even commit code.

Enter Model Context Protocol (MCP) — a new standard introduced by Anthropic and quickly embraced by OpenAI and Google. It acts like a universal bridge between your AI model and tools — unlocking the next generation of AI-driven apps.

In this post, you’ll learn:

  • What MCP is
  • How to use it in a .NET application
  • How to build a practical AI agent that uses real-world tools like SQL and GitHub
  • Best practices for using MCP securely and efficiently

What is MCP?

Model Context Protocol is a new open standard that allows AI models to interact with external tools in a structured, secure, and standardized way.

With MCP:

  • Models can request actions (e.g., “Read this SQL table”)
  • Your app provides a tool interface the model can call
  • The model receives responses from tools and continues the task

Think of it as a language between AI and your app’s ecosystem—a plug-and-play architecture that lets your model “talk to” your codebase, database, APIs, filesystem, and more.

Why Should .NET Developers Care?

.NET apps power a large portion of enterprise software, and now we can extend them with intelligent capabilities using tools like:

  • OpenAI’s Assistants API + MCP support
  • Azure OpenAI with tool-calling features
  • Self-hosted agents that follow MCP specifications

If you’re building apps with:

  • Microservices
  • Enterprise APIs
  • DevOps pipelines
  • Internal tools

…then MCP can help integrate AI into every layer.

How to Use MCP in .NET (Demo Setup)

Let’s create a simple AI Agent that:

  • Reads customer records from a database
  • Summarizes recent support issues
  • Pushes a report to GitHub

Setup

You’ll need:

  • .NET 8 or .NET 10
  • Azure OpenAI or OpenAI API (GPT-4-turbo with tool-calling)
  • A SQL database (e.g., MSSQL or SQLite)
  • GitHub PAT for repo access

Step-by-Step Agent with Tooling

1. Define the Tool Schema

public class CustomerQueryTool : ITool
{
    public string Name => "query_customers";
    public string Description => "Query recent support tickets from database";

    public Task<string> ExecuteAsync(string input)
    {
        var issues = dbContext.SupportTickets
            .OrderByDescending(x => x.CreatedAt)
            .Take(5)
            .Select(x => x.Description)
            .ToList();

        return Task.FromResult(JsonConvert.SerializeObject(issues));
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Add GitHub Writer Tool

public class GitHubWriterTool : ITool
{
    public Task<string> ExecuteAsync(string inputJson)
    {
        var client = new GitHubClient(new ProductHeaderValue("MCPDemo"));
        client.Credentials = new Credentials("YOUR_PAT");

        var content = JsonConvert.DeserializeObject<SummaryData>(inputJson);
        var newFile = client.Repository.Content.CreateFile(
            "your-user/your-repo", 
            $"report-{DateTime.UtcNow:yyyyMMdd}.md", 
            new CreateFileRequest("Auto-generated report", content.Text));

        return Task.FromResult("Pushed to GitHub");
    }
}
Enter fullscreen mode Exit fullscreen mode

Add to Your Assistant

var assistant = new GptAgent("gpt-4", apiKey);
assistant.RegisterTool(new CustomerQueryTool());
assistant.RegisterTool(new GitHubWriterTool());

string result = await assistant.HandleQueryAsync("Summarize customer issues and create report.");
Console.WriteLine(result);
Enter fullscreen mode Exit fullscreen mode

Done! You’ve just created an agent that:

  • Reads from DB
  • Summarizes with GPT
  • Writes a file to GitHub

Security & Best Practices

  1. Isolate tools — Avoid giving the model unrestricted access to file systems or secrets.
  2. Rate-limit tool calls — Prevent abuse or runaway loops.
  3. Validate all inputs/outputs — Never blindly trust model-suggested actions.
  4. Log tool calls — For audit and debugging.
  5. Separate tool logic — Decouple AI tool actions from core business logic.

The Future of MCP in .NET

  • Native Azure Integration: Expect tighter Azure API support with model agents in Azure Functions.
  • MCP ToolKit for .NET: A standard NuGet library will likely emerge.
  • Hybrid Workflows: Combine human and AI workflows across tools like Power Automate and Logic Apps.

Summary

MCP is here to revolutionize how AI interacts with real-world tools—and .NET is perfectly positioned to ride this wave.

Whether you're building internal dashboards, CI bots, or intelligent assistants, MCP lets you:

  • Extend GPT with tool access
  • Maintain security boundaries
  • Build plug-and-play AI services in .NET

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.