MCP with Semantic Kernel - thangchung/mcp-labs GitHub Wiki

MCP with Semantic Kernel

# server part
dotnet new console -n MCPPredictor.Server
cd MCPPredictor.Server
dotnet add package ModelContextProtocol
// server code
using ModelContextProtocol;

await MCPServer.StartAsync("MCPPredictor", "1");
[McpTool("getTrendingAI", "Return top trending AI topics from Bing search")]
public static class TrendingTool {
    public static string GetTrendingAI() => "Semantic Kernel, MCP, .NET AI";
}
# client part
dotnet new console -n MCPPredictor.Client
cd MCPPredictor.Client
dotnet add package ModelContextProtocol.Client
dotnet add package Microsoft.SemanticKernel
// client code
using ModelContextProtocol.Client;
using Microsoft.SemanticKernel;

var client = new MCPClient("MCPPredictor", "1");
var kernel = Kernel.CreateBuilder()
    .AddAzureOpenAIChatCompletion("gpt-4o-mini", "<endpoint>", "<api-key>")
    .Build();

var tool = client.ToKernelFunction("getTrendingAI");
var result = await kernel.RunAsync(tool);
Console.WriteLine("AI Prediction based on trends: " + result.GetValue<string>());

List of extensions method to map MCP server functions with SK plugins and vice versa

⚠️ **GitHub.com Fallback** ⚠️