Sponsored by Deepsite.site

Integrating Model Context Protocol (MCP) Tools with Semantic Kernel: A Step-by-Step Guide

Created By
LiteObject8 months ago
Integrate Model Context Protocol (MCP) tools with Microsoft Semantic Kernel to build AI-driven workflows. This C# project connects MCP servers to Semantic Kernel, enabling LLMs to call external tools seamlessly. Features example code for developers exploring AI orchestration with .NET.
Content

Integrating Model Context Protocol (MCP) Tools with Semantic Kernel: A Step-by-Step Guide

This repository demonstrates how to integrate Model Context Protocol (MCP) tools with Microsoft Semantic Kernel, enabling seamless interaction between AI models and external data sources or tools. By following this guide, you'll learn how to connect to an MCP server, convert MCP tools into Semantic Kernel functions, and leverage large language models (LLMs) for function calling—all within a reusable and extensible framework.

What is Model Context Protocol (MCP)?

The Model Context Protocol (MCP) is an open-standard protocol designed to standardize how applications provide context to AI models. It acts as a universal connector, allowing LLMs to interact with diverse data sources (e.g., APIs, databases, or services) in a consistent way. Think of MCP as a bridge that enhances AI interoperability, flexibility, and contextual understanding.

In this project, we use MCP to expose tools that Semantic Kernel can consume, enabling AI-driven workflows with real-world applications like automation, data retrieval, or system integration.

Why Use Semantic Kernel with MCP?

Microsoft Semantic Kernel is a powerful SDK that simplifies building AI agents and orchestrating complex workflows. By integrating MCP tools, you can:

  • Extend Semantic Kernel with external capabilities via MCP servers.
  • Enable LLMs to call functions dynamically based on user prompts.
  • Promote interoperability between AI models and non-Semantic Kernel applications.
  • Simplify development with a standardized protocol for tool integration.

This repository provides a practical example of how to combine these technologies, complete with sample code to get you started.

Prerequisites

Before diving into the code, ensure you have the following:

  • .NET SDK (version 8.0 or later recommended).
  • A valid OpenAI API key (or another LLM provider compatible with Semantic Kernel).
  • The ModelContextProtocol NuGet package.
  • Basic familiarity with C# and Semantic Kernel concepts.
  • (Optional) An MCP server to test with, such as the example "Everything" MCP server for demo purposes.

Step-by-Step Guide

This section walks you through the process of integrating MCP tools with Semantic Kernel, as implemented in this repository.

Step 1: Set Up Your Project

  1. Clone this repository:

    git clone https://github.com/LiteObject/mcp-with-semantic-kernel.git
    cd mcp-with-semantic-kernel
    
  2. Restore dependencies:

    dotnet restore
    
  3. Configure your OpenAI API key (or other LLM credentials) using environment variables or user secrets:

    dotnet user-secrets set "OpenAI:ApiKey" "your-api-key"
    dotnet user-secrets set "OpenAI:ChatModelId" "gpt-4o"
    

Step 2: Connect to an MCP Server

The project includes code to connect to an MCP server using the ModelContextProtocol package. The MCP client retrieves available tools from the server, which can then be used by Semantic Kernel.

Example code (see mcp-with-semantic-kernel/src/Program.cs):

using ModelContextProtocol;

var mcpConfig = new McpServerConfig
{
    Id = "everything",
    Name = "Everything",
    TransportType = TransportTypes.Sse,
    Location = "http://localhost:8931"
};

var mcpClient = await McpClientFactory.CreateAsync(mcpConfig);
var tools = await mcpClient.ListToolsAsync();

This snippet establishes a connection to an MCP server (e.g., the "Everything" demo server) and fetches its available tools.

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
CursorThe AI Code Editor
DeepChatYour AI Partner on Desktop
Playwright McpPlaywright MCP server
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
WindsurfThe new purpose-built IDE to harness magic
ChatWiseThe second fastest AI chatbot™
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Serper MCP ServerA Serper MCP Server
Tavily Mcp
Amap Maps高德地图官方 MCP Server
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。