Popular blog tags

MCP Server in C# .NET - How it works?(AI Chat Application Using MCP and LLM)

Published

MCP Host

LLM‑hosting teams embed an MCP client in their host (chatbot framework, IDE plugin, etc.).

 

an MCP client LLM Application such as Claude 

 

Claude,cursor

 

using System;
using System.Threading.Tasks;
using Anthropic.Client;            // hypothetical LLM client SDK
using Anthropic.Mcp.Client;

class Program
{
  static async Task Main()
  {
    var mcpClient = new MpcDemoClient("http://localhost:5000/mcp", "<YOUR-MCP-KEY>");
    var llm = new ClaudeClient("<YOUR-LLM-KEY>");

    Console.WriteLine("Ask: ‘Who is customer 1?’");
    while (true)
    {
      Console.Write("> ");
      var userInput = Console.ReadLine();
      if (string.IsNullOrWhiteSpace(userInput)) break;

      // Example prompt instructing the model to call MCP
      var prompt = @$"
You are an assistant that can call MCP tools.
User says: {userInput}

If the user asks for customer data, respond with:
[MCP-ACTION]
tool: getCustomer
args: {{ ""id"": ""1"" }}
[/MCP-ACTION]

Otherwise, answer normally.
";

      var llmResponse = await llm.GenerateAsync(prompt);
      Console.WriteLine($"Model: {llmResponse.Text}");

      if (llmResponse.Text.Contains("[MCP-ACTION]"))
      {
        // Extract tool name and args (parsing omitted for brevity)...
        var customer = await mcpClient.GetCustomerAsync("1");
        Console.WriteLine($"→ Fetched via MCP: {customer?.Name} ({customer?.Email})");
      }
    }
  }
}

 

https://dev.to/extinctsion/setup-mcp-server-in-c-2jc1