Artificial Intelligence (AI) agents are evolving beyond simple chatbots into systems capable of interacting with enterprise applications, databases, APIs, and business workflows. However, integrating AI models with external tools has traditionally required custom connectors for every application, making development and maintenance complex.
The Model Context Protocol (MCP) addresses this challenge by providing a standardized protocol that allows AI models to discover and invoke tools exposed by applications. Instead of building custom integrations for each AI platform, developers can create an MCP server that exposes business capabilities consistently.
In this article, you'll learn how to build an MCP server using ASP.NET Core, expose enterprise tools, and apply production-ready practices for security, logging, dependency injection, and scalability.
What Is the Model Context Protocol (MCP)?
Model Context Protocol (MCP) is an open protocol that standardizes communication between AI applications and external systems.
An MCP server can expose:
Tools
Resources
Prompts
An AI client discovers these capabilities dynamically instead of relying on hardcoded integrations.
Traditional architecture:
AI Model
|
Custom API Integration
|
Business Application
MCP architecture:
AI Model
|
MCP Client
|
-------------------
| MCP Server |
-------------------
|
Business Services
Database
REST APIs
ERP / CRM
Cloud Services
This architecture enables interoperability between AI assistants and enterprise software while reducing integration effort.
Why Use MCP in Enterprise Applications?
Organizations often maintain dozens of internal systems.
Examples include:
Customer Management
Inventory
HR Systems
ERP
Document Management
Internal APIs
Without MCP, each AI assistant requires custom integration with every system.
With MCP:
One standardized integration
Reusable tools
Easier maintenance
Improved governance
Better scalability
This becomes especially valuable as organizations adopt multiple AI platforms.
Prerequisites
Before starting, ensure you have:
.NET SDK installed
Visual Studio or Visual Studio Code
Basic ASP.NET Core knowledge
Familiarity with dependency injection
Basic REST API concepts
Create an ASP.NET Core Project
Create a new Web API.
dotnet new webapi -n EnterpriseMcpServer
Navigate into the project.
cd EnterpriseMcpServer
Add the required MCP package (package names may evolve as the ecosystem matures, so consult the latest documentation for the implementation you choose).
dotnet add package ModelContextProtocol.AspNetCore
If your chosen MCP library differs, adapt the registration APIs accordingly.
Understanding the Project Structure
A clean enterprise project might look like this:
EnterpriseMcpServer
|
|-- Tools
| EmployeeTools.cs
|
|-- Services
| EmployeeService.cs
|
|-- Models
| Employee.cs
|
|-- Program.cs
|
|-- appsettings.json
Keeping business logic inside services makes testing and maintenance easier.
Configure the MCP Server
Register required services inside Program.cs.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<EmployeeService>();
builder.Services.AddMcpServer()
.WithTools();
var app = builder.Build();
app.MapMcp();
app.Run();
Why This Configuration?
AddSingleton<EmployeeService>()registers reusable business logic.AddMcpServer()enables the MCP server.WithTools()registers discoverable tools.MapMcp()exposes the MCP endpoint.
Keeping MCP configuration centralized simplifies future maintenance.
Create a Business Service
Avoid placing business logic directly inside tools.
public class EmployeeService
{
private readonly List<Employee> employees =
[
new Employee(1,"Alice","Engineering"),
new Employee(2,"Bob","Finance"),
new Employee(3,"David","HR")
];
public IEnumerable<Employee> GetEmployees()
{
return employees;
}
public Employee? FindEmployee(int id)
{
return employees.FirstOrDefault(e => e.Id == id);
}
}
This service can later connect to SQL Server, PostgreSQL, or another data source without changing the MCP tool implementation.
Define the Employee Model
public record Employee(
int Id,
string Name,
string Department
);
Using records provides concise, immutable models suitable for data transfer.
Create MCP Tools
Now expose enterprise functionality.
using ModelContextProtocol.Server;
[McpServerToolType]
public class EmployeeTools
{
private readonly EmployeeService service;
public EmployeeTools(EmployeeService service)
{
this.service = service;
}
[McpServerTool]
public IEnumerable<Employee> GetEmployees()
{
return service.GetEmployees();
}
[McpServerTool]
public Employee? GetEmployee(int id)
{
return service.FindEmployee(id);
}
}
Each method decorated with McpServerTool becomes discoverable by compatible MCP clients.
Instead of calling REST endpoints manually, AI agents can inspect the available tools and invoke them using structured parameters.
How AI Discovers These Tools
When an MCP client connects, it can retrieve metadata about available tools.
Example:
Available Tools
GetEmployees()
Returns all employees
GetEmployee(id)
Returns employee details
The AI model understands:
Tool name
Parameters
Return type
Description (when provided)
This eliminates hardcoded integrations.
Dependency Injection Matters
A common mistake is embedding business logic inside tool classes.
Avoid this:
public class EmployeeTools
{
public List<Employee> GetEmployees()
{
// Business logic here
}
}
Instead:
Tool
|
Service
|
Repository
|
Database
Benefits include:
Easier unit testing
Better separation of concerns
Improved maintainability
Reusable business services
Adding Logging
Enterprise applications should log tool execution for diagnostics and auditing.
public class EmployeeService
{
private readonly ILogger<EmployeeService> logger;
public EmployeeService(
ILogger<EmployeeService> logger)
{
this.logger = logger;
}
public IEnumerable<Employee> GetEmployees()
{
logger.LogInformation("Employee list requested.");
return employees;
}
}
Logging helps diagnose failures, monitor usage, and troubleshoot production issues.
Avoid logging sensitive information such as passwords, tokens, or personally identifiable information.
Security Considerations
Exposing internal tools to AI systems requires careful security planning.
Recommended practices:
Protect MCP endpoints using authentication.
Apply role-based authorization.
Validate all tool parameters.
Limit tool permissions to the minimum required.
Audit tool invocations.
Rate-limit requests to prevent abuse.
Avoid exposing internal administrative operations unless necessary.
Treat every AI client as an external consumer and apply the same security standards as you would for public APIs.
Production Best Practices
Consider the following when deploying MCP servers:
| Practice | Why It Matters |
|---|---|
| Dependency Injection | Keeps business logic separate from tool definitions |
| Structured Logging | Simplifies debugging and monitoring |
| Authentication | Prevents unauthorized access |
| Input Validation | Protects against invalid or malicious input |
| Version APIs | Supports backward compatibility |
| Centralized Configuration | Simplifies environment management |
| Health Checks | Improves operational monitoring |
Common Mistakes
| Mistake | Better Approach |
|---|---|
| Business logic inside tools | Delegate to services |
| Exposing every internal API | Publish only necessary tools |
| Missing input validation | Validate all incoming parameters |
| Logging sensitive data | Log only operational details |
| Ignoring authorization | Secure every exposed endpoint |
| Tight coupling | Use dependency injection and interfaces |
Troubleshooting
Tools are not visible
Verify that:
The tool class is registered.
WithTools()is configured.Tool attributes are applied correctly.
The MCP endpoint is mapped.
Dependency injection errors
Ensure every required service is registered in Program.cs.
Tool execution fails
Check:
Application logs
Parameter validation
Exception handling
Database or external service connectivity
Authentication issues
Verify:
Access tokens
Authentication middleware
Authorization policies
Required permissions
Comparing REST APIs and MCP Servers
| Feature | REST API | MCP Server |
|---|---|---|
| AI Tool Discovery | No | Yes |
| Standardized Tool Metadata | No | Yes |
| Dynamic Tool Invocation | Limited | Yes |
| Works with Traditional Applications | Yes | Yes |
| Designed for AI Agents | No | Yes |
| Reusable Across AI Platforms | Limited | Yes |
REST APIs remain essential for general-purpose integrations, while MCP provides an AI-centric layer that enables standardized tool discovery and invocation.
Frequently Asked Questions
Is MCP a replacement for REST APIs?
No. MCP complements REST APIs by exposing AI-friendly interfaces. Your underlying business services can continue to use REST, gRPC, or other communication protocols.
Can an MCP server connect to databases?
Yes. MCP tools can interact with SQL Server, PostgreSQL, Redis, cloud services, or any supported data source through your application's business layer.
Should business logic be implemented inside MCP tools?
No. Keep tools lightweight and delegate business operations to services. This improves testability and maintainability.
Can existing ASP.NET Core applications support MCP?
Yes. Many existing ASP.NET Core applications can expose selected business capabilities through an MCP server without requiring a complete redesign.
Is MCP suitable for enterprise environments?
Yes. When combined with authentication, authorization, logging, monitoring, and secure deployment practices, MCP provides a standardized way to expose enterprise capabilities to AI agents.
Conclusion
The Model Context Protocol simplifies how AI agents interact with enterprise applications by introducing a standardized approach to tool discovery and invocation. Instead of maintaining multiple custom integrations, developers can expose reusable business capabilities through an ASP.NET Core MCP server.
By combining ASP.NET Core's dependency injection, logging, and middleware pipeline with MCP's standardized protocol, you can build AI-ready services that are easier to maintain, more secure, and better aligned with enterprise architecture. As AI agents become increasingly integrated into business workflows, understanding how to design and deploy MCP servers will be an increasingly valuable skill for .NET developers.

Join the conversation! Your thoughts help the community grow.