Table of Contents

👉 Try the demo: https://github.com/LM-Kit/lm-kit-net-samples/tree/main/console_net/agents/mcp_integration

MCP Integration for C# .NET Applications


Purpose of the Sample

MCP Integration demonstrates how to use LM-Kit.NET to connect AI assistants with external tools via the Model Context Protocol (MCP) - an open protocol for AI-tool communication.

The sample shows how to:

  • Connect to MCP servers using McpClient.
  • Discover available MCP tools dynamically.
  • Integrate MCP tools with MultiTurnConversation.
  • Handle tool invocations across the MCP protocol.

Why MCP with LM-Kit.NET?

  • Ecosystem access: connect to hundreds of existing MCP servers.
  • Standardization: uses the open MCP specification.
  • Decoupling: tools run in separate processes for isolation.
  • Discovery: dynamically discover tools from any MCP server.
  • Local-first: all AI processing runs on your hardware.

Sample Application Description

Console app that:

  • Connects to a specified MCP server (stdio or HTTP).
  • Discovers available tools from the server.
  • Allows natural language queries that invoke MCP tools.
  • Demonstrates the MCP client-server communication pattern.

Key Features

  • McpClient: connects to MCP servers via stdio or HTTP.
  • Dynamic Tool Discovery: fetches tool schemas from servers.
  • Native Integration: MCP tools work like built-in tools.
  • Protocol Compliance: follows the MCP specification.

What is MCP?

Model Context Protocol (MCP) is an open protocol that standardizes how AI applications communicate with external tools and data sources.

Key concepts:

  • MCP Server: provides tools, resources, and prompts
  • MCP Client: consumes tools from servers
  • Tools: actions the AI can invoke
  • Resources: data the AI can access
  • Prompts: reusable prompt templates

MCP Server Connection

using LMKit.Mcp.Client;

// Connect via stdio (local process)
var client = new McpClient();
await client.ConnectStdioAsync("npx", "-y", "@modelcontextprotocol/server-filesystem");

// Discover available tools
var tools = await client.ListToolsAsync();

foreach (var tool in tools)
{
    Console.WriteLine($"Tool: {tool.Name} - {tool.Description}");
}

// Use with MultiTurnConversation
conversation.RegisterTools(client.GetToolRegistry());

Server Purpose
@modelcontextprotocol/server-filesystem File system access
@modelcontextprotocol/server-github GitHub integration
@modelcontextprotocol/server-slack Slack messaging
@modelcontextprotocol/server-postgres PostgreSQL database

Getting Started

Prerequisites

  • .NET 8.0 or later
  • VRAM for selected model (4-11 GB)
  • Node.js (for npx-based MCP servers)
  • Or: any MCP server you want to connect to

Run

cd demos/console_net/agents/mcp_integration
dotnet build
dotnet run

Additional Resources