Talk to Your Database Using AI with .NET 9

A Practical Approach with MCP and OpenAI
Modern applications are slowly moving away from rigid query-based data access. Instead of writing SQL every time, users increasingly expect to ask questions in natural language and get clear answers.
Using .NET 9, OpenAI, and the Model Context Protocol (MCP), we can build a controlled and secure system where AI helps users interact with databases—without giving AI direct or unsafe access.
This section explains the idea briefly and shows the minimum working code needed to get started.
How the Flow Works (Short Version)
1. User asks a question like: “Show me all tables in my database” 2. ASP.NET Core API sends the question to OpenAI 3. OpenAI discovers available MCP tools 4. MCP server executes a safe database operation 5. AI converts the result into a readable answer The database is never exposed directly.
Program.cs (ASP.NET Core API – .NET 9)
This is the API gateway that connects OpenAI and the MCP server.
using Microsoft.Extensions.AI; using ModelContextProtocol.Client; using OpenAI; using System.ClientModel; var builder = WebApplication.CreateBuilder(args); // Enable OpenAPI
builder.Services.AddOpenApi(); // Load OpenAI API key from User Secrets var
apiKey = builder.Configuration["OpenAI:ApiKey"]; var credential = new ApiKeyCredential(apiKey); // Create OpenAI chat client IChatClient
chatClient = new OpenAIClient(credential) .GetChatClient("gpt-4o-mini") .AsIChatClient(); // Register as singleton
builder.Services.AddSingleton(chatClient); var app = builder.Build(); // MCP client using stdio transport
var mcpOptions = new StdioClientTransportOptions { Name = "TalkToDb MCP Server", Command = "dotnet", Arguments = [ "run", "--project", "../TalkToDb.MCPServer/TalkToDb.MCPServer.csproj" ] }; var transport = new StdioClientTransport(mcpOptions); await using var mcpClient = await McpClient.CreateAsync(transport); if (app.Environment.IsDevelopment()) { app.MapOpenApi(); } app.UseHttpsRedirection(); // Simple test endpoint
app.MapGet("/ask", async (IChatClient client, string query) => { var message = new ChatMessage(ChatRole.User, query); var response = await client.GetResponseAsync(message); return response.Text; }); app.Run();