Back to MCP directory
publicPublicdnsLocal runtime

Gmail-mcp-server

一个基于fastMCP的Gmail邮件发送MCP服务器,支持多种附件添加方式,具有高容错性设计。

article

README

🚀 Gmail-MCP-Server

Gmail-MCP-Server 是一个借助 fastMCP 构建的高弹性 MCP 服务器,可通过 Gmail 的 SMTP 服务器发送电子邮件,还能利用 AI 代理处理文件附件。

smithery badge

🚀 快速开始

此仓库包含一个使用 fastMCP 构建的有弹性的 MCP 实现。该服务器旨在通过 Gmail 的 SMTP 服务器发送电子邮件,并支持以下几种方法来附加文件:

  • 直接文件路径:可附加本地文件系统中存在的文件。
  • 基于 URL 的附件:能下载来自指定 URL 的文件并进行附件处理。
  • 预存附件:可使用存储在特定目录中的附件。

该服务器的设计注重弹性,具备增加的超时设置和优雅的关闭处理机制。

✨ 主要特性

  • Gmail SMTP 集成:使用 Gmail 的 SMTP 服务器通过 TLS 发送电子邮件。(请确保已在您的账户上设置了 Gmail SMTP 并生成应用程序密码)
  • 多种附件方法
    • 可直接从文件路径附加文件。
    • 能下载来自公共 URL 的文件进行处理。
    • 可使用预存的本地存储附件。
  • 弹性设计:增加超时设置和信号处理,实现优雅关闭。
  • 基于环境变量的配置:使用环境变量安全地管理 Gmail 资格凭证。

📦 安装指南

先决条件

  • Python 3.x
  • fastMCP(通过 pip install mcp 安装)
  • python-dotenv(通过 pip install python-dotenv 安装)
  • requests(通过 pip install requests 安装)

安装步骤

  1. 克隆仓库
    git clone <远程仓库 URL>
    cd gmail-mcp-server
    
  2. 设置环境变量 创建根目录下的 .env 文件,并添加您的 Gmail SMTP 资格凭证:
SMTP_USERNAME=your.email@gmail.com 
SMTP_PASSWORD=your_app_password

⚠️ 重要提示

如果您在 Gmail 账户上启用了两步验证,必须生成并使用应用程序密码。

  1. OPEN CLAUDE 配置 OPEN CLAUDE > 设置 > MCP > 配置 > OPEN claude_desktop_config.json > 将文件粘贴到下方路径:
{
  "mcpServers": {
    "gmail-mcp": {
      "command": "python",
      "args": ["PATH_TO_gmail-mcp.py"],
      "port": 5005
    }
  }
}

PATH_TO_gmail-mcp.py 替换为实际的脚本路径。

  1. 使用 pip 安装 fastMCP
pip install mcp
  1. 运行服务器
python your_script_name.py

💻 使用示例

基础用法

1. 直接文件附件

在 Python 脚本中,直接从文件路径附加:

from mcp import Client

client = Client('localhost', 5005)
client.send_email_with_attachment('to@example.com', '主题', '正文', '/path/to/file.txt')

2. 基于 URL 的附件

通过指定的 URL 下载并附加文件:

from mcp import Client

client = Client('localhost', 5005)
client.send_email_with_url_attachment('to@example.com', '主题', '正文', 'http://example.com/file.txt')

3. 预存附件

使用预存的附件进行处理:

from mcp import Client

client = Client('localhost', 5005)
client.send_email_with_pre_attached_file('to@example.com', '主题', '正文', '/path/to/pre_attachment.txt')
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