返回 MCP 目录
public公开dns本地运行

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

运行方式说明

cloud

托管运行

托管运行通常表示这个 MCP Server 由服务方环境承载,用户一般按页面提供的连接方式或授权流程接入,不需要在本地长期启动一个 MCP 进程

  1. 打开服务方连接页
  2. 完成授权或复制端点
  3. 在 MCP 客户端中连接
terminal

本地运行 / 其它方式

本地运行通常需要用户在自己的电脑或服务器上安装依赖,把 server_config 复制到 MCP 客户端,并按 env_schema 补齐环境变量、密钥或其它配置

  1. 复制 server_config
  2. 安装所需依赖
  3. 补齐环境变量后重启客户端