Back to MCP directory
publicPublicdnsLocal runtime

mpc-csharp-semantickernel

一个展示如何使用Microsoft Semantic Kernel与OpenAI及Model Context Protocol C# SDK的示例项目,包含电子邮件发送功能。

article

README

🚀 mpc-csharp-semantickernel

本项目是一个使用微软语义内核(Semantic Kernel)与OpenAI以及官方C# SDK实现模型上下文协议的示例。通过该示例,你可以参考模型上下文协议的具体实现,其官方仓库链接为:https://github.com/modelcontextprotocol/csharp-sdk

🚀 快速开始

先决条件

  • SMTP邮件服务器(用于EmailTools功能)
  • OpenAI API密钥

如何运行示例项目

  1. 使用OpenAI开发者仪表盘获取一个API密钥。
  2. 填充每个项目的appsettings配置信息。
  3. 工具/EmailTool.cs文件中硬编码了一个电子邮件地址字典,用于发送邮件。请更新此字典中的电子邮件地址列表,建议将其改为配置设置。
  4. 运行项目,默认启动地址为http://localhost:5109

配置说明

WebAppMCPPoc项目

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "OpenAI": {
    "ApiKey": "<yourApiKey>",
    "ChatModelId" : "gpt-4o-mini"
  }
}

MCPServer项目

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "EmailSettings": {
    "SMTPServer": {
      "MailPort": "465",
      "MailServer": "<yourserver.com>",
      "Sender": "<sender@youremail.com>",
      "SenderName": "<AIEmailSender>",
      "Password": "<EmailPassword>",
      "Username": "<EmailUsername>"
    }
  }
}

发送请求示例

可以使用Postman等工具发送请求。

示例请求: POST https://localhost:7113/chat Body内容:{ "text": "发一封邮件给aFriend,告诉他你是一个AI,并用250字说明你是如何使用C#实现的。"}

将"aFriend"替换为在EmailTool类中的emails字典中已添加的一个名称。

private static readonly Dictionary<string, string> emails = new Dictionary<string, string>
{
    { "example", "mail@example.com" },
    { "aFriend", "friendEmail@gmail.com" }
};

📄 许可证

此项目使用MIT许可证。

help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client