Connect an ASP.NET Core app to MCP servers for AI tool access

a computer screen with a bunch of code on it

LLMs generate code well enough. What they can’t do on their own is reach into your project, pull a file, query your repo, or run a terminal command. The Model Context Protocol closes that gap by giving your AI assistant a standardized way to call external tools without custom glue code for every integration.

This guide covers how to wire up an MCP client inside an ASP.NET Core application, what production deployments need to get right, and where things typically break.

️ How the pieces fit together

A working AI coding assistant built on this stack has five components:

  • ASP.NET Core application: provides the UI or API surface
  • OpenAI or Azure OpenAI: handles language understanding and reasoning
  • MCP client: connects your app to the MCP server
  • MCP server: exposes development tools the AI can call
  • External resources: files, Git repos, APIs, documentation, databases

The model decides which tool to invoke. The MCP server handles the actual interaction with the external resource. Your app stays clean.

Connecting and discovering tools

Connecting to an MCP server takes one call:

using ModelContextProtocol.Client;

var client = await McpClient.ConnectAsync(
    "http://localhost:3000");

Once connected, you can retrieve every tool the server exposes:

var tools = await client.ListToolsAsync();

foreach (var tool in tools)
{
    Console.WriteLine(tool.Name);
}

A typical MCP server for development work might expose tools like File Search, Repository Explorer, Documentation Search, Terminal Commands, Database Query, and Code Analyzer. Your assistant discovers these dynamically rather than having them hardcoded.

Invoking a tool looks like this:

var result = await client.CallToolAsync(
    "searchFiles",
    new
    {
        pattern = "*.cs"
    });

Console.WriteLine(result);
lines of HTML codes

️ Production requirements

Dependency injection and configuration

Register the MCP client through ASP.NET Core’s DI container rather than instantiating it manually. This prevents multiple client instances and makes the client easier to mock in tests.

Store server URLs and model names in appsettings.json:

{
  "Mcp": {
    "ServerUrl": "http://localhost:3000"
  }
}

API keys and secrets belong in environment variables or Azure Key Vault, not in source code.

Logging and error handling

Log connection attempts, tool execution, execution time, errors, and retry attempts. Do not log source code, credentials, or confidential project data.

External tools fail. Network issues, unavailable services, and invalid parameters are all real scenarios. Your error handling should detect connection failures, handle missing tools gracefully, retry transient errors, and return readable messages to the user rather than unhandled exceptions.

Security

An assistant with access to project files and terminal commands is a meaningful attack surface. The basics:

  • Authenticate every MCP client
  • Authorize access per tool, not just per connection
  • Validate user input before any tool execution
  • Restrict access to production resources
  • Use HTTPS for all communications
  • Store credentials securely

Never allow unrestricted execution of tools that can modify files or run system commands.

Performance

A single conversation can trigger multiple tool calls. Reuse MCP client connections, cache frequently accessed metadata, run independent tool calls asynchronously, and cut unnecessary tool discovery requests. Response latency is worth monitoring explicitly.

Deployment options

The ASP.NET Core application deploys on any standard host: Azure App Service, Azure Container Apps, Docker, or Kubernetes. Make sure the MCP server is reachable from wherever the app runs and that environment-specific settings are configured securely.

Common mistakes to avoid

  • Hardcoding MCP server URLs
  • Exposing unrestricted file system access
  • Skipping authentication and authorization
  • Logging sensitive project information
  • Calling every available tool on every request
  • Shipping AI-generated code without review

As the assistant grows, you can split responsibilities across multiple specialized agents, each talking to the same MCP server but focused on a specific task. That separation keeps workflows cleaner and makes the overall system easier to reason about.

Stay on top of AI & Automation with BizStack Newsletter
BizStack  —  Entrepreneur’s Business Stack
Logo