> ## Content Index
> Fetch the complete content index at: https://www.hubwiz.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# Semantic Kernel集成MCP
- URL: https://www.hubwiz.com/blog/integrate-mcp-with-semantic-kernel/
- Published: 2025-03-29T01:08:34.000Z
- Updated: 2025-03-29T01:09:36.000Z
- Description: MCP！这是AI领域的新流行词。所以我想——为什么不自己也成为这个潮流的一部分呢？这就是为什么我写这篇关于使用MCP服务器与Semantic Kernel的博客的原因。
- Author: admin
- Tags: APPLICATION, MCP, SEMANTIC KERNEL, #Import 2026-10-14 14:41

**MCP**！这是AI领域的新流行词。所以我想——为什么不自己也成为这个潮流的一部分呢？这就是为什么我写这篇关于使用MCP服务器与Semantic Kernel和Azure AI Foundry的博客的原因。

让我们先来理解一下：**什么是MCP？**

有很多博客和视频帮助我理解了MCP的概念，我会在最后附上那些链接。但对我来说，最简单的方式来理解MCP是这样的：就像REST定义了一种标准化的方式与Web API交互一样，Anthropic提供的\*\*模型上下文协议（MCP）\*\*为LLMs提供了与外部服务交互的标准化方式。

![](https://www.hubwiz.com/blog/content/images/2025/03/image-604.png)

每当我开发一个AI应用程序并想在我的AI代理中添加一个新的工具或功能时，通常需要实现自定义集成。例如，如果我想让我的AI应用连接到AI搜索，我必须定义一个连接器来与AI搜索中的向量存储进行交互。现在，如果我想增加网络搜索能力，我又需要另一个连接器来处理这一点。

像LangChain、Semantic Kernel、AutoGen等AI框架确实提供了一些开箱即用的功能和常见的连接器——但它们并不总是在所有应用中都能很好地扩展。

有了**MCP**，我就不再需要处理这些复杂性了。MCP服务器的开发者已经创建了这些连接器，并提供了一种标准化的方式来访问它们。我所需要做的就是从我的MCP客户端（可以是Claude桌面应用程序、Cursor、Chainlit等）连接到这些MCP服务，并进行工具调用而不必担心集成细节。这不是很好吗？

现在让我们深入一个用例。 我想用 **.NET**构建一个LLM应用程序，在打开新闻网站后点击第一个链接并总结内容。我在之前的[博客](https://medium.com/data-science-collective/step-by-step-guide-to-building-an-openais-operator-like-agent-using-microsoft-s-autogen-framework-1b886b08b525?ref=hubwiz.com)中使用AutoGen创建了一个类似的代理——你可以在那里找到更多细节。

## 1、设置Semantic Kernel

```csharp
var builder = Kernel.CreateBuilder();  
builder.AddAzureOpenAIChatCompletion("GPT4ov1", "https://<AzureOpenAI>.openai.azure.com", "<REPLACE_WITH_KEY>>");  
Kernel kernel = builder.Build();

```

## 2、设置MCP客户端

**在你的.NET应用程序中安装官方的C# MCP并在您的.NET应用程序中设置MCP客户端**

```csharp
public static async Task<IMcpClient> GetMCPClientForPlaywright()  
{  
    McpClientOptions options = new()  
    {  
        ClientInfo = new() { Name = "Playwright", Version = "1.0.0" }  
    };  
  
    var config = new McpServerConfig  
    {  
        Id = "Playwright",  
        Name = "Playwright",  
        TransportType = "stdio",  
        TransportOptions = new Dictionary<string, string>  
        {  
            ["command"] = "npx",  
            ["arguments"] = "-y @playwright/mcp@latest",  
        }  
    };  
  
    var mcpClient = await McpClientFactory.CreateAsync(  
      
        config,  
        options  
        );  
  
    return mcpClient;  
}

```

可以通过打印出服务器上的可用工具来验证你的客户端是否设置正确：

```csharp
var mcpClient = await GetMCPClientForPlaywright();  
  
await foreach (var tool in mcpClient.ListToolsAsync())  
{  
    Console.WriteLine($"{tool.Name} ({tool.Description})");  
}

```

## 3、将MCP函数转换为KernelFunctions

Semantic Kernel工具或插件被包装在`KernelFunctions`中，因此当使用MCP时，我们需要额外一步将其转换为`KernelFunctions`。SK提供了一个扩展程序来完成此操作，[但它不适用于最新版本的.NET](http://xn--ihq1lksu50bhmmu1b3ip89ef3fuvd380h.net/?ref=hubwiz.com) MCP（至少在我尝试时是这样）。因此，我扩展了它以使其适用于最新版本。我会在我的代码参考中分享代码。

```csharp
var KernelFunctions = await mcpClient.MapToFunctionsAsync();  
kernel.Plugins.AddFromFunctions("Browser", KernelFunctions);

```

## 4、发送你的第一条消息

```csharp
// 启用自动函数调用  
var executionSettings = new OpenAIPromptExecutionSettings  
{  
    Temperature = 0,  
    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()  
};  
  
// 提示  
var prompt = "在必应新闻中总结有关MCP的AI新闻。打开第一个链接并总结内容";  
var result = await kernel.InvokePromptAsync(prompt, new(executionSettings)).ConfigureAwait(false);  
Console.WriteLine($"\n\n{prompt}\n{result}");

```

## 5、运行结果

![](https://www.hubwiz.com/blog/content/images/2025/03/image-605.png)

你可以在[这里](https://github.com/akshaykokane/semantic-kernel-playwright-mcp?ref=hubwiz.com)找到代码。

## 6、结束语

MCP真正改变了我们如何将外部服务与大型语言模型集成在一起。凭借其无缝的界面和日益增长的采用率，不难想象未来每个主要的服务都会提供一个MCP兼容层，解锁强大而直观的AI交互。构建AI应用程序从未如此便捷。

---

原文链接：[Build an AI App That Can Browse the Internet Using Microsoft’s Playwright MCP Server & Semantic Kernel — in Just 4 Steps](https://ai.gopubby.com/build-an-ai-app-that-can-browse-the-internet-using-microsofts-playwright-mcp-server-semantic-d7b41de1dcb9?ref=hubwiz.com)

汇智网翻译整理，转载请标明出处