DEV Community

Pawel Janda
Pawel Janda

Posted on

Master MCP integration: Building AI database tools with .NET

Learn how to create powerful AI database tools using Model Context Protocol (MCP) and MaIN.NET framework. This tutorial will teach you to build applications that can interact with any database using natural language queries.

What you'll build

A console application that demonstrates:

  • Natural language database queries - Ask questions in plain English
  • MCP server integration - Connect to any database through standardized protocol
  • AI-powered data operations - Create, read, update, and delete records
  • Interactive database assistant - Chat with your data like never before

Why Airtable?

While this tutorial uses Airtable for simplicity and ease of setup, the techniques you'll learn apply to any database engine supported by MCP servers. You can easily adapt this code to work with:

  • SQL Databases: MySQL, PostgreSQL, SQLite, SQL Server
  • NoSQL Databases: MongoDB, Cassandra, Redis
  • Cloud Databases: BigQuery, Snowflake, DynamoDB
  • File Systems: Local files, CSV, JSON data
  • APIs: REST APIs, GraphQL endpoints

The beauty of MCP is that once you understand the pattern, you can swap out Airtable for any other database by simply changing the MCP server configuration!

Prerequisites

  • .NET SDK
  • Node.js (for running MCP servers)
  • Airtable account
  • OpenAI API key
  • basic C# knowledge

Step 1: Set up Airtable database

1.1 Create Airtable base

  1. Go to Airtable and create a new base
  2. Name it "Employees"

1.2 Import sample data

  1. Create a sample CSV file with the following content:
Name,Email,Department,Position,Hire Date,Salary,Is Active
John Smith,john.smith@company.com,Engineering,Senior Developer,2024-01-15,85000,TRUE
Sarah Johnson,sarah.j@company.com,Marketing,Marketing Manager,2024-03-20,75000,TRUE
Mike Davis,mike.davis@company.com,Sales,Sales Representative,2024-06-10,65000,TRUE
Lisa Chen,lisa.chen@company.com,Engineering,Junior Developer,2024-08-05,70000,TRUE
Tom Wilson,tom.wilson@company.com,HR,HR Specialist,2024-02-28,60000,TRUE
Emma Rodriguez,emma.rodriguez@company.com,Finance,Financial Analyst,2023-04-12,72000,TRUE
David Kim,david.kim@company.com,Engineering,DevOps Engineer,2023-07-18,90000,TRUE
Maria Garcia,maria.garcia@company.com,Marketing,Content Specialist,2023-05-25,68000,TRUE
James Brown,james.brown@company.com,Sales,Sales Manager,2023-09-30,82000,TRUE
Anna Lee,anna.lee@company.com,Engineering,Frontend Developer,2023-11-08,78000,TRUE
Enter fullscreen mode Exit fullscreen mode
  1. In Airtable, click "Add or import""CSV file"
  2. Upload the CSV file
  3. Airtable will auto-detect columns - verify the field types:
    • Name: Single line text
    • Email: Single line text
    • Department: Single select (Engineering, Marketing, Sales, HR, Finance)
    • Position: Single line text
    • Hire Date: Date
    • Salary: Number
    • Is Active: Checkbox

1.3 Get Airtable API key

  1. Go to Airtable Personal Access Tokens
  2. Create a new token with scopes:
    • schema.bases:read
    • data.records:read
    • data.records:write
  3. Important: Make sure to grant access to your specific base
  4. Copy the token

Step 2: Create the console application

2.1 Create new project

# Create new console application
dotnet new console -n EmployeeMCP

# Navigate to project
cd EmployeeMCP
Enter fullscreen mode Exit fullscreen mode

2.2 Add MaIN.NET package

# Add MaIN.NET package
dotnet add package MaIN.NET
Enter fullscreen mode Exit fullscreen mode

Step 3: Build the application

3.1 Create Program.cs

Replace the default Program.cs with:

using MaIN.Core;
using MaIN.Core.Hub;
using MaIN.Domain.Configuration;
using MaIN.Domain.Entities;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

// Check environment variables
var airtableApiKey = Environment.GetEnvironmentVariable("AIRTABLE_API_KEY");
var openAiApiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");

if (string.IsNullOrEmpty(airtableApiKey))
{
    Console.WriteLine("❌ AIRTABLE_API_KEY not set!");
    Console.WriteLine("Please set: export AIRTABLE_API_KEY='your-airtable-token'");
    return;
}

if (string.IsNullOrEmpty(openAiApiKey))
{
    Console.WriteLine("❌ OPENAI_API_KEY not set!");
    Console.WriteLine("Please set: export OPENAI_API_KEY='your-openai-key'");
    return;
}

// Initialize MaIN.NET
var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddEnvironmentVariables()
    .Build();

var services = new ServiceCollection();
services.AddMaIN(configuration, settings =>
{
    settings.BackendType = BackendType.OpenAi;
    settings.OpenAiKey = openAiApiKey;
});

var serviceProvider = services.BuildServiceProvider();
serviceProvider.UseMaIN();

Console.WriteLine("🚀 Employee MCP Console Application");
Console.WriteLine("===================================");
Console.WriteLine("✅ AIHub initialized successfully!");
Console.WriteLine();

// Configure Airtable MCP
var airtableMcp = new Mcp
{
    Name = "Airtable",
    Command = "npx",
    Arguments = ["-y", "airtable-mcp-server"],
    Model = "gpt-4o-mini",
    Backend = BackendType.OpenAi,
    EnvironmentVariables = new Dictionary<string, string>
    {
        ["AIRTABLE_API_KEY"] = airtableApiKey
    }
};

Console.WriteLine("🔧 Airtable MCP Configuration:");
Console.WriteLine($"   Model: {airtableMcp.Model}");
Console.WriteLine($"   Backend: {airtableMcp.Backend}");
Console.WriteLine();

// Interactive mode
Console.WriteLine("🎯 Interactive Employee Query Mode");
Console.WriteLine("==================================");
Console.WriteLine("Ask questions about your employee data!");
Console.WriteLine("Examples:");
Console.WriteLine("- 'Show me all employees'");
Console.WriteLine("- 'List employees in Engineering department'");
Console.WriteLine("- 'Who was hired in 2023?'");
Console.WriteLine("- 'Add a new employee: John Doe, john@company.com, Engineering, Developer'");
Console.WriteLine("Type 'exit' to quit.");
Console.WriteLine();

while (true)
{
    Console.Write("🤖 Query: ");
    var userInput = Console.ReadLine();

    if (string.IsNullOrWhiteSpace(userInput) || userInput.ToLower() == "exit")
        break;

    try
    {
        Console.WriteLine("🔄 Processing...");
        var result = await AIHub.Mcp()
            .WithConfig(airtableMcp)
            .PromptAsync(userInput);

        Console.WriteLine("✅ Response:");
        Console.WriteLine(result.Message.Content);
        Console.WriteLine();
    }
    catch (Exception ex)
    {
        Console.WriteLine($"❌ Error: {ex.Message}");
        Console.WriteLine();
    }
}

Console.WriteLine("👋 Goodbye!");
Enter fullscreen mode Exit fullscreen mode

Step 4: Understanding the code

Let's break down the most important parts of our application:

4.1 Environment variables & API keys

var airtableApiKey = Environment.GetEnvironmentVariable("AIRTABLE_API_KEY");
var openAiApiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Reads API keys from your system environment
  • Makes them available to the application
  • Keeps secrets secure (not hardcoded in source code)

4.2 MaIN.NET configuration

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddEnvironmentVariables()
    .Build();
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Creates configuration object for MaIN.NET
  • Enables reading environment variables and config files
  • Sets up the foundation for dependency injection

4.3 Service registration

var services = new ServiceCollection();
services.AddMaIN(configuration, settings =>
{
    settings.BackendType = BackendType.OpenAi;
    settings.OpenAiKey = openAiApiKey;
});
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Registers MaIN.NET services in dependency injection container
  • Configures AI backend (OpenAI in this case)
  • Sets up all required services (MCP, Chat, etc.)

4.4 Service provider initialization

var serviceProvider = services.BuildServiceProvider();
serviceProvider.UseMaIN();
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Builds the service container with all registered services
  • Initializes MaIN.NET framework and makes it ready to use
  • Creates the AIHub instance for MCP operations

4.5 MCP configuration

var airtableMcp = new Mcp
{
    Name = "Airtable",
    Command = "npx",
    Arguments = ["-y", "airtable-mcp-server"],
    Model = "gpt-4o-mini",
    Backend = BackendType.OpenAi,
    EnvironmentVariables = new Dictionary<string, string>
    {
        ["AIRTABLE_API_KEY"] = airtableApiKey
    }
};
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Defines MCP server configuration
  • Specifies how to run the Airtable MCP server (npx airtable-mcp-server)
  • Sets AI model and backend for processing
  • Passes API key to the MCP server

4.6 How MCP servers work

When you call AIHub.Mcp(), MaIN.NET automatically:

  1. Starts the MCP server process using the specified command and arguments:
   npx -y airtable-mcp-server
Enter fullscreen mode Exit fullscreen mode
  1. Establishes communication between your .NET app and the MCP server via stdio (standard input/output)

  2. Handles the protocol - Your app sends requests, the MCP server processes them and returns responses

  3. Manages the lifecycle - Starts the server when needed, stops it when the application closes

The MCP server is a separate Node.js process that runs alongside your .NET application. It acts as a bridge between your AI queries and the Airtable API.

Note: You need Node.js installed on your system for MCP servers to work, as they are typically Node.js applications. The setup.sh script checks for Node.js installation and helps configure your environment.

4.7 Interactive query loop

while (true)
{
    Console.Write("🤖 Query: ");
    var userInput = Console.ReadLine();

    if (string.IsNullOrWhiteSpace(userInput) || userInput.ToLower() == "exit")
        break;

    try
    {
        var result = await AIHub.Mcp()
            .WithConfig(airtableMcp)
            .PromptAsync(userInput);

        Console.WriteLine(result.Message.Content);
    }
    catch (Exception ex)
    {
        Console.WriteLine($"❌ Error: {ex.Message}");
    }
}
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Creates interactive loop for user queries
  • Uses AIHub.Mcp() to send queries to MCP server
  • Processes natural language through AI backend
  • Handles errors gracefully with try-catch
  • Displays results to user

4.8 The magic: AIHub.Mcp()

var result = await AIHub.Mcp()
    .WithConfig(airtableMcp)
    .PromptAsync(userInput);
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Creates MCP client connection
  • Sends your query to the Airtable MCP server
  • Processes through AI (OpenAI) to understand intent
  • Executes appropriate Airtable API calls
  • Returns natural language response

Key concepts

MCP (Model Context Protocol)

  • Standardized way for AI to interact with external tools
  • Your queryAI understandsMCP translatesAirtable APIResponse

AIHub

  • Central interface for all MaIN.NET operations
  • Manages connections to AI backends and MCP servers
  • Handles the complexity of AI + MCP integration

Dependency Injection

  • Modern .NET pattern for managing services
  • Makes testing easier and code more maintainable
  • MaIN.NET uses it to organize all its components

3.2 Set environment variables

# Set your API keys
export AIRTABLE_API_KEY="pat_your_airtable_token_here"
export OPENAI_API_KEY="sk_your_openai_key_here"
Enter fullscreen mode Exit fullscreen mode

3.3 Build and run

# Build the application
dotnet build

# Run the application
dotnet run
Enter fullscreen mode Exit fullscreen mode

Step 5: Test your application

5.1 Basic queries

Try these example queries:

Query: Show me all employees
Query: List employees in Engineering department
Query: Who was hired in 2023?
Query: Show employees with salary above 80000
Query: How many active employees do we have?
Enter fullscreen mode Exit fullscreen mode

5.2 Data operations

Query: Add a new employee: Alex Brown, alex@company.com, Engineering, Junior Developer
Query: Update Sarah Johnson's salary to 80000
Query: Mark Tom Wilson as inactive
Enter fullscreen mode Exit fullscreen mode

5.3 Handling single select fields

When working with Airtable Single Select fields (like Department), make sure to use exact values:

Query: What departments are available?
Query: Show me all employees and their departments
Query: Add a new employee: Alex Brown, alex@company.com, Engineering, Junior Developer
Enter fullscreen mode Exit fullscreen mode

Important: Single Select fields only accept predefined values. Common department values might be:

  • Engineering
  • Marketing
  • Sales
  • HR
  • Finance

Make sure to use the exact spelling and case as defined in your Airtable base.

Step 6: Using other backends

MaIN.NET supports multiple AI backends. While this tutorial uses OpenAI, you can easily switch to other providers:

Groq

settings.BackendType = BackendType.GroqCloud;
settings.GroqCloudKey = Environment.GetEnvironmentVariable("GROQ_API_KEY");
Enter fullscreen mode Exit fullscreen mode

Gemini

settings.BackendType = BackendType.Gemini;
settings.GeminiKey = Environment.GetEnvironmentVariable("GEMINI_API_KEY");
Enter fullscreen mode Exit fullscreen mode

DeepSeek

settings.BackendType = BackendType.DeepSeek;
settings.DeepSeekKey = Environment.GetEnvironmentVariable("DEEPSEEK_API_KEY");
Enter fullscreen mode Exit fullscreen mode

Simply change the backend configuration and update the model name accordingly.

What you've built

You've created a powerful console application that:

Integrates MCP servers with MaIN.NET framework
Provides natural language interface to Airtable data
Supports multiple AI backends (OpenAI, Groq, Gemini, etc.)
Handles real-time data operations (CRUD)
Offers interactive query mode for easy data exploration
Manages Single Select fields properly in Airtable

Next steps

  • Add more MCP servers: GitHub, File System, etc.
  • Create web interface: Build a Blazor web app
  • Implement caching: Improve performance for frequent queries

Explore more MCP servers

Now that you've mastered Airtable integration, you can expand your application with other MCP servers! Here are some popular database and data-related servers you can try:

Database servers

  • BigQuery - Google's data warehouse integration
  • ClickHouse - High-performance analytical database
  • MySQL - Popular relational database
  • PostgreSQL - Advanced open-source database
  • MongoDB - NoSQL document database
  • SQLite - Lightweight file-based database

Cloud & API servers

  • GitHub - Repository and issue management
  • File System - Local file operations
  • Web Scraping - Extract data from websites
  • Email - Send and manage emails
  • Slack - Team communication integration

How to add new MCP servers

Simply update your MCP configuration:

// Example: Adding GitHub MCP
var githubMcp = new Mcp
{
    Name = "GitHub",
    Command = "npx",
    Arguments = ["-y", "@modelcontextprotocol/server-github"],
    Model = "gpt-4o-mini",
    Backend = BackendType.OpenAi,
    EnvironmentVariables = new Dictionary<string, string>
    {
        ["GITHUB_TOKEN"] = Environment.GetEnvironmentVariable("GITHUB_TOKEN")
    }
};

// Use it in your queries
var result = await AIHub.Mcp()
    .WithConfig(githubMcp)
    .PromptAsync("Show me recent commits in my repository");
Enter fullscreen mode Exit fullscreen mode

Browse all available servers

Check out the complete collection of MCP servers at mcpservers.org, especially the Database category for more data integration options!

Popular categories:

Resources


Happy coding!

Top comments (0)